February 21st, 2005, 06:59 AM
Sending arrays over sockets
I was wondering if there is an easy way to send an array over a socket that is holding integers? THis array can be up to 4 mbs in size so it is pretty large.
February 21st, 2005, 08:05 AM
You can only send a string over a socket; or at least that is how Python sees it. However you could send each element in the array and add it to a list at the other end .
One alternative to this would be to send the string representation of the list and parse it at the other end to bring it back to life; you could use eval() to do this although for safety this probably isn't the best idea IMO.
Hope this helps,
February 21st, 2005, 08:52 AM
There is a general method for sending python objects over arbitrary channels using the pickle module. With the pickle module you can translate objects into binary or ascii data streams and vice versa. For an array this may look like this (copied from an IDLE session):
Note that using pickle may rise security issues like using eval or exec as netytan pointed out before.
>>> import pickle
>>> pickle.dumps([1, 2, 3, 4])
[1, 2, 3, 4]
February 21st, 2005, 07:11 PM
You could try something like Pickle, if you dont mind the security issues. Or, you could use something like zlib and then send it over the socket.