Python Falcon – Waitress


Python Falcon – Waitress



”;


The development server is not recommended to be used in production environment. The development server is not efficient, stable, or secure.

Waitress is a production-quality pure-Python WSGI server with very acceptable performance. It has no dependencies except ones that live in the Python standard library. It runs on CPython on Unix and Windows.

Make sure that Waitress server has been installed in the working environment. The library contains serve class whose object is responsible for serving the incoming requests. The constructor of serve class requires three parameters.


serve (app, host, port)

The falcon application object is the app parameter. The default values of host and port are localhost 8080 by default. The listen parameter is a string as a combination of host:port parameter defaulting to ”0.0.0.0:8080”

Example

In the hellofalcon.py code, we import the serve class instead of simple_server and instantiate its object as follows −


from waitress import serve
import falcon
class HelloResource:
   def on_get(self, req, resp):
   """Handles GET requests"""
   resp.status = falcon.HTTP_200
   resp.content_type = falcon.MEDIA_TEXT
   resp.text = (
      ''Hello World''
   )
app = falcon.App()
hello = HelloResource()
app.add_route(''/hello'', hello)
if __name__ == ''__main__'':
   serve(app, host=''0.0.0.0'', port=8000)

Execute hellofalcon.py and visit the http://localhost:8000/hellolink in the browser as before. Note that the host 0.0.0.0 makes the localhost publicly visible.

The Waitress server can be launched from the command line also, as shown below −


waitress-serve --port=8000 hellofalcon:app

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *