pywebio.platform
— Run and integrate with Web framework¶
platform
模块为PyWebIO提供了对不同Web框架的支持。
Tornado相关¶
-
pywebio.platform.
start_server
(target, port=0, host='', debug=False, 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 将
target
任务函数作为Web服务提供。- 参数
target – 任务函数。任务函数为协程函数时,使用 基于协程的会话实现 ;任务函数为普通函数时,使用基于线程的会话实现。
allowed_origins (list) – 除当前域名外,服务器还允许的请求的来源列表。
port (int) – server bind port. set
0
to find a free port number to usehost (str) – server bind host.
host
may be either an IP address or hostname. If it’s a hostname, the server will listen on all IP addresses associated with the name. set empty string or to listen on all available interfaces.debug (bool) – Tornado debug mode
allowed_origins –
除当前域名外,服务器还允许的请求的来源列表。 来源包含协议和域名和端口部分,允许使用 Unix shell 风格的匹配模式:
*
为通配符?
匹配单个字符[seq]
匹配seq内的字符[!seq]
匹配不在seq内的字符
比如
https://*.example.com
、*://*.example.com
check_origin (callable) – 请求来源检查函数。接收请求来源(包含协议和域名和端口部分)字符串, 返回
True/False
。若设置了check_origin
,allowed_origins
参数将被忽略auto_open_webbrowser (bool) – Whether or not auto open web browser when server is started (if the operating system allows it) .
websocket_max_message_size (int) – Max bytes of a message which Tornado can accept. Messages larger than the
websocket_max_message_size
(default 10MiB) will not be accepted.websocket_ping_interval (int) – If set to a number, all websockets will be pinged every n seconds. This can help keep the connection alive through certain proxy servers which close idle connections, and it can detect if the websocket has failed without being properly closed.
websocket_ping_timeout (int) – If the ping interval is set, and the server doesn’t receive a ‘pong’ in this many seconds, it will close the websocket. The default is three times the ping interval, with a minimum of 30 seconds. Ignored if
websocket_ping_interval
is not set.tornado_app_settings – Additional keyword arguments passed to the constructor of
tornado.web.Application
. ref: https://www.tornadoweb.org/en/stable/web.html#tornado.web.Application.settings
-
pywebio.platform.tornado.
webio_handler
(target, allowed_origins=None, check_origin=None)[源代码]¶ 获取在Tornado中运行PyWebIO任务的RequestHandle类。RequestHandle类基于WebSocket协议与浏览器进行通讯。
- 参数
target – 任务函数。任务函数为协程函数时,使用 基于协程的会话实现 ;任务函数为普通函数时,使用基于线程的会话实现。
allowed_origins (list) –
除当前域名外,服务器还允许的请求的来源列表。 来源包含协议和域名和端口部分,允许使用 Unix shell 风格的匹配模式:
*
为通配符?
匹配单个字符[seq]
匹配seq内的字符[!seq]
匹配不在seq内的字符
比如
https://*.example.com
、*://*.example.com
、check_origin (callable) – 请求来源检查函数。接收请求来源(包含协议和域名和端口部分)字符串, 返回
True/False
。若设置了check_origin
,allowed_origins
参数将被忽略
- 返回
Tornado RequestHandle类
Flask相关¶
-
pywebio.platform.flask.
webio_view
(target, session_expire_seconds=None, session_cleanup_interval=None, allowed_origins=None, check_origin=None)[源代码]¶ 获取在Flask中运行PyWebIO任务的视图函数。基于http请求与前端进行通讯
- 参数
target – 任务函数。任务函数为协程函数时,使用 基于协程的会话实现 ;任务函数为普通函数时,使用基于线程的会话实现。
session_expire_seconds (int) – 会话不活跃过期时间。
session_cleanup_interval (int) – 会话清理间隔。
allowed_origins (list) –
除当前域名外,服务器还允许的请求的来源列表。 来源包含协议和域名和端口部分,允许使用 Unix shell 风格的匹配模式:
*
为通配符?
匹配单个字符[seq]
匹配seq内的字符[!seq]
匹配不在seq内的字符
比如
https://*.example.com
、*://*.example.com
check_origin (callable) – 请求来源检查函数。接收请求来源(包含协议和域名和端口部分)字符串, 返回
True/False
。若设置了check_origin
,allowed_origins
参数将被忽略
- 返回
Flask视图函数
-
pywebio.platform.flask.
start_server
(target, port=8080, host='localhost', allowed_origins=None, check_origin=None, disable_asyncio=False, session_cleanup_interval=None, session_expire_seconds=None, debug=False, **flask_options)[源代码]¶ 启动一个 Flask server 将
target
任务函数作为Web服务提供。- 参数
target – 任务函数。任务函数为协程函数时,使用 基于协程的会话实现 ;任务函数为普通函数时,使用基于线程的会话实现。
port (int) – server bind port. set
0
to find a free port number to usehost (str) – server bind host.
host
may be either an IP address or hostname. If it’s a hostname,allowed_origins (list) –
除当前域名外,服务器还允许的请求的来源列表。 来源包含协议和域名和端口部分,允许使用 Unix shell 风格的匹配模式:
*
为通配符?
匹配单个字符[seq]
匹配seq内的字符[!seq]
匹配不在seq内的字符
比如
https://*.example.com
、*://*.example.com
check_origin (callable) – 请求来源检查函数。接收请求来源(包含协议和域名和端口部分)字符串, 返回
True/False
。若设置了check_origin
,allowed_origins
参数将被忽略disable_asyncio (bool) –
禁用 asyncio 函数。仅在
target
为协程函数时有效。注解
实现说明: 当使用Flask backend时,若要在PyWebIO的会话中使用
asyncio
标准库里的协程函数,PyWebIO需要单独开启一个线程来运行asyncio
事件循环, 若程序中没有使用到asyncio
中的异步函数,可以开启此选项来避免不必要的资源浪费session_expire_seconds (int) – 会话过期时间。若 session_expire_seconds 秒内没有收到客户端的请求,则认为会话过期。
session_cleanup_interval (int) – 会话清理间隔。
debug (bool) – Flask debug mode
flask_options – Additional keyword arguments passed to the constructor of
flask.Flask.run
. ref: https://flask.palletsprojects.com/en/1.1.x/api/?highlight=flask%20run#flask.Flask.run
Django相关¶
-
pywebio.platform.django.
webio_view
(target, session_expire_seconds=None, session_cleanup_interval=None, allowed_origins=None, check_origin=None)[源代码]¶ 获取在django中运行PyWebIO任务的视图函数。 基于http请求与前端进行通讯
- 参数
target – 任务函数。任务函数为协程函数时,使用 基于协程的会话实现 ;任务函数为普通函数时,使用基于线程的会话实现。
session_expire_seconds (int) – 会话不活跃过期时间。
session_cleanup_interval (int) – 会话清理间隔。
allowed_origins (list) –
除当前域名外,服务器还允许的请求的来源列表。 来源包含协议和域名和端口部分,允许使用 Unix shell 风格的匹配模式:
*
为通配符?
匹配单个字符[seq]
匹配seq内的字符[!seq]
匹配不在seq内的字符
比如
https://*.example.com
、*://*.example.com
check_origin (callable) – 请求来源检查函数。接收请求来源(包含协议和域名和端口部分)字符串, 返回
True/False
。若设置了check_origin
,allowed_origins
参数将被忽略
- 返回
Django视图函数
-
pywebio.platform.django.
start_server
(target, port=8080, host='localhost', allowed_origins=None, check_origin=None, disable_asyncio=False, session_cleanup_interval=None, session_expire_seconds=None, debug=False, **django_options)[源代码]¶ 启动一个 Django server 将
target
任务函数作为Web服务提供。- 参数
target – 任务函数。任务函数为协程函数时,使用 基于协程的会话实现 ;任务函数为普通函数时,使用基于线程的会话实现。
port (int) – server bind port. set
0
to find a free port number to usehost (str) – server bind host.
host
may be either an IP address or hostname. If it’s a hostname,allowed_origins (list) –
除当前域名外,服务器还允许的请求的来源列表。 来源包含协议和域名和端口部分,允许使用 Unix shell 风格的匹配模式:
*
为通配符?
匹配单个字符[seq]
匹配seq内的字符[!seq]
匹配不在seq内的字符
比如
https://*.example.com
、*://*.example.com
check_origin (callable) – 请求来源检查函数。接收请求来源(包含协议和域名和端口部分)字符串, 返回
True/False
。若设置了check_origin
,allowed_origins
参数将被忽略disable_asyncio (bool) –
禁用 asyncio 函数。仅在
target
为协程函数时有效。注解
实现说明: 当使用Django backend时,若要在PyWebIO的会话中使用
asyncio
标准库里的协程函数,PyWebIO需要单独开启一个线程来运行asyncio
事件循环, 若程序中没有使用到asyncio
中的异步函数,可以开启此选项来避免不必要的资源浪费session_expire_seconds (int) – 会话过期时间。若 session_expire_seconds 秒内没有收到客户端的请求,则认为会话过期。
session_cleanup_interval (int) – 会话清理间隔。
debug (bool) – 开启 Django debug mode 和一般访问日志的记录
django_options – django应用的其他设置,见 https://docs.djangoproject.com/en/3.0/ref/settings/ . 其中
DEBUG
、ALLOWED_HOSTS
、ROOT_URLCONF
、SECRET_KEY
被PyWebIO设置,不可以手动指定
aiohttp相关¶
-
pywebio.platform.aiohttp.
webio_handler
(target, allowed_origins=None, check_origin=None, websocket_settings=None)[源代码]¶ 获取在aiohttp中运行PyWebIO任务函数的 Request Handle 协程。 Request Handle基于WebSocket协议与浏览器进行通讯。
- 参数
target – 任务函数。任务函数为协程函数时,使用 基于协程的会话实现 ;任务函数为普通函数时,使用基于线程的会话实现。
allowed_origins (list) –
除当前域名外,服务器还允许的请求的来源列表。 来源包含协议和域名和端口部分,允许使用 Unix shell 风格的匹配模式:
*
为通配符?
匹配单个字符[seq]
匹配seq内的字符[!seq]
匹配不在seq内的字符
比如
https://*.example.com
、*://*.example.com
、check_origin (callable) – 请求来源检查函数。接收请求来源(包含协议和域名和端口部分)字符串, 返回
True/False
。若设置了check_origin
,allowed_origins
参数将被忽略websocket_settings (dict) – 创建 aiohttp WebSocketResponse 时使用的参数。见 https://docs.aiohttp.org/en/stable/web_reference.html#websocketresponse
- 返回
aiohttp Request Handler
-
pywebio.platform.aiohttp.
start_server
(target, port=0, host='', debug=False, allowed_origins=None, check_origin=None, auto_open_webbrowser=False, websocket_settings=None, **aiohttp_settings)[源代码]¶ 启动一个 aiohttp server 将
target
任务函数作为Web服务提供。- 参数
target – 任务函数。任务函数为协程函数时,使用 基于协程的会话实现 ;任务函数为普通函数时,使用基于线程的会话实现。
allowed_origins (list) – 除当前域名外,服务器还允许的请求的来源列表。
port (int) – server bind port. set
0
to find a free port number to usehost (str) – server bind host.
host
may be either an IP address or hostname. If it’s a hostname, the server will listen on all IP addresses associated with the name. set empty string or to listen on all available interfaces.debug (bool) – asyncio Debug Mode
allowed_origins –
除当前域名外,服务器还允许的请求的来源列表。 来源包含协议和域名和端口部分,允许使用 Unix shell 风格的匹配模式:
*
为通配符?
匹配单个字符[seq]
匹配seq内的字符[!seq]
匹配不在seq内的字符
比如
https://*.example.com
、*://*.example.com
check_origin (callable) – 请求来源检查函数。接收请求来源(包含协议和域名和端口部分)字符串, 返回
True/False
。若设置了check_origin
,allowed_origins
参数将被忽略auto_open_webbrowser (bool) – Whether or not auto open web browser when server is started (if the operating system allows it) .
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.httpbased.
run_event_loop
(debug=False)[源代码]¶ 运行事件循环
基于协程的会话在启动基于线程的http服务器之前需要启动一个单独的线程来运行事件循环。
- 参数
debug – Set the debug mode of the event loop. See also: https://docs.python.org/3/library/asyncio-dev.html#asyncio-debug-mode