pywebio.platform
— 应用部署¶
platform
模块为PyWebIO提供了在不同Web框架中运行PyWebIO应用的支持。
Directory Deploy¶
可以使用 path_deploy()
或 path_deploy_http()
来从一个路径中部署PyWebIO应用。服务端会根据用户访问的URL来确定需要加载的文件并从中读取PyWebIO应用来运行。
-
pywebio.platform.
path_deploy
(base, port=0, host='', index=True, static_dir=None, reconnect_timeout=0, cdn=True, debug=True, allowed_origins=None, check_origin=None, websocket_max_message_size=None, websocket_ping_interval=None, websocket_ping_timeout=None, **tornado_app_settings)[源代码]¶ 从一个路径中部署PyWebIO应用
服务端使用WebSocket协议与浏览器进行通讯。
- 参数
base (str) – 用于加载PyWebIO应用的根目录
port (int) – 服务器监听的端口
host (str) – 服务绑定的地址
index (bool/callable) – 当请求一个文件夹时是否显示默认的索引页面,默认为
True
。index
也可以为一个函数来自定义索引页面,其接收请求的文件夹路径作为参数,返回页面HTML字符串。你可以在文件夹中创建一个index.py
PyWebIO应用文件来重写文件夹的索引页。static_dir (str) – 应用静态文件目录。目录下的文件可以通过
http://<host>:<port>/static/files
访问。例如http_static_dir
路径下存在文件A/B.jpg
,则其URL为http://<host>:<port>/static/A/B.jpg
。reconnect_timeout (int) – 客户端重连超时时间(秒)。客户端若意外与服务端断开连接,在
reconnect_timeout
秒内可以重新连接并自动恢复会话。
剩余参数的详细说明见
pywebio.platform.tornado.start_server()
的同名参数。
-
pywebio.platform.
path_deploy_http
(base, port=0, host='', index=True, static_dir=None, cdn=True, debug=True, allowed_origins=None, check_origin=None, session_expire_seconds=None, session_cleanup_interval=None, **tornado_app_settings)[源代码]¶ 从一个路径中部署PyWebIO应用
服务端使用HTTP协议与浏览器进行通讯。
关于
path_deploy_http()
的base
,port
,host
,index
,static_dir
参数的详细说明见pywebio.platform.path_deploy()
的同名参数。剩余参数的详细说明见
pywebio.platform.tornado_http.start_server()
的同名参数。
Application Deploy¶
start_server()
函数可以启动一个Web服务器来将PyWebIO应用作为Web服务运行。
webio_handler()
和 webio_view()
函数用于将PyWebIO应用整合到现有的Python Web项目中。
在 1.1 版更改: Added the cdn
parameter in start_server()
, webio_handler()
and webio_view()
.
在 1.2 版更改: Added the static_dir
parameter in start_server()
.
Tornado相关¶
服务端可以通过两种协议(WebSocket 和 HTTP)来与用户浏览器通信。
WebSocket¶
-
pywebio.platform.tornado.
start_server
(applications, port=0, host='', debug=False, cdn=True, static_dir=None, reconnect_timeout=0, allowed_origins=None, check_origin=None, auto_open_webbrowser=False, websocket_max_message_size=None, websocket_ping_interval=None, websocket_ping_timeout=None, **tornado_app_settings)[源代码]¶ 启动一个 Tornado server 将PyWebIO应用作为Web服务提供。
Tornado server 使用WebSocket协议与浏览器进行通讯。
Tornado为PyWebIO应用的默认后端Server,可以直接使用
from pywebio import start_server
导入。- 参数
applications (list/dict/callable) –
PyWebIO应用. 可以是任务函数或者任务函数的字典或列表。
类型为字典时,字典键为任务名,类型为列表时,函数名为任务名。
可以通过
app
URL参数选择要运行的任务(例如访问http://host:port/?app=foo
来运行foo
任务), 默认使用运行index
任务函数,当index
任务不存在时,PyWebIO会提供一个默认的索引页作为主页。 参见 Server模式任务函数为协程函数时,使用 基于协程的会话实现 ;任务函数为普通函数时,使用基于线程的会话实现。
port (int) – 服务监听的端口。设置为
0
时,表示自动选择可用端口。host (str) – 服务绑定的地址。
host
可以是IP地址或者为hostname。如果为hostname,服务会监听所有与该hostname关联的IP地址。 通过设置host
为空字符串或None
来将服务绑定到所有可用的地址上。debug (bool) – 是否开启Tornado Server的debug模式,开启后,代码发生修改后服务器会自动重启。 详情请参阅 tornado 文档
cdn (bool/str) – 是否从CDN加载前端静态资源,默认为
True
。支持传入自定义的URL来指定静态资源的部署地址static_dir (str) – 应用静态文件目录。目录下的文件可以通过
http://<host>:<port>/static/files
访问。例如http_static_dir
路径下存在文件A/B.jpg
,则其URL为http://<host>:<port>/static/A/B.jpg
。reconnect_timeout (int) – 客户端重连超时时间(秒)。客户端若意外与服务端断开连接,在
reconnect_timeout
秒内可以重新连接并自动恢复会话。allowed_origins (list) –
除当前域名外,服务器还允许的请求的来源列表。来源包含协议、域名和端口部分,允许使用 Unix shell 风格的匹配模式:
*
为通配符?
匹配单个字符[seq]
匹配seq中的任何字符[!seq]
匹配任何不在seq中的字符
比如
https://*.example.com
、*://*.example.com
全部规则参见 Python文档
check_origin (callable) – 请求来源检查函数。接收请求来源(包含协议、域名和端口部分)字符串作为参数, 返回
True/False
指示服务器接受/拒绝该请求。若设置了check_origin
,allowed_origins
参数将被忽略auto_open_webbrowser (bool) – 当服务启动后,是否自动打开浏览器来访问服务。(该操作需要操作系统支持)
websocket_max_message_size (int/str) – Max bytes of a message which Tornado can accept. Messages larger than the
websocket_max_message_size
(default 10MB) will not be accepted.websocket_max_message_size
can be a integer indicating the number of bytes, or a string ending withK
/M
/G
(representing kilobytes, megabytes, and gigabytes, respectively). E.g:500
,'40K'
,'3M'
websocket_ping_interval (int) – 当被设置后,服务器会以
websocket_ping_interval
秒周期性地向每个WebSockets连接发送’ping’消息。 如果应用处在某些反向代理服务器之后,设置websocket_ping_interval
可以避免WebSockets连接被代理服务器当作空闲连接而关闭。 同时,若WebSockets连接在某些情况下被异常关闭,应用也可以及时感知。websocket_ping_timeout (int) – 如果设置了
websocket_ping_interval
,而服务没有在发送’ping’消息后的websocket_ping_timeout
秒 内收到’pong’消息,应用会将连接关闭。默认的超时时间为websocket_ping_interval
的三倍。tornado_app_settings – 传递给
tornado.web.Application
构造函数的额外的关键字参数 可设置项参考: https://www.tornadoweb.org/en/stable/web.html#tornado.web.Application.settings
-
pywebio.platform.tornado.
webio_handler
(applications, cdn=True, reconnect_timeout=0, allowed_origins=None, check_origin=None)[源代码]¶ 获取在Tornado中运行PyWebIO应用的RequestHandler类。RequestHandler类基于WebSocket协议与浏览器进行通讯。
关于各参数的详细说明见
pywebio.platform.tornado.start_server()
的同名参数。
HTTP¶
-
pywebio.platform.tornado_http.
start_server
(applications, port=8080, host='', debug=False, cdn=True, static_dir=None, allowed_origins=None, check_origin=None, auto_open_webbrowser=False, session_expire_seconds=None, session_cleanup_interval=None, websocket_max_message_size=None, websocket_ping_interval=None, websocket_ping_timeout=None, **tornado_app_settings)[源代码]¶ 启动一个 Tornado server 将PyWebIO应用作为Web服务提供。
服务端使用HTTP协议与浏览器进行通讯。
- 参数
session_expire_seconds (int) – 会话过期时间,单位为秒(默认60秒)。若 session_expire_seconds 秒内没有收到客户端的请求,则认为会话过期。
session_cleanup_interval (int) – 会话清理间隔,单位为秒(默认12秒)。服务端会周期性清理过期的会话,释放会话占用的资源。
剩余参数的详细说明见
pywebio.platform.tornado.start_server()
的同名参数。1.2 新版功能.
-
pywebio.platform.tornado_http.
webio_handler
(applications, cdn=True, session_expire_seconds=None, session_cleanup_interval=None, allowed_origins=None, check_origin=None)[源代码]¶ 获取在Tornado中运行PyWebIO应用的RequestHandler类。RequestHandler类基于HTTP协议与浏览器进行通讯。
关于各参数的详细说明见
pywebio.platform.tornado_http.start_server()
的同名参数。1.2 新版功能.
Flask support¶
使用Flask后端作为PyWebIO应用Server时,需要您自行安装Flask,并确保版本大于等于 0.10
。
可通过以下命令安装:
pip3 install -U flask>=0.10
-
pywebio.platform.flask.
webio_view
(applications, cdn=True, session_expire_seconds=None, session_cleanup_interval=None, allowed_origins=None, check_origin=None)[源代码]¶ 获取在Flask中运行PyWebIO任务的视图函数。基于http请求与前端页面进行通讯
关于各参数的详细说明见
pywebio.platform.flask.start_server()
的同名参数。
-
pywebio.platform.flask.
start_server
(applications, port=8080, host='', cdn=True, static_dir=None, allowed_origins=None, check_origin=None, session_expire_seconds=None, session_cleanup_interval=None, debug=False, **flask_options)[源代码]¶ 启动一个 Flask server 将PyWebIO应用作为Web服务提供。
- 参数
session_expire_seconds (int) – 会话过期时间,单位为秒(默认600秒)。若 session_expire_seconds 秒内没有收到客户端的请求,则认为会话过期。
session_cleanup_interval (int) – 会话清理间隔,单位为秒(默认300秒)。服务端会周期性清理过期的会话,释放会话占用的资源。
debug (bool) – 是否开启Flask Server的debug模式,开启后,代码发生修改后服务器会自动重启。
flask_options – 传递给
flask.Flask.run
函数的额外的关键字参数 可设置项参考: https://flask.palletsprojects.com/en/1.1.x/api/#flask.Flask.run
关于各参数的详细说明见
pywebio.platform.tornado.start_server()
的同名参数
Django support¶
使用Django后端作为PyWebIO应用Server时,需要您自行安装Django,并确保版本大于等于 2.2
。
可通过以下命令安装:
pip3 install -U django>=2.2
-
pywebio.platform.django.
webio_view
(applications, cdn=True, session_expire_seconds=None, session_cleanup_interval=None, allowed_origins=None, check_origin=None)[源代码]¶ 获取在django中运行PyWebIO任务的视图函数。 基于http请求与前端进行通讯
关于各参数的详细说明见
pywebio.platform.flask.webio_view()
的同名参数。
-
pywebio.platform.django.
start_server
(applications, port=8080, host='', cdn=True, static_dir=None, allowed_origins=None, check_origin=None, session_expire_seconds=None, session_cleanup_interval=None, debug=False, **django_options)[源代码]¶ 启动一个 Django server 将PyWebIO应用作为Web服务提供。
- 参数
debug (bool) – 开启 Django debug mode。参见 Django doc .
django_options – django应用的其他设置,见 https://docs.djangoproject.com/en/3.0/ref/settings/ . 其中
DEBUG
、ALLOWED_HOSTS
、ROOT_URLCONF
、SECRET_KEY
被PyWebIO设置,无法在django_options
中指定
剩余参数的详细说明见
pywebio.platform.flask.start_server()
的同名参数。
aiohttp support¶
使用aiohttp后端作为PyWebIO应用Server时,需要您自行安装aiohttp,并确保版本大于等于 3.1
。
可通过以下命令安装:
pip3 install -U aiohttp>=3.1
-
pywebio.platform.aiohttp.
webio_handler
(applications, cdn=True, allowed_origins=None, check_origin=None, websocket_settings=None)[源代码]¶ 获取在aiohttp中运行PyWebIO任务函数的 Request Handler 协程。Request Handler基于WebSocket协议与浏览器进行通讯。
关于
applications
、allowed_origins
、check_origin
参数的详细说明见pywebio.platform.aiohttp.start_server()
的同名参数。- 返回
aiohttp Request Handler
-
pywebio.platform.aiohttp.
start_server
(applications, port=0, host='', debug=False, cdn=True, static_dir=None, allowed_origins=None, check_origin=None, auto_open_webbrowser=False, websocket_settings=None, **aiohttp_settings)[源代码]¶ 启动一个 aiohttp server 将PyWebIO应用作为Web服务提供。
- 参数
websocket_settings (dict) – 创建 aiohttp WebSocketResponse 时使用的参数。见 https://docs.aiohttp.org/en/stable/web_reference.html#websocketresponse
aiohttp_settings – 需要传给 aiohttp Application 的参数。可用参数见 https://docs.aiohttp.org/en/stable/web_reference.html#application
剩余参数的详细说明见
pywebio.platform.tornado.start_server()
的同名参数。
其他¶
-
pywebio.platform.
seo
(title, description=None, app=None)[源代码]¶ 设置PyWebIO应用的SEO信息(在被搜索引擎索引时提供的网页信息)
- 参数
title (str) – 应用标题
description (str) – 应用简介
app (callable) – PyWebIO任务函数
除了使用
seo()
函数,PyWebIO默认会将任务函数的函数注释作为SEO信息:可以通过装饰器或直接调用的方式使用
seo()
@seo("title", "description") def foo(): pass def bar(): pass def hello(): """应用标题 应用简介… (应用简介和标题之间使用一个空行分隔) """ start_server([ foo, hello, seo("title", "description", bar), ])
1.1 新版功能.
-
pywebio.platform.
run_event_loop
(debug=False)[源代码]¶ 运行 asyncio 事件循环
See also: Integration coroutine-based session with Web framework
- 参数
debug – Set the debug mode of the event loop. See also: https://docs.python.org/3/library/asyncio-dev.html#asyncio-debug-mode