Python 2.7 urllib3.HTTPSConnectionPool错误
我尝试多次请求RestAPI资源。为了节省时间,我尝试使用urllib3.HTTPSConnectionPool而不是urllib2。但是,它不断向我抛出以下错误:Python 2.7 urllib3.HTTPSConnectionPool错误,python-2.7,urllib3,Python 2.7,Urllib3,我尝试多次请求RestAPI资源。为了节省时间,我尝试使用urllib3.HTTPSConnectionPool而不是urllib2。但是,它不断向我抛出以下错误: Traceback (most recent call last): File "LCRestapi.py", line 135, in <module> listedLoansFast(version, key, showAll='false') File "LCRestapi.py", line 55
Traceback (most recent call last):
File "LCRestapi.py", line 135, in <module>
listedLoansFast(version, key, showAll='false')
File "LCRestapi.py", line 55, in listedLoansFast
pool.urlopen('GET',url+resource,headers={'Authorization':key})
File "/Library/Python/2.7/site-packages/urllib3/connectionpool.py", line 515, in urlopen
raise HostChangedError(self, url, retries)
urllib3.exceptions.HostChangedError: HTTPSConnectionPool(host='https://api.lendingclub.com/api/investor/v1/loans/listing?showAll=false', port=None): Tried to open a foreign host with url: https://api.lendingclub.com/api/investor/v1/loans/listing?showAll=false
谢谢你的帮助 问题是您正在创建一个
PoolManager
,但从未使用过它。相反,您还创建了一个HTTPSConnectionPool
(绑定到特定主机)并使用它来代替PoolManager
。PoolManager
将代表您自动管理HTTPSConnectionPool
对象,因此您无需担心
这应该起作用:
# Your example called this `manager`
http = urllib3.PoolManager()
url = 'https://api.lendingclub.com/api/investor/v1/loans/listing?showAll=false'
headers = {'Authorization': key}
# Your example did url+resource, but let's assume the url variable
# contains the combined absolute url.
r = http.request('GET', url, headers=headers)
print r.data
如果愿意,您可以指定PoolManager
的大小,但除非您试图将资源限制在线程池中,否则不需要指定
# Your example called this `manager`
http = urllib3.PoolManager()
url = 'https://api.lendingclub.com/api/investor/v1/loans/listing?showAll=false'
headers = {'Authorization': key}
# Your example did url+resource, but let's assume the url variable
# contains the combined absolute url.
r = http.request('GET', url, headers=headers)
print r.data