[英]TypeError when calling requests_oauthlib multiple times (error actually raised within urllib3)
我正在编写一个脚本以从Twitter API中提取一些数据。 这是使用OAuth 1.1的意思我使用的requests_oauthlib
之上助手库requests
验证会话。
对API的第一次调用有效,但随后的调用给出TypeError
,如下所示:
/Users/phil/code/Virtualenv/req_test/lib/python2.7/site-packages/requests/packages/urllib3/connection.pyc in __init__(self, *args, **kw)
124
125 # Superclass also sets self.source_address in Python 2.7+.
--> 126 _HTTPConnection.__init__(self, *args, **kw)
127
128 def _new_conn(self):
TypeError: unbound method __init__() must be called with HTTPConnection instance as first argument (got VerifiedHTTPSConnection instance instead)
会话中似乎存在某些问题,因为经常重复使用该错误会出现。 我已经尝试了一个干净的virtualenv,并通过pip安装了最新版本,没有区别。
我使用上下文管理器方法,因此认为每次调用后会话都将被破坏,从而避免了这种情况的发生:
with ro.OAuth1Session(**self._auth) as s:
response = s.get(url)
任何了解导致问题的原因的修补程序或指针,将不胜感激。
编辑:我尝试了一种不同的方法,使用如requests
文档( http://docs.python-requests.org/en/master/user/authentication/ )中所述的另一种方式来建立会话,但相同的错误是提高。
编辑:全栈,以防有用:
/Users/phil/code/Virtualenv/req_test/lib/python2.7/site-packages/requests/sessions.pyc in get(self,url,** kwargs)485486 kwargs.setdefault('allow_redirects',True)- -> 487 return self.request('GET',url,** kwargs)488489 def选项(self,url,** kwargs):
/Users/phil/code/Virtualenv/req_test/lib/python2.7/site-packages/requests/sessions.pyc in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
473 }
474 send_kwargs.update(settings)
--> 475 resp = self.send(prep, **send_kwargs)
476
477 return resp
/Users/phil/code/Virtualenv/req_test/lib/python2.7/site-packages/requests/sessions.pyc in send(self, request, **kwargs)
583
584 # Send the request
--> 585 r = adapter.send(request, **kwargs)
586
587 # Total elapsed time of the request (approximately)
/Users/phil/code/Virtualenv/req_test/lib/python2.7/site-packages/requests/adapters.pyc in send(self, request, stream, timeout, verify, cert, proxies)
401 decode_content=False,
402 retries=self.max_retries,
--> 403 timeout=timeout
404 )
405
/Users/phil/code/Virtualenv/req_test/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.pyc in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, **response_kw)
564 # Request a connection from the queue.
565 timeout_obj = self._get_timeout(timeout)
--> 566 conn = self._get_conn(timeout=pool_timeout)
567
568 conn.timeout = timeout_obj.connect_timeout
/Users/phil/code/Virtualenv/req_test/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.pyc in _get_conn(self, timeout)
254 conn = None
255
--> 256 return conn or self._new_conn()
257
258 def _put_conn(self, conn):
/Users/phil/code/Virtualenv/req_test/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.pyc in _new_conn(self)
800 conn = self.ConnectionCls(host=actual_host, port=actual_port,
801 timeout=self.timeout.connect_timeout,
--> 802 strict=self.strict, **self.conn_kw)
803
804 return self._prepare_conn(conn)
/Users/phil/code/Virtualenv/req_test/lib/python2.7/site-packages/requests/packages/urllib3/connection.pyc in __init__(self, host, port, key_file, cert_file, strict, timeout, **kw)
208
209 HTTPConnection.__init__(self, host, port, strict=strict,
--> 210 timeout=timeout, **kw)
211
212 self.key_file = key_file
/Users/phil/code/Virtualenv/req_test/lib/python2.7/site-packages/requests/packages/urllib3/connection.pyc in __init__(self, *args, **kw)
124
125 # Superclass also sets self.source_address in Python 2.7+.
--> 126 _HTTPConnection.__init__(self, *args, **kw)
127
128 def _new_conn(self):
TypeError: unbound method __init__() must be called with HTTPConnection instance as first argument (got VerifiedHTTPSConnection instance instead)
OAuth1Session的格式为:
oauth = OAuth1Session(client_key,
client_secret=client_secret,
resource_owner_key=resource_owner_key,
resource_owner_secret=resource_owner_secret,
verifier=verifier)
**与关键字参数一起使用,并且OAuth1Session具有不同的签名
编辑:还添加了对在IPython / Jupyter中重新加载容器库的引用。
读一个公平位后,问题出现了,当你调用可能出现get
的请求,但不访问响应体(这是我做过什么,而建设/调试流程):
http://docs.python-requests.org/en/master/user/advanced/#keep-alive
"Note that connections are only released back to the pool for reuse once all body data has been read; be sure to either set stream to False or read the content property of the Response object."
因此,答案是通过发出Response.content
, Response.json()
或类似方法来确保发出请求后的第一件事是刷新响应。
仅在使用requests_oauthlib
库时才出现此问题,因为它使用的会话不太常见。 通过使用不影响会话对象本身的查询参数,我对Facebook和LinkedIn API进行了类似操作,没有出现问题。
在IPython / Jupyter中重新加载帮助程序库时,它也最常出现。 退出笔记本或命令行会话,然后重新启动将消除此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.