本地运行项目成功 ,但在服务器运行程序就会报Failed to establish a new connection: [Errno -2] Name or service not known

时间:2024-09-09 11:05:20
equests.exceptions.ConnectionError: HTTPSConnectionPool(host='appapi.xxxx.com', port=): Max retries exceeded with url: /appapi/exchange//v1/prolist (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7fca889818d0>: Failed to establish a new connection: [Errno -2] Name or service not known',))

排查request连接 错误 ,说明是和网络 相关的,

step1:先在服务器端ping 一下测试网址的host

(automationVenv) [root@ automationTest]# ping appapi.xxx.com
PING appapi.5i5j.com.wswebpic.com (103.254.190.254) () bytes of data.
bytes from 103.254.190.254 (xxx.254.190.): icmp_seq= ttl= time=2.93 ms
bytes from 103.254.190.254 (xxx.254.190.): icmp_seq= ttl= time=2.56 ms
bytes from 103.254.190.254 (xxx.254.190.): icmp_seq= ttl= time=4.97 ms
bytes from 103.254.190.254 (xxx.254.190.): icmp_seq= ttl= time=3.07 ms

step2:在本地ping 一下测试网址的host

wangju@wangju-HP--G4:~$ ping appapi.xxx.com
PING appapi.xxx.com (47.95.31.237) () bytes of data.
bytes from 47.95.31.237 (47.95.31.237): icmp_seq= ttl= time=5.18 ms
bytes from 47.95.31.237 (47.95.31.237): icmp_seq= ttl= time=3.94 ms
^C
--- appapi.xxx.com ping statistics ---
packets transmitted, received, % packet loss, time 999ms
rtt min/avg/max/mdev = 3.948/4.567/5.186/0.619 ms

观查到服务端和本地 同一host指向的ip不一样

解决 办法 就是在服务器端 配置被测网址的host与本地一样

nano /etc/hosts

增加内容如下:

47.95.31.237 appapi.xxx.com

本地运行项目成功 ,但在服务器运行程序就会报Failed to establish a new connection: [Errno -2] Name or service not known

再次运行脚本就可以正常执行了

本地运行项目成功 ,但在服务器运行程序就会报Failed to establish a new connection: [Errno -2] Name or service not known