python-3.x - 在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
解决方案
推荐阅读
- php - Laravel 包含关系的关系
- javascript - div 显示在页面上,除非按下按钮,否则不应显示
- r - Valgrind/R 不工作:“致命错误:无法创建 'R_TempDir'”
- c# - 加快 N(P) 问题搜索 - 和方问题
- django - 扩展base.html时来自多个应用程序的Django静态CSS
- c# - identityReference.Translate(typeof(System.Security.Principal.NTAccount)).ToString() 未提供完整的域名
- javascript - $(...).Jcrop 不是函数
- python - 检查python中的平衡括号
- reactjs - slideToShow 未呈现指定数量的列
- javascript - 如何使链接适合其内部的组件?