Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bottle web framework - How to stop?

Tags:

python

bottle

When starting a bottle webserver without a thread or a subprocess, there's no problem. To exit the bottle app -> CTRL + c.

In a thread, how can I programmatically stop the bottle web server ?

I didn't find a stop() method or something like that in the documentation. Is there a reason ?

like image 737
Sandro Munda Avatar asked Jul 01 '12 13:07

Sandro Munda


People also ask

How do you run a bottle app?

Bottle framework learning checklistDownload Bottle or install via pip with pip install bottle on your local development machine. Work through the official Bottle tutorial. Start coding your Bottle app based on what you learned in the official tutorial plus reading open source example applications found above.

How do you run a Python bottle?

Run this script or paste it into a Python console, then point your browser to http://localhost:8080/hello/world. That's it. Install the latest stable release with pip install bottle or download bottle.py (unstable) into your project directory. There are no hard [1] dependencies other than the Python standard library.

How to stop bottle web framework without thread?

python - Bottle web framework - How to stop? - Stack Overflow When starting a bottle webserver without a thread or a subprocess, there's no problem. To exit the bottle app -> CTRL + c. In a thread, how can I programmatically stop the bottle web server ?

How to start and stop the bottle in WSGI?

In bottle's code the WSGI server is started with: srv.serve_forever() If you have started bottle in its own thread, you can stop it using: srv.shutdown() To access the srv variable in your code, you need to edit the bottle source code and make it global. After changing the bottle code, it would look like:

How to stop a bottle in a thread?

If you have started bottle in its own thread, you can stop it using: srv.shutdown() To access the srv variable in your code, you need to edit the bottle source code and make it global. After changing the bottle code, it would look like:

How to stop a bottle server?

This question was top in my google search, so i will post my answer: When the server is started with the Bottle() class, it has a method close() to stop the server. From the source code: """ Close the application and all installed plugins.


1 Answers

For the default (WSGIRef) server, this is what I do (actually it is a cleaner approach of Vikram Pudi's suggestion):

from bottle import Bottle, ServerAdapter  class MyWSGIRefServer(ServerAdapter):     server = None      def run(self, handler):         from wsgiref.simple_server import make_server, WSGIRequestHandler         if self.quiet:             class QuietHandler(WSGIRequestHandler):                 def log_request(*args, **kw): pass             self.options['handler_class'] = QuietHandler         self.server = make_server(self.host, self.port, handler, **self.options)         self.server.serve_forever()      def stop(self):         # self.server.server_close() <--- alternative but causes bad fd exception         self.server.shutdown()  app = Bottle()  @app.route('/') def index():     return 'Hello world'  @app.route('/stop')  # not working from here, it has to come from another thread def stopit():     server.stop()    server = MyWSGIRefServer(port=80) try:     app.run(server=server) except:     print('Bye') 

When I want to stop the bottle application, from another thread, I do the following:

server.stop() 
like image 113
mike Avatar answered Oct 24 '22 00:10

mike