python requests/urllib3 连接池未捕获 HTTP 错误
python requests/urllib3 connection pooling not catching HTTP errors
python 使用连接池的请求 (urllib3) 未捕获 http 错误。这是一个错误吗?还是我做错了什么?
#!/usr/bin/env python
import contextlib
import requests
import sys
connection_pool_size = 2
adapter = requests.adapters.HTTPAdapter(pool_connections=connection_pool_size,
pool_maxsize=connection_pool_size)
r_session = requests.Session()
r_session.mount('http', adapter)
try:
with contextlib.closing(r_session.get(sys.argv[1], timeout=30, allow_redirects=True)) as r:
print 'success %r' % r
except requests.exceptions.HTTPError as e:
print 'HTTPError %r' % e
except Exception as e:
print 'Exception %r' % e
输出:
$ ./test.py https://github.com
success <Response [200]>
$ ./test.py https://github.com/sithlordyoyoma
success <Response [404]>
我期待 HTTPError 。我做错了什么吗?
关闭我从这个线程 should I call close() after urllib.urlopen()? 获得的 contextlib。正如 Alex Martelli 所建议的那样。
实际上 运行 没有连接的请求也表现出这种行为
#!/usr/bin/env python
import contextlib
import requests
import sys
try:
with contextlib.closing(requests.get(sys.argv[1], timeout=30, allow_redirects=True)) as r:
print 'success %r' % r
except requests.exceptions.HTTPError as e:
print 'HTTPError %r' % e
except Exception as e:
print 'Exception %r' % e
输出:
$ ./test.py https://github.com
success <Response [200]>
$ ./test.py https://github.com/sithlordyoyoma
success <Response [404]>
urllib2 正确地做到了这一点
#!/usr/bin/env python
import contextlib
import urllib2
import sys
try:
with contextlib.closing(urllib2.urlopen(sys.argv[1], timeout=30)) as r:
print 'success %r' % r
except urllib2.HTTPError as e:
print 'HTTPError %r' % e
except Exception as e:
print 'Exception %r' % e
输出:
$ ./test.py https://github.com
success <addinfourl at 4338734792 whose fp = <socket._fileobject object at 0x1025a5c50>>
$ ./test.py https://github.com/sithlordyoyoma
HTTPError HTTPError()
无论连接池如何,requests.post
(和其他 HTTP 方法)不会在 404 上引发 HTTPError
。通过调用 .raise_for_status()
引发 HTTPError
,如下所示示例演示:
#!/usr/bin/env python
import requests
r = requests.post(
'https://github.com/sithlordyoyoma',
timeout=30,
allow_redirects=True
)
print 'success %r' % r
r.raise_for_status()
python 使用连接池的请求 (urllib3) 未捕获 http 错误。这是一个错误吗?还是我做错了什么?
#!/usr/bin/env python
import contextlib
import requests
import sys
connection_pool_size = 2
adapter = requests.adapters.HTTPAdapter(pool_connections=connection_pool_size,
pool_maxsize=connection_pool_size)
r_session = requests.Session()
r_session.mount('http', adapter)
try:
with contextlib.closing(r_session.get(sys.argv[1], timeout=30, allow_redirects=True)) as r:
print 'success %r' % r
except requests.exceptions.HTTPError as e:
print 'HTTPError %r' % e
except Exception as e:
print 'Exception %r' % e
输出:
$ ./test.py https://github.com
success <Response [200]>
$ ./test.py https://github.com/sithlordyoyoma
success <Response [404]>
我期待 HTTPError 。我做错了什么吗?
关闭我从这个线程 should I call close() after urllib.urlopen()? 获得的 contextlib。正如 Alex Martelli 所建议的那样。
实际上 运行 没有连接的请求也表现出这种行为
#!/usr/bin/env python
import contextlib
import requests
import sys
try:
with contextlib.closing(requests.get(sys.argv[1], timeout=30, allow_redirects=True)) as r:
print 'success %r' % r
except requests.exceptions.HTTPError as e:
print 'HTTPError %r' % e
except Exception as e:
print 'Exception %r' % e
输出:
$ ./test.py https://github.com
success <Response [200]>
$ ./test.py https://github.com/sithlordyoyoma
success <Response [404]>
urllib2 正确地做到了这一点
#!/usr/bin/env python
import contextlib
import urllib2
import sys
try:
with contextlib.closing(urllib2.urlopen(sys.argv[1], timeout=30)) as r:
print 'success %r' % r
except urllib2.HTTPError as e:
print 'HTTPError %r' % e
except Exception as e:
print 'Exception %r' % e
输出:
$ ./test.py https://github.com
success <addinfourl at 4338734792 whose fp = <socket._fileobject object at 0x1025a5c50>>
$ ./test.py https://github.com/sithlordyoyoma
HTTPError HTTPError()
无论连接池如何,requests.post
(和其他 HTTP 方法)不会在 404 上引发 HTTPError
。通过调用 .raise_for_status()
引发 HTTPError
,如下所示示例演示:
#!/usr/bin/env python
import requests
r = requests.post(
'https://github.com/sithlordyoyoma',
timeout=30,
allow_redirects=True
)
print 'success %r' % r
r.raise_for_status()