webserver - How do I write a python HTTP server to listen on multiple ports? -


i'm writing small web server in python, using basehttpserver , custom subclass of basehttpserver.basehttprequesthandler. possible make listen on more 1 port?

what i'm doing now:

class myrequesthandler(basehttpserver.basehttprequesthandler):   def doget   [...]  class threadinghttpserver(threadingmixin, httpserver):      pass  server = threadinghttpserver(('localhost', 80), myrequesthandler) server.serve_forever() 

sure; start 2 different servers on 2 different ports in 2 different threads each use same handler. here's complete, working example wrote , tested. if run code you'll able hello world webpage @ both http://localhost:1111/ , http://localhost:2222/

from threading import thread socketserver import threadingmixin basehttpserver import httpserver, basehttprequesthandler  class handler(basehttprequesthandler):     def do_get(self):         self.send_response(200)         self.send_header("content-type", "text/plain")         self.end_headers()         self.wfile.write("hello world!")  class threadinghttpserver(threadingmixin, httpserver):     pass  def serve_on_port(port):     server = threadinghttpserver(("localhost",port), handler)     server.serve_forever()  thread(target=serve_on_port, args=[1111]).start() serve_on_port(2222) 

Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -