I need to open up python http server on 10 ports, and here is my current code.
self.server_thread = Thread(target=self.start_web_server)
self.httpd = BaseHTTPServer.HTTPServer((HOST_NAME, PORT_NUMBER[0]), MyHandler)
self.httpd1 = BaseHTTPServer.HTTPServer((HOST_NAME, PORT_NUMBER[1]), MyHandler)
self.httpd2 = BaseHTTPServer.HTTPServer((HOST_NAME, PORT_NUMBER[2]), MyHandler)
self.httpd3 = BaseHTTPServer.HTTPServer((HOST_NAME, PORT_NUMBER[3]), MyHandler)
self.httpd4 = BaseHTTPServer.HTTPServer((HOST_NAME, PORT_NUMBER[4]), MyHandler)
self.httpd5 = BaseHTTPServer.HTTPServer((HOST_NAME, PORT_NUMBER[5]), MyHandler)
self.httpd6 = BaseHTTPServer.HTTPServer((HOST_NAME, PORT_NUMBER[6]), MyHandler)
self.httpd7 = BaseHTTPServer.HTTPServer((HOST_NAME, PORT_NUMBER[7]), MyHandler)
self.httpd8 = BaseHTTPServer.HTTPServer((HOST_NAME, PORT_NUMBER[8]), MyHandler)
self.httpd9 = BaseHTTPServer.HTTPServer((HOST_NAME, PORT_NUMBER[9]), MyHandler)
I was wondering if there is any better way to turn this into for loop.
def wait_for_message(self):
print time.asctime(), "Server Starts"
self.httpd.serve_forever()
self.httpd1.serve_forever()
self.httpd2.serve_forever()
self.httpd3.serve_forever()
self.httpd4.serve_forever()
self.httpd5.serve_forever()
self.httpd6.serve_forever()
self.httpd7.serve_forever()
self.httpd8.serve_forever()
self.httpd9.serve_forever()
def stop(self):
self.httpd.shutdown()
self.httpd1.shutdown()
self.httpd2.shutdown()
self.httpd3.shutdown()
self.httpd4.shutdown()
self.httpd5.shutdown()
self.httpd6.shutdown()
self.httpd7.shutdown()
self.httpd8.shutdown()
self.httpd9.shutdown()
print time.asctime(), "Server Stops"
Also, this is my start and shutdown http server code.
This looks terrible, so would be nice if I also could turn this into a for loop.
Thank you.