socket function that loops AND returns something

Brad Tilley bradtilley at usa.net
Wed Sep 15 22:26:02 EDT 2004


I have a function that starts a socket server looping continuously 
listening for connections. It works. Clients can connect to it and send 
data.

The problem is this: I want to get the data that the clients send out of 
the loop but at the same time keep the loop going so it can continue 
listening for connections. If I return, I exit the function and the loop 
stops. How might I handle this?

  def listen(ip_param, port_param):
     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     while 1:
         print "\nWaiting for new connection...\n"
         s.listen(1)
         conn, addr = s.accept()
         print "Client", addr[0], "connected and was directed to port", 
addr[1]
         data = conn.recv(1024)
         # I want to get 'data' out of the loop, but keep the loop going	
         print "Client sent this message:", data
         conn.close()



More information about the Python-list mailing list