当我使用 时aiohttp.web.run_app(. . ., port=0),我假设它选择了一个任意可用的端口来提供服务。这样对吗?如果是这样,有没有办法弄清楚它选择了哪个端口?
您可以使用server.sockets以下代码:
@asyncio.coroutine
def status(request):
"""Check that the app is properly working"""
return web.json_response('OK')
app = web.Application() # pylint: disable=invalid-name
app.router.add_get('/api/status', status)
def main():
"""Starts the aiohttp process to serve the REST API"""
loop = asyncio.get_event_loop()
# continue server bootstraping
handler = app.make_handler()
coroutine = loop.create_server(handler, '0.0.0.0', 0)
server = loop.run_until_complete(coroutine)
print('Serving on http://%s:%s' % server.sockets[0].getsockname()) # HERE!
try:
loop.run_forever()
except KeyboardInterrupt:
pass
finally:
server.close()
loop.run_until_complete(server.wait_closed())
loop.run_until_complete(handler.finish_connections(1.0))
loop.close()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
740 次 |
| 最近记录: |