Understanding sockets?

G gharik at comcast.net
Thu Mar 20 18:41:36 EST 2003


Hi ,

I'm new to python and to network programming.
I have written a client and a server, however
all the data that i'm sending from the client is
not appearing at the server. I think my problem
is that the server is blocking on recv() ?
I am not sure. I have posted my code below.
I would appreciate the help.
Oh, and i would like to keep the socket open
for further communication, that is why there is no
socket.close()

the program prints this:
C:\Python23\ralph>python QS.py
(lp0
S'1'
p1
aS'2'
p2
aS'3'
p3
a.(lp0
S'4'
p1
aS'5'
p2
aS'6'
p3
a.
['1', '2', '3']

Thanks in advance.
g


################# CLIENT ##########################
myHost = 'localhost'
myPort = 50007

class Client:

    def __init__(self):
        self.sockobj = socket(AF_INET, SOCK_STREAM)
        self.sockobj.connect((myHost, myPort))

    def send(self, data):
         self.sockobj.sendall(pickle.dumps(data))

if __name__ == "__main__":
    c = Client()
    c.send(['1', '2', '3'])
    c.send(['4', '5' , '6'])

################### SERVER ############################
myHost = ''
myPort = 50007

class Server(SocketServer.BaseRequestHandler):

    def handle(self):
        msg = ""
        while 1:
            data = self.request.recv(1024)
            if not data: break
            msg += data
            print msg
            msg = pickle.loads(msg)                # serialize
            print msg

# make a threaded server, listen/handle clients forever
if __name__ == "__main__":
    myaddr = (myHost, myPort)
    server = SocketServer.ThreadingTCPServer(myaddr, Server)
    server.serve_forever()









More information about the Python-list mailing list