python requests.exceptions.ConnectionError
今天遇到一个奇葩问题,
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)
python requests.exceptions.ConnectionError的更多相关文章
- 【airtest, python】报错:requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(54, 'Connection reset by peer')),解决方法如下
环境及设备 mac, xcode , iphonex 问题 最近出现一个让人费解的问题,airtest 没跑多长时间,服务就断掉,而且总是报“requests.exceptions.Connectio ...
- requests 处理异常错误 requests.exceptions.ConnectionError HTTPSConnectionPool [Errno 10060]
使用python requests模块调用vmallarg.vmall.com接口API时报如下错误: requests.exceptions.ConnectionError: HTTPSConnec ...
- 【Mac + ATX基于uiautomator2】使用weditor时,报错:requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(54, 'Connection reset by peer'))
产生以下原因找到了:是因为启动了appium,两者冲突,不能同时使用. 之前讲过怎么安装u2([Mac安装,ATX基于uiautomator2]之安装步骤)以及使用weditor, 但是经过一段时间, ...
- 【airtest】报错:requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(54, 'Connection reset by peer')),解决方法如下
1. 环境及设备:mac, xcode , iphonex 2. 最近出现一个让人费解的问题,airtest 没跑多长时间,服务就断掉,而且总是报“requests.exceptions.Connec ...
- 工作问题--------爬虫遇到requests.exceptions.ConnectionError: HTTPSConnectionPool Max retries exceeded
问题描述:爬取京东的网站,爬取一段时间后报错. 经过一番查询,发现该错误是因为如下: http的连接数超过最大限制,默认的情况下连接是Keep-alive的,所以这就导致了服务器保持了太多连接而不能再 ...
- 关于requests.exceptions.ConnectionError: HTTPSConnectionPool的问题
错误如下: raise ConnectionError(e, request=request)requests.exceptions.ConnectionError: HTTPSConnectionP ...
- 安装SpaCy出现报错:requests.exceptions.ConnectionError: HTTPSConnectionPool(host='raw.githubusercontent.com', port=443):
内含安装步骤及报错解决:https://www.cnblogs.com/xiaolan-Lin/p/13286885.html
- python +requests 爬虫-爬取图片并进行下载到本地
因为写12306抢票脚本需要用到爬虫技术下载验证码并进行定位点击所以这章主要讲解,爬虫,从网页上爬取图片并进行下载到本地 爬虫实现方式: 1.首先选取你需要的抓取的URL:2.将这些URL放入待抓 ...
- Python requests 使用心得
最近在用requests写一些项目,遇见了一些问题,百度了很多,有些都不太好使,最后看了下requestsAPI文档,才明白了很多,最后项目趋于稳定.看来学东西还是API文档比较权威啊~ 问题场景 项 ...
随机推荐
- 【k8s】了解一下k8s
了解一下k8s 地址: https://www.kubernetes.org.cn/doc-11
- 解决小米手机Android Studio安装app 报错的问题It is possible that this issue is resolved by uninstalling an existi
问题描述 Android Studio升级到2.3版本之后,小米手机MIUI8不能运行Android Studio程序,报如下错误: Installation failed with message ...
- CSS实现四种loading动画效果
四种loading加载效果: <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...
- Leagal or Not —— 拓扑排序(王道)
Problem Description ACM-DIY is a large QQ group where many excellent acmers get together. It is so h ...
- oracle 存储过程的一个小问题
在存储过程中,一般不能直接使用DDL语句,需要借助 execute immediate方法,否则可能报 pls00103错误 例子如下: create or replace procedure rt_ ...
- 2015 Multi-University Training Contest 1记录
1001 OO's Sequence 分析: 对于例子,能够得到,我们要求的是(1,1)(1,2)(1,3)(1,4)(1,5)(2,2)(2,3)(2,4)(2,5)(3,3)(3,4)(3,5)( ...
- Axure——恢复以及备份功能从容面对意外
1. 背景 近期刚刚接手java项目,在进行了为期一周的需求理解之后,对于最初的原型也開始做微小的调整,十一期的小伙伴们也积极地參与进来,这使得我们这个团队不断地在壮大和温馨. 眼看着原型即 ...
- 手机号码月消费档次API
手机号码月消费档次API,返回手机号的每月消费水平,身份证姓名不做一致性校验 文档:https://www.juhe.cn/docs/api/id/261 接口地址:http://v.juhe.cn/ ...
- GDB基本命令(整合)(转)
directory:添加源文件目录 l src.cpp:line_num可进入文件 如:l src.cpp:10 回车自动重复上一命令 一.gdb调试基本知识a.调试器指示的是将要执行的代码行b.只有 ...
- 【DB2】对两列分组之后判断另外一列是否有重复
建立表数据如下: ),sex ),sex_nm ),OWER ),TYPE ),TYPE_NM )); ,','水果'), (,','水果'), (,','水果'), (,','水果'), (,',' ...