静态构建的python代码无法解析DNS名称

Jua*_*arg 5 python dns urllib urllib3 python-3.x

我正在尝试将代码部署到我没有 root 访问权限的服务器。所以我想的解决方案是使用pyinstallerstaticx进行部署。

我的代码在 python 3.7 中运行,简而言之,执行以下操作:

import requests
response = requests.get('http://example.com/api/action')
# Do something with the response
Run Code Online (Sandbox Code Playgroud)

当我在我的环境中运行它时,即使在使用pyinstaller和“构建”之后staticx,它也能完美运行。

但是,当我尝试部署它时(服务器运行的是 Red Hat Enterprise Linux Server 7.4 版,它没有 python 3),我得到:

Traceback (most recent call last):
  File "site-packages/urllib3/connection.py", line 160, in _new_conn
  File "site-packages/urllib3/util/connection.py", line 57, in create_connection
  File "socket.py", line 748, in getaddrinfo
socket.gaierror: [Errno -2] Name or service not known

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "site-packages/urllib3/connectionpool.py", line 603, in urlopen
  File "site-packages/urllib3/connectionpool.py", line 355, in _make_request
  File "http/client.py", line 1229, in request
  File "http/client.py", line 1275, in _send_request
  File "http/client.py", line 1224, in endheaders
  File "http/client.py", line 1016, in _send_output
  File "http/client.py", line 956, in send
  File "site-packages/urllib3/connection.py", line 183, in connect
  File "site-packages/urllib3/connection.py", line 169, in _new_conn
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7f912ca96f28>: Failed to establish a new connection: [Errno -2] Name or service not known

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "site-packages/requests/adapters.py", line 449, in send
  File "site-packages/urllib3/connectionpool.py", line 641, in urlopen
  File "site-packages/urllib3/util/retry.py", line 399, in increment
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='someserver.com', port=80): Max retries exceeded with url: /api/action (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f912ca96f28>: Failed to establish a new connection: [Errno -2] Name or service not known'))
Run Code Online (Sandbox Code Playgroud)

如果我使用 IP 地址而不是域,则一切正常。不幸的是,这是不可能的,因为我必须访问使用主机的代理。此外,IP 地址将来可能会更改。

如果我使用nslookup或,服务器可以很好地解析名称ping

任何想法为什么会发生这种情况?关于如何以在远程服务器中工作的方式部署我的代码的任何替代方案也可能是一个有效的答案。但请注意:

  • 我没有 root 访问权限并请求安装 python3、任何库或任何其他包(例如 docker 等)很可能会被我的客户拒绝
  • 服务器可以解析外部名称,例如google.com但不能访问外部互联网(代码可以在内部服务器上工作,具有内部 DNS,例如someserver.mycustomer.example