[Tutor] using sockets to send a list

Don Arnold Don Arnold" <darnold02@sprynet.com
Sat Feb 22 10:05:02 2003


----- Original Message -----
From: "Adrian Maier" <am@fx.ro>
To: "Abel Daniel" <abli@freemail.hu>
Cc: "Don Arnold" <darnold02@sprynet.com>; <am@fx.ro>; <tutor@python.org>
Sent: Saturday, February 22, 2003 4:16 AM
Subject: Re: [Tutor] using sockets to send a list


> On Sat, Feb 22, 2003 at 08:49:27AM +0100, Abel Daniel wrote:
> > Replacing the print statement with something truly destructive is left
> > as an exercise to the reader.
> > I might trust you (and your server) enough to play a game, but I
> > seriously doubt I would trust you enough to let you execute arbitrary
> > commands on my computer.
> >
> > I would use pickle instead.

I would've, too, if I had known how. ; )

>
> I see.
>
> Now i've found an acceptable temporary solution :
> instead of :     exec("self.player=%s"%dst)
> i do:            self.player = dst
> (i guess this is less dangerous)

Less dangerous, but non-functional: self.player is now a string
representation of what was sent, not an actual object of the appropriate
type.

>
> I'll try to improve the program to use pickle later , when i'll
> have more time.
>

After looking through the documentation and book or two (specifically,
Wesley Chun's "Core Python Programming" and Steve Holden's "Python Web
Programming"), here's what I've come up with:

####################################################
## myData.py - data structure to be sent
####################################################
class Data:
    def __init__(self,*args):
        self.members = [item for item in args]

    def showMembers(self):
        for item in self.members:
            print '\t>>', item

####################################################
## server.py - receives an object from the socket
####################################################
from socket import *
import cPickle
import StringIO
import myData

if __name__ == '__main__':
    HOST = '127.0.0.1'
    PORT = 50000
    BUFSIZE = 1024
    ADDR = (HOST, PORT)

    serverSock = socket(AF_INET, SOCK_STREAM)
    serverSock.bind(ADDR)
    serverSock.listen(5)

    clientSock, address = serverSock.accept()
    print 'connection from:', address

    databuffer = StringIO.StringIO(clientSock.recv(BUFSIZE))

    someobj = cPickle.load(databuffer)

    print type(someobj)
    someobj.showMembers()

    clientSock.close()
    serverSock.close()

####################################################
## 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()



Run server.py in one window, then client.py in another. The client's output
isn't too impressive, but the server's makes up for it (IMHO):

D:\Python22\tutor>d:\python22\python server.py
connection from: ('127.0.0.1', 3590)
<type 'instance'>
        >> 1
        >> 3
        >> 4
        >> stuff

Looks like we received a full-blown object from the socket and are able to
call its methods. Success!

HTH,
Don