gpt4 book ai didi

python - 如何修复[Errno 111]连接被拒绝

转载 作者:行者123 更新时间:2023-12-03 08:54:29 28 4
gpt4 key购买 nike

我正在使用 authlib (0.11) 库设置 oauth2 服务器。我已经创建了 Oauth2Session 并收到了授权代码授予。现在我必须 fetch_access_token(...),但在尝试与 access_token_url 通信时收到 [Errno 111] 连接被拒绝。我正在使用 docker compose,并通过桥将两者链接起来。我还使用端口映射从主机访问本地主机。 (macOS)

我已经在本地主机上尝试了 netcat 和特定链接并发送 GET/HTTP/1.1。有效。我已经检查过端口转发并将其设置为 true (=1)我尝试将主机链接的名称(127.0.0.1)设置为docker compose名称(后端)

access_token_url = 'http://127.0.0.1:50000/oauth/token'
# tried 'http://backend:50000/oauth/token', 'http://localhost:50000/oauth/token'
token = sessionOauth.fetch_access_token(access_token_url, authorization_response=authorization_response)
# the part that concerns the question
backend:
build: .

ports:
- "50000:5000"
environment:
- AUTHLIB_INSECURE_TRANSPORT=true
- PYTHONUNBUFFERED=1
networks:
- db
- front_back

vuejs:
build: ../x/
ports:
- 8080:8080
networks:
- front_back
networks:
main:
driver: bridge
db:
driver: bridge
front_back:
driver: bridge
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2311, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1834, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1737, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 36, in reraise
raise value
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1832, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1818, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/usr/local/lib/python3.7/site-packages/flask_cors/decorator.py", line 128, in wrapped_function
resp = make_response(f(*args, **kwargs))
File "/var/app/app/routes.py", line 325, in testing3
token = sessionOauth.fetch_access_token(access_token_url, authorization_response=authorization_response)
File "/usr/local/lib/python3.7/site-packages/authlib/client/oauth2_session.py", line 102, in fetch_access_token
return self.fetch_token(url, **kwargs)
File "/usr/local/lib/python3.7/site-packages/authlib/oauth2/client.py", line 177, in fetch_token
headers=headers, **session_kwargs
File "/usr/local/lib/python3.7/site-packages/authlib/oauth2/client.py", line 185, in _fetch_token
auth=auth, **kwargs)
File "/usr/local/lib/python3.7/site-packages/requests/sessions.py", line 581, in post
return self.request('POST', url, data=data, json=json, **kwargs)
File "/usr/local/lib/python3.7/site-packages/authlib/client/oauth2_session.py", line 110, in request
method, url, auth=auth, **kwargs)
File "/usr/local/lib/python3.7/site-packages/requests/sessions.py", line 533, in request
resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python3.7/site-packages/requests/sessions.py", line 646, in send
r = adapter.send(request, **kwargs)
File "/usr/local/lib/python3.7/site-packages/requests/adapters.py", line 516, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='127.0.0.1', port=50000): Max retries exceeded with url: /oauth/token (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fc872466198>: Failed to establish a new connection: [Errno 111] Connection refused'))

最佳答案

答案是:

使用容器的名称(在我的例子中为后端),然后使用原始端口而不是暴露的端口(在我的例子中为 5000)。

access_token_url = 'http://backend:5000/oauth/token'

关于python - 如何修复[Errno 111]连接被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56699866/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com