1

Here I have some abstract code that I need to work.

>>> s0 = Server(port='5000', handle=print)
>>> s0.stop()
>>> # Starting a second server with another port seems OK.
>>> s1 = Server(port='5001', handle=print)
>>> s1.stop()
>>> # This will raise Exception, because port '5000' is still busy by a first server(s0 object).
>>> # I have to exit python process, to release port.
>>> # P.S. deleting an object does not change anything.
>>> s2 = Server(port='5000', handle=print)

I've tried Flask. I've tried SimpleXMLRPCServer: How to stop BaseHTTPServer.serve_forever() in a BaseHTTPRequestHandler subclass?

Even if it is not raising Exception, the (s2 object) does not accept requests, they are going nowhere, not returning a status or anything.

How can I create a server on port '5000' for example, close this server and create it again when needed. Is it possible with python?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Lazy Bum
  • 35
  • 4
  • Seems based on the answer the function to stop seems to be named `shutdown()` not `stop()` – mousetail Jun 20 '22 at 14:16
  • 1
    A particular port number remains blocked for a couple of minutes after the socket was closed, so that late-arriving packets don't confusingly get delivered to an entirely unrelated socket using the same port. You can disable this by setting the `SO_REUSEADDR` option on the socket; since you're using much higher-level abstractions, I have no idea how (or even *if*) you could get access to the underlying socket to set this option. – jasonharper Jun 20 '22 at 14:18

0 Answers0