简体   繁体   English

用Python腌制,接收到的数据不正确

[英]Pickle in Python, incorrect received data

I am creating simple server client architecture using pickle. 我正在使用pickle创建简单的服务器客户端体系结构。 When I receive data and print it, first number disaapear, why? 当我接收并打印数据时,第一个数字消失了,为什么? how can I avoid this? 我该如何避免呢? for example: ({2,2,4}, {3,4}) - > ({2,4}, {3,4}) 例如:({2,2,4},{3,4})->({2,4},{3,4})

server code: import pickle import socket 服务器代码:import pickle导入套接字

TCP_IP = '127.0.0.1'
TCP_PORT = 5005
BUFFER_SIZE = 1024

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)
conn, addr = s.accept()


print ("conn info: ", conn)
objrcv = pickle.loads(conn.recv(10024))
print("conn recv: ", objrcv)
print("conn from: ", addr)
print(objrcv) 

client code 客户代码

import socket
import pickle

name = 'name'
TCP_IP = '127.0.0.1'
TCP_PORT = 5005
BUFFER_SIZE = 1024

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

data = pickle.dumps(name)
s.connect((TCP_IP, TCP_PORT))
# #s.send(data) #powitalne info, dane gracza

def wyslij_dane(my_array): #sending data
    data = pickle.dumps(my_array)
    if data[0] != 0:  # pierwszy argument odpowiedzialny za poddanie sie
        print('before send')
        s.send(data)
        print('after send') #just to control
        #data_recv = s.recv(BUFFER_SIZE)
        #print("received data:", data_recv)


def rozlacz():#disconnecting
    s.close()

wyslij_dane(({2,2,4}, {3,4}))
wyslij_dane({2,2,433})
wyslij_dane(0)
rozlacz() 

server output: 服务器输出:

conn info:  ...
conn recv:  ({2, 4}, {3, 4})
conn from:  ('127.0.0.1', 51876)
({2, 4}, {3, 4})

And another question is: What can I do to handle all sending data (all call wyslij_dane() funcions)? 另一个问题是:我该怎么办才能处理所有发送的数据(全部调用wyslij_dane()函数)?

"{}" means you are using a set. “ {}”表示您正在使用一套。 In a set every item exists only once. 在一组中,每个项目仅存在一次。 Instead you can use a list with these -> "[]" or a tupel -> "()" So 相反,您可以使用包含以下内容的列表->“ []”或Tupel->“()”

wyslij_dane(({2,2,4}, {3,4}))

becomes 变成

wyslij_dane(([2,2,4], [3,4]))

and so on. 等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM