python requests.exceptions.ConnectionError
2017-08-17 16:48
531 查看
今天遇到一个奇葩问题,
1.r.request.post(url)
2..print r. status_code
居然第一步就报错了,原因是url不正确,按道理应该可以走到第二步然后输入404的
import requests try: requests.get("http://not.a.real.url/really_not") except requests.exceptions.ConnectionError as e: pass >>> e ConnectionError(MaxRetryError("HTTPConnectionPool(host='not.a.real.url', port=80): Max retries exceeded with url: /really_not (Caused by <class 'socket.gaierror'>: [Errno -2] Name or service not known)",),) >>> e.args (MaxRetryError("HTTPConnectionPool(host='not.a.real.url', port=80): Max retries exceeded with url: /really_not (Caused by <class 'socket.gaierror'>: [Errno -2] Name or service not known)",),) >>> e.args[0] MaxRetryError("HTTPConnectionPool(host='not.a.real.url', port=80): Max retries exceeded with url: /really_not (Caused by <class 'socket.gaierror'>: [Errno -2] Name or service not known)",) >>> dir(e.args[0]) ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__getitem__', '__getslice__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__unicode__', '__weakref__', 'args', 'message', 'pool', 'reason', 'url'] >>> e.args[0].reason gaierror(-2, 'Name or service not known') >>> dir(e.args[0].reason) ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__getitem__', '__getslice__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__unicode__', '__weakref__', 'args', 'errno', 'filename', 'message', 'strerror'] >>> e.args[0].reason.errno -2
这样就可以异常设置值
try: r = login_session.post(self.url, data=self.data,headers=self.headers) except requests.exceptions.ConnectionError as e: # print e.args[0].reason #[Errno 11004] getaddrinfo failed if e.args[0].reason.errno== 11004: r = 404 final: return r
得到r,即使不是response类型,也能获取值
getattr(result, 'status_code', result)
相关文章推荐
- pycharm和fiddler同时开启时,报requests.exceptions.ProxyError: HTTPSConnectionPool,解决办法
- pyhon_requests_ requests.exceptions.ConnectionError
- requests.exceptions.ConnectionError: ('Connection aborted.', BadStatusLine("''",))
- 银豹 PYTHON requests.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed
- gevent和requests同时使用报requests.exceptions.ReadTimeout:HTTPSConnectionPool(host='www.baidu.com', port=4
- python 爬虫 <class 'socket.error'>: [Errno 104] Connection reset by peer)
- Python使用post请求时遇到.ConnectionError: HTTPConnectionPool Max retries exceeded with url: Failed to esta
- Python package下载中遇到ReadTimeoutError: HTTPSConnectionPool?
- python ConnectionError 或 curl Could not resolve host
- requests.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581
- centos 安装pysqlite-2.8.3.tar.gz src/connection.h:26:20: fatal error: Python.h: No such file or direct
- pip升级出错解决方法ReadTimeoutError: HTTPSConnectionPool(host='pypi.python.org', port=443): Read ti med out.
- python [Errno socket error] [Errno 104] Connection reset by peer
- pip安装scrapy报错 ReadTimeoutError: HTTPSConnectionPool(host='pypi.python.org, port=443) Read time out
- Python——报错:ImportError: cannot import name requests
- Python requests 多线程抓取 出现HTTPConnectionPool Max retires exceeded异常
- 用py2exe打包运行后出现requests.exceptions.SSLError: [Errno 2]解决办法
- Python中的错误与异常处理(Error & Exceptions)
- pip install Error - ReadTimeoutError: HTTPSConnectionPool(host='pypi.python.org', port=443): Read
- 【python】AttributeError: 'module' object has no attribute 'HTTPSConnection'