Python 3.3 Webserver restarting problems

Posted by IPDGino on Stack Overflow See other posts from Stack Overflow or by IPDGino
Published on 2013-07-01T16:19:16Z Indexed on 2013/07/01 16:20 UTC
Read the original article Hit count: 230

Filed under:
|
|
|

I have made a simple webserver in python, and had some problems with it before as described here: Python (3.3) Webserver script with an interesting error

In that question, the answer was to use a While True: loop so that any crashes or errors would be resolved instantly, because it would just start itself again.

I've used this for a while, and still want to make the server restart itself every few minutes, but on Linux for some reason it won't work for me. On windows the code below works fine, but on linux it keeps saying

Handler class up here
...
...
class Server:

    def __init__(self):

        self.server_class = HTTPServer
        self.server_adress = ('MY IP GOES HERE, or localhost', 8080)
        global httpd
        httpd = self.server_class(self.server_adress, Handler)
        self.main()

    def main(self):

        if count > 1:

            global SERVER_UP_SINCE
            HOUR_CHECK = int(((count - 1) * RESTART_INTERVAL) / 60)

            SERVER_UPTIME = str(HOUR_CHECK) + " MINUTES"


            if HOUR_CHECK > 60:
                minutes = int(HOUR_CHECK % 60)
                hours = int(HOUR_CHECK // 60)
                SERVER_UPTIME = ("%s HOURS, %s MINUTES" % (str(hours), str(minutes)))

            SERVING_ON_ADDR = self.server_adress
            SERVER_UP_SINCE = str(SERVER_UP_SINCE)
            SERVER_RESTART_NUMBER = count - 1        

            print("""
SERVER INFO
-------------------------------------
SERVER_UPTIME: %s
SERVER_UP_SINCE: %s
TOTAL_FILES_SERVED: %d
SERVING_ON_ADDR: %s
SERVER_RESTART_NUMBER: %s
\n\nSERVER HAS RESTARTED
            """ % (SERVER_UPTIME, SERVER_UP_SINCE, TOTAL_FILES, SERVING_ON_ADDR, SERVER_RESTART_NUMBER))

        else:

            print("SERVER_BOOT=1\nSERVER_ONLINE=TRUE\nRESTART_LOOP=TRUE\nSERVING_ON_ADDR:%s" % str(self.server_adress))

        while True:

            try:

                httpd.serve_forever()

            except KeyboardInterrupt:

                print("Shutting down...")
                break
                httpd.shutdown()
                httpd.socket.close()
                raise(SystemExit)

        return


def server_restart():
    """If you want the restart timer to be longer, replace the number after the RESTART_INTERVAL variable"""

    global RESTART_INTERVAL
    RESTART_INTERVAL = 10
    threading.Timer(RESTART_INTERVAL, server_restart).start()
    global count
    count = count + 1
    instance = Server()


if __name__ == "__main__":

    global SERVER_UP_SINCE
    SERVER_UP_SINCE = strftime("%d-%m-%Y %H:%M:%S", gmtime())
    server_restart()

Basically, I make a thread to restart it every 10 seconds (For testing purposes) and start the server. After ten seconds it will say

  File "/home/username/Desktop/Webserver/server.py", line 199, in __init__
    httpd = self.server_class(self.server_adress, Handler)
  File "/usr/lib/python3.3/socketserver.py", line 430, in __init__
    self.server_bind()
  File "/usr/lib/python3.3/http/server.py", line 135, in server_bind
    socketserver.TCPServer.server_bind(self)
  File "/usr/lib/python3.3/socketserver.py", line 441, in server_bind
    self.socket.bind(self.server_address)
OSError: [Errno 98] Address already in use

As you can see in the except KeyboardInterruption line, I tried everything to make the server stop, and the program stop, but it will NOT stop. But the thing I really want to know is how to make this server able to restart, without giving some wonky errors.

© Stack Overflow or respective owner

Related posts about python

Related posts about sockets