4

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.

4
  • as a first step you could add a except BaseException as e: to catch all the other exceptions that can be raised like SystemExit if your code might raise that by accident. Commented Mar 31, 2016 at 16:02
  • @TadhgMcDonald-Jensen Does BaseException catch more exception types than Exception? Commented Mar 31, 2016 at 16:04
  • yes, if any exception that is not a subclass of BaseException is tried to be raised, it raises TypeError: exceptions must derive from BaseException Commented Mar 31, 2016 at 16:08
  • so BaseException catches all possible error types. though the only ones I can think of are SystemExit and GeneratorExit that are not a subclass of Exception. oh and KeyboardInterrupt Commented Mar 31, 2016 at 16:11

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

how is the ssh session ending? would it raise a KeyboardInterrupt to the python program? (in which case except BaseException would be run instead of quitting)
The SSH session is ending because sshd is configured to disconnect idle sessions after a period of time (you can adjust these settings here is some information on them), and here is some information on what is actually done to kill the processes owned by your session.
Ahhhhhhh okay, that's a basic misunderstanding I had about how ssh works. This is the answer then. Thanks.

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.