[Tutor] using sockets to send a list

Michael Janssen Janssen@rz.uni-frankfurt.de
Sat Feb 22 12:37:02 2003


On Sat, 22 Feb 2003, Don Arnold wrote:

> ####################################################
> ## client.py - sends an object to the server
> ####################################################
> from socket import *
> import cPickle
> import StringIO
> import myData
>
> if __name__ == '__main__':
>     HOST = '127.0.0.1'
>     PORT = 50000
>     BUFSIZE = 1024
>     ADDR = (HOST, PORT)
>
>     someData = myData.Data(1,3,4,'stuff')
>
>     databuffer = StringIO.StringIO()
>
>     cPickle.dump(someData,databuffer)
>
>     clientSock = socket(AF_INET, SOCK_STREAM)
>     clientSock.connect(ADDR)
>     clientSock.send(databuffer.getvalue())
>     clientSock.close()


instead of this three lines:

>     databuffer = StringIO.StringIO()
>     cPickle.dump(someData,databuffer)
>     clientSock.send(databuffer.getvalue())

you can also use:

     clientSock.send(cPickle.dumps(someData))>

--> cPickle's dumps (and loads) produces (and reads from) strings
directly.

Michael