Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stopping a cherrypy server over http

I have a cherrypy app that I'm controlling over http with a wxpython ui. I want to kill the server when the ui closes, but I don't know how to do that. Right now I'm just doing a sys.exit() on the window close event but thats resulting in

Traceback (most recent call last):
  File "ui.py", line 67, in exitevent
    urllib.urlopen("http://"+server+"/?sigkill=1")
  File "c:\python26\lib\urllib.py", line 87, in urlopen
    return opener.open(url)
  File "c:\python26\lib\urllib.py", line 206, in open
    return getattr(self, name)(url)
  File "c:\python26\lib\urllib.py", line 354, in open_http
    'got a bad status line', None)
IOError: ('http protocol error', 0, 'got a bad status line', None)

is that because I'm not stopping cherrypy properly?

like image 854
d.c Avatar asked Jan 23 '10 23:01

d.c


People also ask

How do you stop Cherrypies?

exit : >>> help(cherrypy. engine. exit) exit(self) method of cherrypy.

Is CherryPy a Web server?

CherryPy comes with its own web (HTTP) server. That is why CherryPy is self-contained and allows users to run a CherryPy application within minutes of getting the library. The web server acts as the gateway to the application with the help of which all the requests and responses are kept in track.

What is CherryPy used for?

CherryPy is an object-oriented web application framework using the Python programming language. It is designed for rapid development of web applications by wrapping the HTTP protocol but stays at a low level and does not offer much more than what is defined in RFC 7231.


2 Answers

How are you stopping CherryPy? By sending a SIGKILL to itself? You should send TERM instead at the least, but even better would be to call cherrypy.engine.exit() (version 3.1+). Both techniques will allow CherryPy to shut down more gracefully, which includes allowing any in-process requests (like your "?sigkill=1" request itself) to finish up and close cleanly.

like image 116
fumanchu Avatar answered Oct 19 '22 23:10

fumanchu


I use os._exit. I also put it on a thread, so that I can serve up a "you've quit the server" page before exiting.

class MyApp(object):
    @cherrypy.expose
    def exit(self):
        """
        /exit
        Quits the application
        """

        threading.Timer(1, lambda: os._exit(0)).start()
        return render("exit.html", {})
like image 29
Ryan Ginstrom Avatar answered Oct 19 '22 23:10

Ryan Ginstrom