Sending and receiving arrays via Sockets

Is it possible to send an array via UDP sockets using Python? I am using Python 2.5 and trying to send a simple array, but it does not work. It can send an array successfully, but when I try to print it with an array element, the program will work. I am not sure what the error is, as I take precautions when converting data to an array, but it does not work. I hope I explained the problem as clearly as possible. I would appreciate help!

# Client program from socket import * import numpy from array import* # Set the socket parameters host = "localhost" port = 21567 buf = 4096 addr = (host,port) # Create socket UDPSock = socket(AF_INET,SOCK_DGRAM) def_msg = "===Enter message to send to server==="; print "\n",def_msg a = array('i',[1,3,2]) # Send messages while (1): data = raw_input('yes or now') if data!= "yes": break else: if(UDPSock.sendto(a,addr)): print "Sending message" # Close socket UDPSock.close() # Server program from socket import * # Set the socket parameters host = "localhost" port = 21567 buf = 4096 addr = (host,port) # Create socket and bind to address UDPSock = socket(AF_INET,SOCK_DGRAM) UDPSock.bind(addr) # Receive messages while 1: data,addr = UDPSock.recvfrom(buf) L = eval(data) if not data: print "Client has exited!" break else: print "\nReceived message '", L[1],"'" # Close socket UDPSock.close() 
+8
python udp sockets
source share
6 answers

eval does something completely different than what you think.

To transfer data over a network, you need to serialize it into an array of bytes, and then deserialize it. In Python, most objects can be serialized using the pickle module:

 if (UDPSock.sendto( pickle.dumps(a), addr)): 

Deserialization:

 data,addr = UDPSock.recvfrom(buf) L = pickle.loads(data) print repr(L) # prints array('i', [1, 3, 2]) 
+14
source share

I personally used tostring and fromstring since the built-in serialization methods are many times faster and pickle may not support NaN, Inf and other undefined values.

+6
source share

You can try pickle array. Pickle is a python library for en- and decoding python objects. He is able to do much more, but this is enough to complete your task:

on the sender side you pickle an object in a string:

 pickled_string = pickle.dumps(a) 

on the receiver side you are an unpickle object:

 a = pickle.loads(received_string) # a is now your sent array 
+3
source share

You are trying to send a python object through a socket, it is normal that it does not work, you cannot send objects to a socket, objects are not data, they represent some data in a given programming language. You need to "translate" your object into data and recreate the object from the data on the other side of the socket. One way to do this would be with the pickle module.

On the client side, you "drag" the object:

 data = pickle.dumps(my_array) 

And on the server side, you "print" the received data:

 my_array = pickle.loads(received_data) 
+3
source share

Some time has passed since this question was asked, but I thought it was worth sharing the jsonsocket library. This makes it easy to send strings, lists, and dictionaries over sockets. It can efficiently process large amounts of data. And you do not need to do manual serialization / deserialization. Under the hood, it serializes data as JSON strings on the client and deserializes it on the server.

+1
source share

If you don't need UDP, try zmqObjectExchanger ( https://github.com/ZdenekM/zmq_object_exchanger ). It wraps pickle and zmq to transport python objects over TCP.

0
source share

All Articles