Python: asynchronous tcp socketserver

I am looking for http://docs.python.org/library/socketserver.html to try to handle asynchronous requests using socketserver in python. There is an example at the very bottom, but that doesn't make sense. It says that you use port 0, which assigns an arbitrary unused port. But how do you know which port to use for the client if they are not in the same program? I do not quite understand how to make this useful.

+4
source share
4 answers

Since the client is implemented in the same script as the server, the port is known. In a real scenario, you must specify the port for your daemon. In addition, so that your clients know which port to connect to, you may also need to know so that you can open firewalls between your clients and your server.

+9
source

In the example you linked, they retrieve the port:

# Port 0 means to select an arbitrary unused port HOST, PORT = "localhost", 0 server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler) ip, port = server.server_address 

However, you really should look at www.twistedmatrix.com if you are serious about writing asynchronous processing :)

+5
source

You need to get the port that was assigned to socketserver after bind completed: in this case it will probably be via ip, port = server.server_address .

An arbitrary port is easy if you want to create a server without specifying a port: the OS will assign an available port.

Of course, there should also be a way to indicate which port to bind to.

+2
source
 server = ThreadedTCPServer((HOST, 0), ThreadedTCPRequestHandler) ip, port = server.server_address ... client(ip, port, "Hello World 1") 

A PORT value of 0 says, "I don't care what the port number is," so the server_address port value is assigned by calling ThreadedTCPServer (). This is not zero. Later, you pass this port value to the client that uses it.

0
source

All Articles