Today’s blog post should help if you got the python socket.error errno 48 error you used earlier.
You can also do the following on the next largest available port in Python:
Import SimpleHTTPServerImport SocketServerHandler = SimpleHTTPServer.SimpleHTTPRequestHandlerPort = 8000so far true: Try: httpd = SocketServer.TCPServer (('', Port), Dispatcher) print 'Serving on Port', Port httpd.serve_forever () except SocketServer.socket.error like exc: if exc.args [0]! = 48: breeding Print "port", "port", "already in use". Port = 1 different: Interrupt
If you need to do the same for other utilities, this may be more convenient than a bash script:
#! / usr / bin / env bashMIN_PORT = {1: -1025} USDMAX_PORT = {2: -65535} USD(netstat -atn | awk '{printf "% s n% s n", $ 4, $ 4}' | grep -oE '[0-9] * $'; seq "$ MIN_PORT" "$ MAX_PORT" ) | sort -R | Head -n 1
Configure it as an executable named get-free-port
and you can do the following:
someprogram --port = $ (get-free-port)
This is not as reliable as Python’s own approach because the bash script does not hijack the port – another process might get the port before yours (race condition) – but still quite useful if you are using a utility that does not have a Your own approach to retry.
What Causes The “Socket.error: [Errno 48] Address Is Already In Use” Error?
After a short search, we found the following reasons:
How To Fix Python Socket 48 Error: Address Is Already In Use?