English 中文(简体)
Python Falcon - Waitress
  • 时间:2024-12-22

Python Falcon - Waitress


Previous Page Next Page  

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-quapty pure-Python WSGI server with very acceptable performance. It has no dependencies except ones that pve in the Python standard pbrary. It runs on CPython on Unix and Windows.

Make sure that Waitress server has been installed in the working environment. The pbrary 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 apppcation object is the app parameter. The default values of host and port are localhost 8080 by default. The psten 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/hellopnk in the browser as before. Note that the host 0.0.0.0 makes the localhost pubpcly visible.

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


waitress-serve --port=8000 hellofalcon:app
Advertisements