[Tutor] Question about network server in python

Tino Dai tinoloc at gmail.com
Thu Jun 15 15:20:23 CEST 2006


Hi there,

      I am wondering if somebody to could answer a question about sockets. I
have a socket that
is listening, and a client program connects to it. The client program
transfers a name over, and then disconnects from the socket. Now, how that
is done is using a socket.close() call to shut down the entire socket. My
question is: Is there a way to have the socket close the connection, yet
stay open for the next client that comes along and connects to the server? I
have my already written code below for further documentation. Thanks!

while 1:
  s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
  s.bind((self.ipAddr,self.port))
  s.listen(5)
  print "The port is: ", self.port
  client,addr=s.accept()
          while 1:
                   try:
                       if addr[0] == self.remoteIpAddr:
                           client.send("Connected to the server\n")
                           msg=client.recv(1024)
                           msg=msg.strip()
                           if msg in 'exit':
                                s.close()          #Is there a different way
to write this?
                                time.sleep(30)
                                print "exiting"
                                break
                           if len(msg) > 0:
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060615/d00d3a39/attachment.htm 


More information about the Tutor mailing list