Alex Rivera | Logout

How do I keep a python HTTP Server up forever?

Asked 2011-09-09T15:24:37.060
9

I wrote a simple HTTP server in python to manage a database hosted on a server via a web UI. It is perfectly functional and works as intended. However it has one huge problem, it won't stay put. It will work for an hour or so, but if left unused for long periods of time when returning to use it I have to re-initialize it every time. Right now the method I use to make it serve is:

def main():
    global db
    db = DB("localhost")
    server = HTTPServer(('', 8080), MyHandler)
    print 'started httpserver...'
    server.serve_forever()

if __name__ == '__main__':
    main()

I run this in the background on a linux server so I would run a command like sudo python webserver.py & to detach it, but as I mentioned previously after a while it quits. Any advice is appreciated cause as it stands I don't see why it shuts down.

Edit
Report

1 Answer

1

Have you tried running it from inside a screen session?

$ screen -L sudo python webserver.py
answered 2011-09-09T15:30:12.733

Your Answer