0

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.

3
  • 1
    Store all servers in a list, instead of holding a variable per server? Commented Nov 9, 2015 at 14:47
  • Honest question: Why would you do that (open ten instances of python http server)? Commented Nov 9, 2015 at 14:58
  • Because I have a program that sends message to 10 ports, and I need to make sure that all messages are transmitted correctly. That's why I had to open ten instances of python http server. Commented Nov 9, 2015 at 15:09

1 Answer 1

2

Put the servers into a list:

servers = [HTTPServer((HOST_NAME, port_number), MyHandler)
           for port_number in PORT_NUMBER]

To start servers each in their own thread:

from threading import Thread

for httpd in servers:
    Thread(target=httpd.serve_forever).start()

To stop all servers gracefully:

import logging

for httpd in servers:
    try:
        httpd.shutdown()
    except Exception:
        logging.exception("Can't shutdown %r" % (httpd,)) # log exception here
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.