Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: asynchronous tcp socketserver

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

like image 723
The.Anti.9 Avatar asked Dec 05 '22 04:12

The.Anti.9


2 Answers

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

like image 58
AJ. Avatar answered Dec 07 '22 16:12

AJ.


In the example you linked, they are fetching 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 should really be looking at www.twistedmatrix.com if you are serious about writing async handling :)

like image 26
mthurlin Avatar answered Dec 07 '22 16:12

mthurlin