How to create an HTTP server in Python using the first available port?

I want to avoid hard coding of the port number, as in the following:

httpd = make_server('', 8000, simple_app)

The reason I create the server this way is because I want to use it as the “core” for the Adobe AIR application so that it communicates using PyAMF. Since I run this on the client side, it is very possible that any port that I define is already in use. If there is a better way to do this and I am asking the wrong question, please let me know.

+3
source share
4 answers

, . 0, , .

+7

, . 0, , .

, . :

>>> import socket
>>> s = socket.socket()
>>> s.bind(("", 0))
>>> s.getsockname()
('0.0.0.0', 54485)

, 54485.

+6

make_server , ? , , ? , , ( 0 ), .

, , 54315... - .

+2

--. .

, , , .

This is a good place for famous ports .

-one
source

All Articles