本文整理汇总了Python中aiohttp.web.TCPSite方法的典型用法代码示例。如果您正苦于以下问题:Python web.TCPSite方法的具体用法?Python web.TCPSite怎么用?Python web.TCPSite使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类aiohttp.web
的用法示例。
在下文中一共展示了web.TCPSite方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: start
# 需要导入模块: from aiohttp import web [as 别名]
# 或者: from aiohttp.web import TCPSite [as 别名]
def start(self):
app = web.Application(client_max_size=JSON_RPC_CLIENT_REQUEST_MAX_SIZE)
cors = aiohttp_cors.setup(app)
route = app.router.add_post("/", self.__handle)
cors.add(
route,
{
"*": aiohttp_cors.ResourceOptions(
allow_credentials=True,
expose_headers=("X-Custom-Server-Header",),
allow_methods=["POST", "PUT"],
allow_headers=("X-Requested-With", "Content-Type"),
)
},
)
self.runner = web.AppRunner(app, access_log=None)
self.loop.run_until_complete(self.runner.setup())
site = web.TCPSite(self.runner, self.host, self.port)
self.loop.run_until_complete(site.start())
示例2: start
# 需要导入模块: from aiohttp import web [as 别名]
# 或者: from aiohttp.web import TCPSite [as 别名]
def start(self) -> None:
"""
Start this transport.
Raises:
InboundTransportSetupError: If there was an error starting the webserver
"""
app = await self.make_application()
runner = web.AppRunner(app)
await runner.setup()
self.site = web.TCPSite(runner, host=self.host, port=self.port)
try:
await self.site.start()
except OSError:
raise InboundTransportSetupError(
"Unable to start websocket server with host "
+ f"'{self.host}' and port '{self.port}'\n"
)
示例3: start
# 需要导入模块: from aiohttp import web [as 别名]
# 或者: from aiohttp.web import TCPSite [as 别名]
def start(self) -> None:
"""
Start this transport.
Raises:
InboundTransportSetupError: If there was an error starting the webserver
"""
app = await self.make_application()
runner = web.AppRunner(app)
await runner.setup()
self.site = web.TCPSite(runner, host=self.host, port=self.port)
try:
await self.site.start()
except OSError:
raise InboundTransportSetupError(
"Unable to start webserver with host "
+ f"'{self.host}' and port '{self.port}'\n"
)
示例4: http_endpoint
# 需要导入模块: from aiohttp import web [as 别名]
# 或者: from aiohttp.web import TCPSite [as 别名]
def http_endpoint(config, suite):
"""Create http server task."""
async def handle(request):
"""aiohttp handle POST."""
response = []
with suite.reply(response.append):
await suite.handle(await request.read())
if response:
return web.Response(body=response.pop())
raise web.HTTPAccepted()
app = web.Application()
app.router.add_post('/', handle)
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, config['host'], config['port'])
server_task = asyncio.ensure_future(site.start())
yield
server_task.cancel()
with suppress(asyncio.CancelledError):
await server_task
await runner.cleanup()
示例5: run_server
# 需要导入模块: from aiohttp import web [as 别名]
# 或者: from aiohttp.web import TCPSite [as 别名]
def run_server():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
app = web.Application(debug=False)
app.add_routes([web.get('/', say_hello)])
app.add_routes([web.get('/401', four_hundred_one)])
app.add_routes([web.get('/500', five_hundred)])
app.add_routes([web.get('/exception', raise_exception)])
runner = web.AppRunner(app)
loop.run_until_complete(runner.setup())
site = web.TCPSite(runner, '127.0.0.1', testenv["aiohttp_port"])
loop.run_until_complete(site.start())
loop.run_forever()
示例6: start_site
# 需要导入模块: from aiohttp import web [as 别名]
# 或者: from aiohttp.web import TCPSite [as 别名]
def start_site(self, app, address='localhost', port=8080, usessl=False):
runner = web.AppRunner(app)
self.runners.append(runner)
await runner.setup()
if usessl:
ssl_ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
ssl_ctx.load_cert_chain(bumper.server_cert, bumper.server_key)
site = web.TCPSite(
runner,
host=address,
port=port,
ssl_context=ssl_ctx,
)
else:
site = web.TCPSite(
runner, host=address, port=port
)
await site.start()
示例7: test_confserver_exceptions
# 需要导入模块: from aiohttp import web [as 别名]
# 或者: from aiohttp.web import TCPSite [as 别名]
def test_confserver_exceptions():
with LogCapture() as l:
conf_server = bumper.ConfServer(("127.0.0.1", 8007), usessl=True)
conf_server.confserver_app()
conf_server.site = web.TCPSite
#bind permission
conf_server.site.start = mock.Mock(side_effect=OSError(1, "error while attempting to bind on address ('127.0.0.1', 8007): permission denied"))
await conf_server.start_server()
#asyncio Cancel
conf_server.site = web.TCPSite
conf_server.site.start = mock.Mock(side_effect=asyncio.CancelledError)
await conf_server.start_server()
#general exception
conf_server.site = web.TCPSite
conf_server.site.start = mock.Mock(side_effect=Exception(1, "general"))
await conf_server.start_server()
l.check_present(
("confserver", "ERROR", "error while attempting to bind on address ('127.0.0.1', 8007): permission denied")
)
示例8: start
# 需要导入模块: from aiohttp import web [as 别名]
# 或者: from aiohttp.web import TCPSite [as 别名]
def start(self):
if self.insecure:
self.site = web.TCPSite(
self.runner, host=self.addr, port=self.port
)
else:
ssl_context = ssl.create_default_context(
purpose=ssl.Purpose.SERVER_AUTH, cafile=self.cert
)
ssl_context.load_cert_chain(self.cert, self.key)
self.site = web.TCPSite(
self.runner,
host=self.addr,
port=self.port,
ssl_context=ssl_context,
)
await self.site.start()
self.port = self.site._server.sockets[0].getsockname()[1]
self.logger.info(f"Serving on {self.addr}:{self.port}")
示例9: start
# 需要导入模块: from aiohttp import web [as 别名]
# 或者: from aiohttp.web import TCPSite [as 别名]
def start(self):
app = web.Application()
app.add_routes([web.route('*', '/{tail:.*}', self.handle_request)])
aiohttp_jinja2.setup(
app, loader=jinja2.FileSystemLoader(self.dir)
)
middleware = SnareMiddleware(
error_404=self.meta['/status_404'].get('hash'),
headers=self.meta['/status_404'].get('headers', []),
server_header=self.run_args.server_header
)
middleware.setup_middlewares(app)
self.runner = web.AppRunner(app)
await self.runner.setup()
site = web.TCPSite(
self.runner,
self.run_args.host_ip,
self.run_args.port)
await site.start()
names = sorted(str(s.name) for s in self.runner.sites)
print("======== Running on {} ========\n"
"(Press CTRL+C to quit)".format(', '.join(names)))
示例10: start
# 需要导入模块: from aiohttp import web [as 别名]
# 或者: from aiohttp.web import TCPSite [as 别名]
def start(self, host: str = "127.0.0.1", port: int = 8080) -> None:
connector = None
self.log.debug(f"Starting appservice web server on {host}:{port}")
if self.server.startswith("https://") and not self.verify_ssl:
connector = aiohttp.TCPConnector(verify_ssl=False)
self._http_session = aiohttp.ClientSession(loop=self.loop, connector=connector)
self._intent = AppServiceAPI(base_url=self.server, bot_mxid=self.bot_mxid, log=self.log,
token=self.as_token, state_store=self.state_store,
real_user_content_key=self.real_user_content_key,
client_session=self._http_session).bot_intent()
ssl_ctx = None
if self.tls_cert and self.tls_key:
ssl_ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
ssl_ctx.load_cert_chain(self.tls_cert, self.tls_key)
self.runner = web.AppRunner(self.app)
await self.runner.setup()
site = web.TCPSite(self.runner, host, port, ssl_context=ssl_ctx)
await site.start()
示例11: webhook
# 需要导入模块: from aiohttp import web [as 别名]
# 或者: from aiohttp.web import TCPSite [as 别名]
def webhook(self):
async def vote_handler(request):
req_auth = request.headers.get('Authorization')
if self.webhook_auth == req_auth:
data = await request.json()
if data.get('type') == 'upvote':
event_name = 'dbl_vote'
elif data.get('type') == 'test':
event_name = 'dbl_test'
self.bot.dispatch(event_name, data)
return web.Response()
else:
return web.Response(status=401)
app = web.Application(loop=self.loop)
app.router.add_post(self.webhook_path, vote_handler)
runner = web.AppRunner(app)
await runner.setup()
self._webserver = web.TCPSite(runner, '0.0.0.0', self.webhook_port)
await self._webserver.start()
示例12: listening_endpoint
# 需要导入模块: from aiohttp import web [as 别名]
# 或者: from aiohttp.web import TCPSite [as 别名]
def listening_endpoint(connection, unused_tcp_port):
"""Create http server task."""
async def handle(request):
"""aiohttp handle POST."""
await connection.handle(await request.read())
raise web.HTTPAccepted()
app = web.Application()
app.router.add_post('/', handle)
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, 'localhost', unused_tcp_port)
server_task = asyncio.ensure_future(site.start())
yield 'http://localhost:{}'.format(unused_tcp_port)
server_task.cancel()
with suppress(asyncio.CancelledError):
await server_task
await runner.cleanup()
示例13: test_startup_initializes_an_web_application
# 需要导入模块: from aiohttp import web [as 别名]
# 或者: from aiohttp.web import TCPSite [as 别名]
def test_startup_initializes_an_web_application(self, start):
self.app.routes_registry = self.routes_registry
await self.signal_handler.startup(self.app)
self.assertIsInstance(self.app[RouteTypes.HTTP]["app"], web.Application)
self.assertIsInstance(
self.app[RouteTypes.HTTP]["runner"], web.AppRunner
)
site: web.TCPSite = self.app[RouteTypes.HTTP]["site"]
self.assertIsInstance(site, web.TCPSite)
self.assertEqual(site._port, settings.HTTP_PORT)
self.assertEqual(site._host, settings.HTTP_HOST)
start.assert_awaited_once()
示例14: start
# 需要导入模块: from aiohttp import web [as 别名]
# 或者: from aiohttp.web import TCPSite [as 别名]
def start(self):
self.runner = web.AppRunner(self.app)
await self.runner.setup()
self.port = os.environ.get("PORT", 8080)
site = web.TCPSite(self.runner, None, self.port)
await site.start()
self.running.set()
示例15: start_app_runner
# 需要导入模块: from aiohttp import web [as 别名]
# 或者: from aiohttp.web import TCPSite [as 别名]
def start_app_runner(runner, address, port):
await runner.setup()
site = web.TCPSite(runner, address, port)
await site.start()