简体   繁体   English

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

[英]How to fix [Errno 111] Connection refused

I am setting up an oauth2 server using the authlib (0.11) library.我正在使用 authlib (0.11) 库设置 oauth2 服务器。 I already created the Oauth2Session and received the authorization code grant.我已经创建了 Oauth2Session 并收到了授权代码授权。 Now I have to fetch_access_token(...), but I am receiving a [Errno 111] Connection refused while trying to communicate with the access_token_url.现在我必须 fetch_access_token(...),但我在尝试与 access_token_url 通信时收到 [Errno 111] 连接被拒绝。 I am using docker compose, and have linked both with a bridge.我正在使用 docker compose,并已将两者与桥连接。 I am also using port mapping to access the localhost from the host.我还使用端口映射从主机访问本地主机。 (macOS) (苹果系统)

I already tried netcat on the localhost and the specific link and sending GET / HTTP/1.1.我已经在本地主机和特定链接上尝试过 netcat 并发送 GET / HTTP/1.1。 It worked.有效。 I have already checked port forwarding and it is set to true (=1) I tried setting the name of the host link (127.0.0.1) as the docker compose name (backend)我已经检查了端口转发并将其设置为 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'))

The answer is:答案是:

use the name of the container, in my case backend, and then use the original port not the exposed one, in my case 5000.使用容器的名称,在我的情况下为后端,然后使用原始端口而不是暴露的端口,在我的情况下为 5000。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM