aiohttp.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import asyncio
  2. import fnmatch
  3. import json
  4. import logging
  5. from functools import partial
  6. from os import path, listdir
  7. from urllib.parse import urlparse
  8. from aiohttp import web
  9. from .tornado import open_webbrowser_on_server_started
  10. from .utils import make_applications
  11. from ..session import CoroutineBasedSession, ThreadBasedSession, register_session_implement_for_target, Session
  12. from ..session.base import get_session_info_from_headers
  13. from ..utils import get_free_port, STATIC_PATH, iscoroutinefunction, isgeneratorfunction
  14. logger = logging.getLogger(__name__)
  15. def _check_origin(origin, allowed_origins, host):
  16. if _is_same_site(origin, host):
  17. return True
  18. return any(
  19. fnmatch.fnmatch(origin, patten)
  20. for patten in allowed_origins
  21. )
  22. def _is_same_site(origin, host):
  23. """判断 origin 和 host 是否一致。origin 和 host 都为http协议请求头"""
  24. parsed_origin = urlparse(origin)
  25. origin = parsed_origin.netloc
  26. origin = origin.lower()
  27. # Check to see that origin matches host directly, including ports
  28. return origin == host
  29. def _webio_handler(applications, websocket_settings, check_origin_func=_is_same_site):
  30. """获取用于Tornado进行整合的RequestHandle类
  31. :param dict applications: 任务名->任务函数 的映射
  32. :param callable check_origin_func: check_origin_func(origin, handler) -> bool
  33. :return: Tornado RequestHandle类
  34. """
  35. ioloop = asyncio.get_event_loop()
  36. async def wshandle(request: web.Request):
  37. origin = request.headers.get('origin')
  38. if origin and not check_origin_func(origin=origin, host=request.host):
  39. return web.Response(status=403, text="Cross origin websockets not allowed")
  40. ws = web.WebSocketResponse(**websocket_settings)
  41. await ws.prepare(request)
  42. close_from_session_tag = False # 是否由session主动关闭连接
  43. def send_msg_to_client(session: Session):
  44. for msg in session.get_task_commands():
  45. msg_str = json.dumps(msg)
  46. ioloop.create_task(ws.send_str(msg_str))
  47. def close_from_session():
  48. nonlocal close_from_session_tag
  49. close_from_session_tag = True
  50. ioloop.create_task(ws.close())
  51. logger.debug("WebSocket closed from session")
  52. session_info = get_session_info_from_headers(request.headers)
  53. session_info['user_ip'] = request.remote
  54. session_info['request'] = request
  55. session_info['backend'] = 'aiohttp'
  56. app_name = request.query.getone('app', 'index')
  57. application = applications.get(app_name) or applications['index']
  58. if iscoroutinefunction(application) or isgeneratorfunction(application):
  59. session = CoroutineBasedSession(application, session_info=session_info,
  60. on_task_command=send_msg_to_client,
  61. on_session_close=close_from_session)
  62. else:
  63. session = ThreadBasedSession(application, session_info=session_info,
  64. on_task_command=send_msg_to_client,
  65. on_session_close=close_from_session, loop=ioloop)
  66. async for msg in ws:
  67. if msg.type == web.WSMsgType.text:
  68. data = msg.json()
  69. if data is not None:
  70. session.send_client_event(data)
  71. elif msg.type == web.WSMsgType.binary:
  72. pass
  73. elif msg.type == web.WSMsgType.close:
  74. if not close_from_session_tag:
  75. session.close()
  76. logger.debug("WebSocket closed from client")
  77. return ws
  78. return wshandle
  79. def webio_handler(applications, allowed_origins=None, check_origin=None, websocket_settings=None):
  80. """获取在aiohttp中运行PyWebIO任务函数的 `Request Handle <https://docs.aiohttp.org/en/stable/web_quickstart.html#aiohttp-web-handler>`_ 协程。
  81. Request Handle基于WebSocket协议与浏览器进行通讯。
  82. :param list/dict/callable applications: PyWebIO应用. 可以是任务函数或者任务函数的字典或列表。
  83. :param list allowed_origins: 除当前域名外,服务器还允许的请求的来源列表。
  84. 来源包含协议和域名和端口部分,允许使用 Unix shell 风格的匹配模式:
  85. - ``*`` 为通配符
  86. - ``?`` 匹配单个字符
  87. - ``[seq]`` 匹配seq内的字符
  88. - ``[!seq]`` 匹配不在seq内的字符
  89. 比如 ``https://*.example.com`` 、 ``*://*.example.com`` 、
  90. :param callable check_origin: 请求来源检查函数。接收请求来源(包含协议和域名和端口部分)字符串,
  91. 返回 ``True/False`` 。若设置了 ``check_origin`` , ``allowed_origins`` 参数将被忽略
  92. :param dict websocket_settings: 创建 aiohttp WebSocketResponse 时使用的参数。见 https://docs.aiohttp.org/en/stable/web_reference.html#websocketresponse
  93. :return: aiohttp Request Handler
  94. """
  95. applications = make_applications(applications)
  96. for target in applications.values():
  97. register_session_implement_for_target(target)
  98. websocket_settings = websocket_settings or {}
  99. if check_origin is None:
  100. check_origin_func = partial(_check_origin, allowed_origins=allowed_origins or [])
  101. else:
  102. check_origin_func = lambda origin, handler: _is_same_site(origin, handler) or check_origin(origin)
  103. return _webio_handler(applications=applications,
  104. check_origin_func=check_origin_func,
  105. websocket_settings=websocket_settings)
  106. def static_routes(static_path):
  107. """获取用于提供PyWebIO静态文件的aiohttp路由"""
  108. async def index(request):
  109. return web.FileResponse(path.join(STATIC_PATH, 'index.html'))
  110. files = [path.join(static_path, d) for d in listdir(static_path)]
  111. dirs = filter(path.isdir, files)
  112. routes = [web.static('/' + path.basename(d), d) for d in dirs]
  113. routes.append(web.get('/', index))
  114. return routes
  115. def start_server(applications, port=0, host='', debug=False,
  116. allowed_origins=None, check_origin=None,
  117. auto_open_webbrowser=False,
  118. websocket_settings=None,
  119. **aiohttp_settings):
  120. """启动一个 aiohttp server 将 ``target`` 任务函数作为Web服务提供。
  121. :param list/dict/callable applications: PyWebIO应用. 可以是任务函数或者任务函数的字典或列表。
  122. :param list allowed_origins: 除当前域名外,服务器还允许的请求的来源列表。
  123. :param int port: server bind port. set ``0`` to find a free port number to use
  124. :param str host: server bind host. ``host`` may be either an IP address or hostname. If it's a hostname,
  125. the server will listen on all IP addresses associated with the name.
  126. set empty string or to listen on all available interfaces.
  127. :param bool debug: asyncio Debug Mode
  128. :param list allowed_origins: 除当前域名外,服务器还允许的请求的来源列表。
  129. 来源包含协议和域名和端口部分,允许使用 Unix shell 风格的匹配模式:
  130. - ``*`` 为通配符
  131. - ``?`` 匹配单个字符
  132. - ``[seq]`` 匹配seq内的字符
  133. - ``[!seq]`` 匹配不在seq内的字符
  134. 比如 ``https://*.example.com`` 、 ``*://*.example.com``
  135. :param callable check_origin: 请求来源检查函数。接收请求来源(包含协议和域名和端口部分)字符串,
  136. 返回 ``True/False`` 。若设置了 ``check_origin`` , ``allowed_origins`` 参数将被忽略
  137. :param bool auto_open_webbrowser: Whether or not auto open web browser when server is started (if the operating system allows it) .
  138. :param dict websocket_settings: 创建 aiohttp WebSocketResponse 时使用的参数。见 https://docs.aiohttp.org/en/stable/web_reference.html#websocketresponse
  139. :param aiohttp_settings: 需要传给 aiohttp Application 的参数。可用参数见 https://docs.aiohttp.org/en/stable/web_reference.html#application
  140. """
  141. kwargs = locals()
  142. if not host:
  143. host = '0.0.0.0'
  144. if port == 0:
  145. port = get_free_port()
  146. handler = webio_handler(applications, allowed_origins=allowed_origins, check_origin=check_origin,
  147. websocket_settings=websocket_settings)
  148. app = web.Application(**aiohttp_settings)
  149. app.add_routes([web.get('/io', handler)])
  150. app.add_routes(static_routes(STATIC_PATH))
  151. if auto_open_webbrowser:
  152. asyncio.get_event_loop().create_task(open_webbrowser_on_server_started('localhost', port))
  153. if debug:
  154. logging.getLogger("asyncio").setLevel(logging.DEBUG)
  155. print('Listen on %s:%s' % (host, port))
  156. web.run_app(app, host=host, port=port)