码迷,mamicode.com
首页 > 编程语言 > 详细

python requests.exceptions.ConnectionError

时间:2017-08-17 17:23:06      阅读:580      评论:0      收藏:0      [点我收藏+]

标签:connect   filename   set   reason   lan   stat   item   getattr   ons   

今天遇到一个奇葩问题,

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

  

 

 

python requests.exceptions.ConnectionError

标签:connect   filename   set   reason   lan   stat   item   getattr   ons   

原文地址:http://www.cnblogs.com/alamZ/p/7382633.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!