fightstarr20
fightstarr20

Reputation: 12598

Python http.server 2 instances and 2 folders

I am using python http.server to init 2 instances at different ports and serve up a html file from a folder...

import http.server
import socketserver
import os

PORT1 = 8000
PORT2 = 8001

os.chdir("html/folder1/")
Handler1 = http.server.SimpleHTTPRequestHandler

os.chdir("../folder2/")
Handler2 = http.server.SimpleHTTPRequestHandler

httpd1 = socketserver.TCPServer(("", PORT1), Handler1)
httpd2 = socketserver.TCPServer(("", PORT2), Handler2)

print("serving at port", PORT1)
print("serving at port", PORT2)

httpd1.serve_forever()
httpd2.serve_forever()

This loads without errors but I am only able to load http://localhost:8000

Any ideas where I am going wrong?

Upvotes: 0

Views: 100

Answers (1)

larsks
larsks

Reputation: 311703

The serve_forever method does just that...serves http requests, and never exits. So when you do this:

httpd1.serve_forever()
httpd2.serve_forever()

The second statement is never executed because the first never exits. Possibly you could make this work by putting each call to serve_forever in a dedicated thread, and then just waiting for all threads to complete, but there may be a better solution.

Upvotes: 2

Related Questions