Python Error:No host specified

若如初见. 提交于 2021-02-10 05:38:51

问题


I just wrote a simple python demo,while met a confusing problem.

import requests

print(requests.get('http://www.sina.com.cn/'))

I know that right result is return Response [200].But in my WIN10 x64,it returns the following error,I guess maybe some problems occur in my computer.

Traceback (most recent call last):
  File "C:\Users\CJY\Desktop\Python_Demo\web.py", line 2, in <module>
    print(requests.get('http://www.sina.com.cn/'))
  File "D:\python3.6.1\lib\site-packages\requests\api.py", line 72, in get
    return request('get', url, params=params, **kwargs)
  File "D:\python3.6.1\lib\site-packages\requests\api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "D:\python3.6.1\lib\site-packages\requests\sessions.py", line 518, in request
    resp = self.send(prep, **send_kwargs)
  File "D:\python3.6.1\lib\site-packages\requests\sessions.py", line 639, in send
    r = adapter.send(request, **kwargs)
  File "D:\python3.6.1\lib\site-packages\requests\adapters.py", line 403, in send
    conn = self.get_connection(request.url, proxies)
  File "D:\python3.6.1\lib\site-packages\requests\adapters.py", line 302, in get_connection
    conn = proxy_manager.connection_from_url(url)
  File "D:\python3.6.1\lib\site-packages\requests\packages\urllib3\poolmanager.py", line 279, in connection_from_url
    pool_kwargs=pool_kwargs)
  File "D:\python3.6.1\lib\site-packages\requests\packages\urllib3\poolmanager.py", line 408, in connection_from_host
    self.proxy.host, self.proxy.port, self.proxy.scheme, pool_kwargs=pool_kwargs)
  File "D:\python3.6.1\lib\site-packages\requests\packages\urllib3\poolmanager.py", line 218, in connection_from_host
    raise LocationValueError("No host specified.")
requests.packages.urllib3.exceptions.LocationValueError: No host specified.
[Finished in 0.2s]

Please help me!


回答1:


That works for me. Please ensure you have internet connectivity and you can ping www.sina.com.cn




回答2:


Just tested this with the same python version on windows 10 64-Bit and it worked for me.

When using requests in windows i have come across the same error when the local dns cache is pointing to an incorrect value. If you are still having no luck try flushing the local dns cache on that machine my entering the following command in command prompt.

ipconfig /flushdns



回答3:


error location:

Lib\urllib\request.py:
proxyEnable = winreg.QueryValueEx(internetSettings, 'ProxyEnable')[0]

if proxyEnable is string , you'll see the error. The reason is in your registry, ProxyEnable is set as REG_SZ but not REG_DWORD, so change it and all is ok. open the registry:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings \ ProxyEnable

(you can also directly search ProxyEnable)

delete ProxyEnable create a new ProxyEnable form (REG_SZ 0) to (REG_DWORD 0x00000000(0))

see follow pictures,my pc language is chinese, but the location for ProxyEnable is the same.

create a new ProxyEnable

right value for ProxyEnable



来源:https://stackoverflow.com/questions/44173904/python-errorno-host-specified

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!