Python Falcon 简明教程

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 是一个生产级的纯 Python WSGI 服务器,性能非常不错。除了生活在 Python 标准库中的依赖项之外,它没有其他依赖项。它在 Unix 和 Windows 上的 CPython 上运行。

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.

确保 Waitress 服务器已安装在工作环境中。该库包含 serve 类,其对象负责处理传入请求。serve 类的构造函数需要三个参数。

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)

falcon 应用程序对象是 app 参数。默认情况下,host 和 port 的默认值为 localhost 8080。listen 参数是字符串,作为 host:port 参数的组合,默认值为“0.0.0.0:8080”

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

hellofalcon.py 代码中,我们导入 serve 类,而不是 simple_server ,并按如下方式实例化它的对象 −

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)

执行 hellofalcon.py 并像之前一样在浏览器中访问 http://localhost:8000/hellolink 。请注意,主机 0.0.0.0 使 localhost 公开可见。

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.

还可以从命令行启动 Waitress 服务器,如下所示 −

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

waitress-serve --port=8000 hellofalcon:app