python3 使用 socket.gethostbyname 报错 getaddrinfo failed 解决方法

python2中:

val = socket.gethostbyname('http://www.baidu.com')

print (val)   

运行OK。


但在python3中还是如上代码运行时报错:[Errno 11004] getaddrinfo failed

原因是,输入地址时要把http://前缀去掉才ok。即

val = socket.gethostbyname('www.baidu.com')

print (val)  

运行正常。完成!
 

Traceback (most recent call last): File "C:\Anaconda\envs\tensorflow\lib\site-packages\urllib3\connection.py", line 198, in _new_conn sock = connection.create_connection( File "C:\Anaconda\envs\tensorflow\lib\site-packages\urllib3\util\connection.py", line 60, in create_connection for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM): File "C:\Anaconda\envs\tensorflow\lib\socket.py", line 966, in getaddrinfo for res in _socket.getaddrinfo(host, port, family, type, proto, flags): socket.gaierror: [Errno 11001] getaddrinfo failed The above exception was the direct cause of the following exception: Traceback (most recent call last): File "C:\Anaconda\envs\tensorflow\lib\site-packages\urllib3\connectionpool.py", line 787, in urlopen response = self._make_request( File "C:\Anaconda\envs\tensorflow\lib\site-packages\urllib3\connectionpool.py", line 488, in _make_request raise new_e File "C:\Anaconda\envs\tensorflow\lib\site-packages\urllib3\connectionpool.py", line 464, in _make_request self._validate_conn(conn) File "C:\Anaconda\envs\tensorflow\lib\site-packages\urllib3\connectionpool.py", line 1093, in _validate_conn conn.connect() File "C:\Anaconda\envs\tensorflow\lib\site-packages\urllib3\connection.py", line 704, in connect self.sock = sock = self._new_conn() File "C:\Anaconda\envs\tensorflow\lib\site-packages\urllib3\connection.py", line 205, in _new_conn raise NameResolutionError(self.host, self, e) from e urllib3.exceptions.NameResolutionError: <urllib3.connection.HTTPSConnection object at 0x0000020D94902070>: Failed to resolve &#39;api.massbank.eu&#39; ([Errno 11001] getaddrinfo failed) The above exception was the direct cause of the following exception: Traceback (most recent call last): File "C:\Anaconda\envs\tensorflow\lib\site-packages\requests\adapters.py", line 667, in send resp = conn.urlopen( File "C:\Anaconda\envs\tensorflow\lib\site-packages\urllib3\connectionpool.py", line 841, in urlopen retries = retries.increment( File "C:\Anaconda\envs\tensorflow\lib\site-packages\urllib3\util\retry.py", line 519, in increment raise MaxRetryError(_pool, url, reason) from reason # type: ignore[arg-type] urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host=&#39;api.massbank.eu&#39;, port=443): Max retries exceeded with url: /api/record?page=1 (Caused by NameResolutionError("<urllib3.connection.HTTPSConnection object at 0x0000020D94902070>: Failed to resolve &#39;api.massbank.eu&#39; ([Errno 11001] getaddrinfo failed)")) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "c:\Users\Mwj\Desktop\Chemical.py", line 46, in <module> df = get_massbank_data() File "c:\Users\Mwj\Desktop\Chemical.py", line 17, in get_massbank_data response = requests.get(url) File "C:\Anaconda\envs\tensorflow\lib\site-packages\requests\api.py", line 73, in get return request("get", url, params=params, **kwargs) File "C:\Anaconda\envs\tensorflow\lib\site-packages\requests\api.py", line 59, in request return session.request(method=method, url=url, **kwargs) File "C:\Anaconda\envs\tensorflow\lib\site-packages\requests\sessions.py", line 589, in request resp = self.send(prep, **send_kwargs) File "C:\Anaconda\envs\tensorflow\lib\site-packages\requests\sessions.py", line 703, in send r = adapter.send(request, **kwargs) File "C:\Anaconda\envs\tensorflow\lib\site-packages\requests\adapters.py", line 700, in send raise ConnectionError(e, request=request) requests.exceptions.ConnectionError: HTTPSConnectionPool(host=&#39;api.massbank.eu&#39;, port=443): Max retries exceeded with url: /api/record?page=1 (Caused by NameResolutionError("<urllib3.connection.HTTPSConnection object at 0x0000020D94902070>: Failed to resolve &#39;api.massbank.eu&#39; ([Errno 11001] getaddrinfo failed)"))
03-29
ConnectionError(MaxRetryError("HTTPConnectionPool(host=&#39;127.0.0.1&#39;, port=12376): Max retries exceeded with url: /script (Caused by NewConnectionError(&#39;<urllib3.connection.HTTPConnection object at 0x26166DB0>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。&#39;))")) PS D:\daily_test\media_test_new> python generate_testcase.py codec "[&#39;h264&#39;,&#39;h265&#39;]" "{&#39;被控&#39;:&#39;nvidia&#39;,&#39;主控&#39;:&#39;intel&#39;}" "192.168.103.145.12376" 8888 "http://client-file.zuler.sh/artifact/mediatest-todesk-toc-win/95/ToDesk_Setup_4.7.6.2_95_x64.exe" "video" codec [&#39;h264&#39;, &#39;h265&#39;] {&#39;被控&#39;: &#39;nvidia&#39;, &#39;主控&#39;: &#39;intel&#39;} 192.168.103.145.12376 8888 http://client-file.zuler.sh/artifact/mediatest-todesk-toc-win/95/ToDesk_Setup_4.7.6.2_95_x64.exe video ConnectionError(MaxRetryError("HTTPConnectionPool(host=&#39;192.168.103.145.12376&#39;, port=80): Max retries exceeded with url: /device/modify/false (Caused by NewConnectionError(&#39;<urllib3.connection.HTTPConnection object at 0x261A40B0>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed&#39;))")) http://127.0.0.1:12376/script media_test generate_testcase Error!!! ConnectionError(MaxRetryError("HTTPConnectionPool(host=&#39;127.0.0.1&#39;, port=12376): Max retries exceeded with url: /script (Caused by NewConnectionError(&#39;<urllib3.connection.HTTPConnection object at 0x261A4DF0>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。&#39;))")) PS D:\daily_test\media_test_new>
最新发布
07-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值