10

I want to send a list through TCP sockets but i can't get the exact list when receiving from the server-side . To be more specific say that i have this list:

 y=[0,12,6,8,3,2,10] 

Then, i send each item of the list like this:

 for x in y :
 s.send(str(x))

Now the code of server to recieve the data looks like this:

 while True:
 data = connection.recv(4096)
 if data:
 print('received "%s"' % data)             
 else:
 print('no more data from', client_address)
 break

The problem is that when i run the program i don't get the same list but something like this:

data=[012,6,83,210]

Also, everytime i run the program i get a different result for list data

Any ideas what's going wrong with my code ?

3 Answers 3

21

Use pickle or json to send list(or any other object for that matter) over sockets depending on the receiving side. You don't need json in this case as your receiving host is using python.

import pickle
y=[0,12,6,8,3,2,10] 
data=pickle.dumps(y)
s.send(data)

Use pickle.loads(recvd_data) on the receiving side.

Reference: https://docs.python.org/2/library/pickle.html

Sign up to request clarification or add additional context in comments.

7 Comments

Did you mean a weird string? If then wait for some time, n I will add a little more explanation or you can read the docs. If you understand pickle and still getting something weird then please describe it.
Yes i get a weird string. I just read the docs you posted , and understood why i need to use pickle. However, i can't really describe that weird thing..
I used pickle.loads(connection.recv(4096)) on the receiving side but i get an EOFError. Used try-except but the else statement never gets true
Its because data is not received completely on the receiving side. Try putting connection.recv(4096) inside a loop and loop till you have received the complete data.
Do you know anything about Reed-Solomon Code ?
|
6

Sorry for my extremely late response, but I hope this can help anyone else who has this problem. This may not be the best solution, but I would convert the list into a string, and encode and send that string. Then, the receiving end can receive, decode, and convert this string back into a list using eval(). This would look something like this:

Sender:

y = [0,12,6,8,3,2,10] 
# Convert To String
y = str(y)
# Encode String
y = y.encode()
# Send Encoded String version of the List
s.send(y)

Receiver:

data = connection.recv(4096)
# Decode received data into UTF-8
data = data.decode('utf-8')
# Convert decoded data into list
data = eval(data)

1 Comment

this seems to me like an open door to all sorts of malicious code injections
-1

you can use NetStructer module it's provide the way to send list,str,tupple and more .... . here a simple code

from NetStructer import Bridge

addr = ('192.168.1.4',71) # the address of the server that's you want to connect

bridge = Bridge.Link(addr)# use Bridge class to connect to server with address

bridge.SendBuffer([1,2,3,4,5,6,7,9,10])

2 Comments

Is this your product? How is anyone supposed to find it? Does it have documentation?
Welcome, and please highlight what your answer adds to the already accepted answer stackoverflow.com/a/38413006/12846804 from seven years ago. See How to Answer for guidance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.