所以我知道下面会看到很多代码,但我绝对难以接受,因为我在使用python时从未遇到过这样的问题。
我希望使用请求模块来帮助我加快一些基于Web的工作。我根据开发人员的要求下载了zipball并运行了#34; setup.py"将它安装到" site-packages"目录。
我已经验证了所有内容确实应该在哪里,理论上我应该能够导入它然后使用它,但是当我输入任何代码来创建新请求时,例如:<登记/> r = requests.get(&#39; https://api.github.com/events&#39;) 我在下面得到了大量的错误,我不明白这一点。
Traceback (most recent call last):
File "C:\Users\Dakota\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests-2.8.1-py3.5.egg\requests\packages\urllib3\connection.py", line 135, in _new_conn
(self.host, self.port), self.timeout, **extra_kw)
File "C:\Users\Dakota\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests-2.8.1-py3.5.egg\requests\packages\urllib3\util\connection.py", line 90, in create_connection
raise err
File "C:\Users\Dakota\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests-2.8.1-py3.5.egg\requests\packages\urllib3\util\connection.py", line 80, in create_connection
sock.connect(sa)
**ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it**
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Dakota\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests-2.8.1-py3.5.egg\requests\packages\urllib3\connectionpool.py", line 554, in urlopen
self._prepare_proxy(conn)
File "C:\Users\Dakota\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests-2.8.1-py3.5.egg\requests\packages\urllib3\connectionpool.py", line 748, in _prepare_proxy
conn.connect()
File "C:\Users\Dakota\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests-2.8.1-py3.5.egg\requests\packages\urllib3\connection.py", line 215, in connect
conn = self._new_conn()
File "C:\Users\Dakota\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests-2.8.1-py3.5.egg\requests\packages\urllib3\connection.py", line 144, in _new_conn
self, "Failed to establish a new connection: %s" % e)
requests.packages.urllib3.exceptions.NewConnectionError: <requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x03908510>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Dakota\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests-2.8.1-py3.5.egg\requests\adapters.py", line 370, in send
timeout=timeout
File "C:\Users\Dakota\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests-2.8.1-py3.5.egg\requests\packages\urllib3\connectionpool.py", line 609, in urlopen
_stacktrace=sys.exc_info()[2])
File "C:\Users\Dakota\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests-2.8.1-py3.5.egg\requests\packages\urllib3\util\retry.py", line 271, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
requests.packages.urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.github.com', port=443): Max retries exceeded with url: /events (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x03908510>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it',)))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
r = requests.get('https://api.github.com/events')
File "C:\Users\Dakota\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests-2.8.1-py3.5.egg\requests\api.py", line 69, in get
return request('get', url, params=params, **kwargs)
File "C:\Users\Dakota\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests-2.8.1-py3.5.egg\requests\api.py", line 50, in request
response = session.request(method=method, url=url, **kwargs)
File "C:\Users\Dakota\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests-2.8.1-py3.5.egg\requests\sessions.py", line 468, in request
resp = self.send(prep, **send_kwargs)
File "C:\Users\Dakota\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests-2.8.1-py3.5.egg\requests\sessions.py", line 576, in send
r = adapter.send(request, **kwargs)
File "C:\Users\Dakota\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests-2.8.1-py3.5.egg\requests\adapters.py", line 431, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='api.github.com', port=443): Max retries exceeded with url: /events (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x03908510>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it',)))
答案 0 :(得分:2)
我回答了我自己的问题,因为我在URLLIB3和Python 3.5请求的文档中做了更多的挖掘,发现根问题是WIN错误10061.
URLLIB3和Requests从Internet Explorer的默认代理设置中获取其设置。
通过将我的Internet Explorer代理设置重置为空,它解决了我的所有问题。
出于某种原因,即使我从未在这台机器上使用过Internet Explorer,除了下载firefox,我使用的其他一些应用程序必须更改IE中的设置。可能是Windows防火墙。
希望将来出现的任何人都会看到这个问题并从我的错误中吸取教训。
答案 1 :(得分:0)
很明显,您遇到了连接问题。不知道更多,我猜这是由于代理问题。如果是,请stackoverflow answer设置requests
模块的代理。
如果没有,1)操作系统是什么?,2)为pip设置环境变量? 3)企业网络还是家庭?
答案 2 :(得分:0)
确实,问题出在 Internet 设置上。如何修复: