今天遇到一个奇葩问题,

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的更多相关文章

  1. 【airtest, python】报错:requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(54, 'Connection reset by peer')),解决方法如下

    环境及设备 mac, xcode , iphonex 问题 最近出现一个让人费解的问题,airtest 没跑多长时间,服务就断掉,而且总是报“requests.exceptions.Connectio ...

  2. requests 处理异常错误 requests.exceptions.ConnectionError HTTPSConnectionPool [Errno 10060]

    使用python requests模块调用vmallarg.vmall.com接口API时报如下错误: requests.exceptions.ConnectionError: HTTPSConnec ...

  3. 【Mac + ATX基于uiautomator2】使用weditor时,报错:requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(54, 'Connection reset by peer'))

    产生以下原因找到了:是因为启动了appium,两者冲突,不能同时使用. 之前讲过怎么安装u2([Mac安装,ATX基于uiautomator2]之安装步骤)以及使用weditor, 但是经过一段时间, ...

  4. 【airtest】报错:requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(54, 'Connection reset by peer')),解决方法如下

    1. 环境及设备:mac, xcode , iphonex 2. 最近出现一个让人费解的问题,airtest 没跑多长时间,服务就断掉,而且总是报“requests.exceptions.Connec ...

  5. 工作问题--------爬虫遇到requests.exceptions.ConnectionError: HTTPSConnectionPool Max retries exceeded

    问题描述:爬取京东的网站,爬取一段时间后报错. 经过一番查询,发现该错误是因为如下: http的连接数超过最大限制,默认的情况下连接是Keep-alive的,所以这就导致了服务器保持了太多连接而不能再 ...

  6. 关于requests.exceptions.ConnectionError: HTTPSConnectionPool的问题

    错误如下: raise ConnectionError(e, request=request)requests.exceptions.ConnectionError: HTTPSConnectionP ...

  7. 安装SpaCy出现报错:requests.exceptions.ConnectionError: HTTPSConnectionPool(host='raw.githubusercontent.com', port=443):

    内含安装步骤及报错解决:https://www.cnblogs.com/xiaolan-Lin/p/13286885.html

  8. python +requests 爬虫-爬取图片并进行下载到本地

    因为写12306抢票脚本需要用到爬虫技术下载验证码并进行定位点击所以这章主要讲解,爬虫,从网页上爬取图片并进行下载到本地   爬虫实现方式: 1.首先选取你需要的抓取的URL:2.将这些URL放入待抓 ...

  9. Python requests 使用心得

    最近在用requests写一些项目,遇见了一些问题,百度了很多,有些都不太好使,最后看了下requestsAPI文档,才明白了很多,最后项目趋于稳定.看来学东西还是API文档比较权威啊~ 问题场景 项 ...

随机推荐

  1. Setting an appropriate geodatabase spatial domain

    原文地址:http://webhelp.esri.com/arcgisdesktop/9.1/body.cfm?tocVisable=1&ID=1470&TopicName=Setti ...

  2. 【mybatis】mybatis中update更新原来的值加1

    示例代码: floor的值 = floor原来的值+要更新的差距值 <update id="updateFloor" parameterType="com.pise ...

  3. C#中使用 HttpWebRequest 向网站提交数据

    HttpWebRequest 是 .NET 基类库中的一个类,在命名空间 System.Net 里,用来使用户通过 HTTP 协议和服务器交互. HttpWebRequest 对 HTTP 协议进行了 ...

  4. cdev结构体及其相关函数

    一.在Linux2.6内核中一个字符设备用cdev结构来描述,其定义如下: struct cdev { struct kobject kobj; struct module *owner; //所属模 ...

  5. GPU Instance

    http://forum.china.unity3d.com/thread-17131-1-1.html https://docs.unity3d.com/Manual/GPUInstancing.h ...

  6. List<实体>与List<String>数据互转

    1.List<实体>数据: public List<Device> queryOSDevice(String cpu,String ip,String name){ Strin ...

  7. Android实战简易教程-第十枪(画廊组件Gallery有用研究)

    Gallery组件用于拖拽浏览图片,以下我们就来看一下怎样实现. 一.实现Gallery 1.布局文件非常easy: <?xml version="1.0" encoding ...

  8. 如何在Ubuntu上使用Glances监控系统

    导读 Glances 是一个用于监控系统的跨平台.基于文本模式的命令行工具.它是用 Python 编写的,使用 psutil 库从系统获取信息.你可以用它来监控 CPU.平均负载.内存.网络接口.磁盘 ...

  9. Android TextView 常见问题与使用总结

    一.文字显示行数设置 1. 仅显示一行文字 android:singleLine="true" setTransformationMethod(TransformationMeth ...

  10. git学习——远程仓库操作

    查看当前的远程库——git remote 列出了仅仅是远程库的简单名字 可以加上-v 现实对应的克隆地址 添加远程仓库——git remote add [shortname] [url] git re ...