首页 > 解决方案 > 在heroku上托管python websockets

问题描述

我正在尝试在 heroku 上托管我的 websocket 应用程序。我目前正在我的 app.py 文件中启动我的服务器,如下所示:

def startme():
    start_server = websockets.serve(main_connect,'',os.environ.get('PORT'))
    asyncio.get_event_loop().run_until_complete(start_server)
    asyncio.get_event_loop().run_forever()

我的 Procfile 看起来像这样:

web: gunicorn app:startme

查看日志,它启动正常,显示它正在运行的端口,如下所示:

[2021-04-30 23:37:06 +0000] [4] [INFO] Listening at: http://0.0.0.0:35403 (4)

但是当我尝试连接它时,就像这样:

uri = "wss://myapp.herokuapp.com/0.0.0.0:35403"
async with websockets.connect(uri) as websocket: #connects

我只是在服务器上收到此错误:

2021-04-30T23:37:16.141358+00:00 app[web.1]: [2021-04-30 23:37:16 +0000] [7] [ERROR] Socket error processing request.
2021-04-30T23:37:16.141400+00:00 app[web.1]: Traceback (most recent call last):
2021-04-30T23:37:16.141402+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/workers/sync.py", line 135, in handle
2021-04-30T23:37:16.141403+00:00 app[web.1]: self.handle_request(listener, req, client, addr)
2021-04-30T23:37:16.141403+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/workers/sync.py", line 191, in handle_request
2021-04-30T23:37:16.141404+00:00 app[web.1]: six.reraise(*sys.exc_info())
2021-04-30T23:37:16.141418+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/six.py", line 625, in reraise
2021-04-30T23:37:16.141420+00:00 app[web.1]: raise value
2021-04-30T23:37:16.141420+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/workers/sync.py", line 176, in handle_request
2021-04-30T23:37:16.141421+00:00 app[web.1]: respiter = self.wsgi(environ, resp.start_response)
2021-04-30T23:37:16.141421+00:00 app[web.1]: File "/app/app.py", line 121, in startme
2021-04-30T23:37:16.141422+00:00 app[web.1]: asyncio.get_event_loop().run_until_complete(start_server)
2021-04-30T23:37:16.141422+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
2021-04-30T23:37:16.141423+00:00 app[web.1]: return future.result()
2021-04-30T23:37:16.141424+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/asyncio/tasks.py", line 690, in _wrap_awaitable
2021-04-30T23:37:16.141424+00:00 app[web.1]: return (yield from awaitable.__await__())
2021-04-30T23:37:16.141425+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/websockets/server.py", line 965, in __await_impl__
2021-04-30T23:37:16.141425+00:00 app[web.1]: server = await self._create_server()
2021-04-30T23:37:16.141425+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/asyncio/base_events.py", line 1494, in create_server
2021-04-30T23:37:16.141426+00:00 app[web.1]: raise OSError(err.errno, 'error while attempting '
2021-04-30T23:37:16.141426+00:00 app[web.1]: OSError: [Errno 98] error while attempting to bind on address ('0.0.0.0', 35403): address already in use
   

标签: python-3.xherokuwebsocket

解决方案


推荐阅读