简体   繁体   中英

Python SimpleHTTPServer keeps going down and I don't know why

This is my first time working with SimpleHTTPServer, and honestly my first time working with web servers in general, and I'm having a frustrating problem. I'll start up my server (via SSH) and then I'll go try to access it and everything will be fine. But I'll come back a few hours later and the server won't be running anymore. And by that point the SSH session has disconnected, so I can't see if there were any error messages. (Yes, I know I should use something like screen to save the shell messages -- trying that right now, but I need to wait for it to go down again.)

I thought it might just be that my code was throwing an exception, since I had no error handling, but I added what should be a pretty catch-all try/catch block, and I'm still experiencing the issue. (I feel like this is probably not the best method of error handling, but I'm new at this... so let me know if there's a better way to do this)

class MyRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    # (this is the only function my request handler has)
    def do_GET(self):
        if 'search=' in self.path:
            try:
                # (my code that does stuff)
            except Exception as e:
                # (log the error to a file)
            return
        else:
            SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)

Does anyone have any advice for things to check, or ways to diagnose the issue? Most likely, I guess, is that my code is just crashing somewhere else... but if there's anything in particular I should know about the way SimpleHTTPServer operates, let me know.

I never had SimpleHTTPServer running for an extended period of time usually I just use it to transfer a couple of files in an ad-hoc manner, but I guess that it wouldn't be so bad as long as your security restraints are elsewhere (ie firewall) and you don't have need for much scale.

The SSH session is ending, which is killing your tasks (both foreground and background tasks). There are two solutions to this:

  1. Like you've already mentioned use a utility such as screen to prevent your session from ending.
  2. If you really want this to run for an extended period of time, you should look into your operating system's documentation on how to start/stop/enable services (now-a-days most of the cool kids are using systemd , but you might also find yourself using SysVinit or some other init system)

EDIT:

This link is in the comments, but I thought I should put it here as it answers this question pretty well

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM