Run 2 Python3 `http.server` on the same machine, different ports

1k views Asked by At

Situation: Need 2 ad-hoc Python3 http.server instances on the same computer.

Problem: The first server was started successfully on the command line

python3 -m http.server 8888

The second server was attempted with the following script:

import http.server
import socketserver as ss

os.chdir("/path/to/working/directory")
Handler = http.server.SimpleHTTPRequestHandler
with ss.TCPServer(("", 8000), Handler) as httpd:
    try:
        httpd.serve_forever()
    except PermissionError:
        print("Permission denied.")

The second server terminated with OSError: [Errno 98] Address already in use.

Question: How can I run two Python3 http.server on the same machine (listening on 0.0.0.0)?

Additional Information 1: I have checked, and there are no other services holding onto port 8888 (server 1's port), and 8000 (server 2's port).

Additional Information 2: I am not sure why, but if I reverse the two ports, both servers run as intended (i.e. server 1 runs on port 8000; server 2 runs on 8888). Any ideas why?

0

There are 0 answers