Session.send()报错
def hard_request(): from requests import Request, Session s = Session() headers = {'User-Agent': 'fake1.3.4'} req = Request('GET', build_url('user/emails'), auth=('imoocdemo', 'imoocdemo123'), headers=headers) prep = req.prepare() print prep.body print prep.headers resp = s.send(prep, timeout=10) print resp.status_code print resp.headers print resp.text
输出结果:
None
{'Authorization': 'Basic aW1vb2NkZW1vOmltb29jZGVtbzEyMw==', 'User-Agent': 'fake1.3.4'}
Traceback (most recent call last):
File "C:/Users/10234881/PycharmProjects/test/requests_demo/github_api.py", line 63, in <module>
hard_request()
File "C:/Users/10234881/PycharmProjects/test/requests_demo/github_api.py", line 53, in hard_request
resp = s.send(prep, timeout=10)
File "C:\Python27\lib\site-packages\requests-2.21.0-py2.7.egg\requests\sessions.py", line 646, in send
r = adapter.send(request, **kwargs)
File "C:\Python27\lib\site-packages\requests-2.21.0-py2.7.egg\requests\adapters.py", line 504, in send
raise ConnectTimeout(e, request=request)
requests.exceptions.ConnectTimeout: HTTPSConnectionPool(host='api.github.com', port=443): Max retries exceeded with url: /user/emails (Caused by ConnectTimeoutError(<urllib3.connection.VerifiedHTTPSConnection object at 0x040BD290>, 'Connection to api.github.com timed out. (connect timeout=10)'))