项目中有个模块是定时获取设备状态的,通过设备的http请求访问获取返回的状态码,然后根据状态码检测设备是否在线。但是最近发现连接后出现了偶尔连接不上的情况,报错如下:

2018-06-14 15:51:42,076 - configuration_model - ERROR - HTTPSConnectionPool(host='10.1.1.1', port=443): Max retries exceeded with url: /api/1.0/device/status (Caused by ConnectTimeoutError(, 'Connection to 10.1.1.1 timed out. (connect timeout=2)'))2018-06-14 15:51:48,475 - configuration_model - ERROR - HTTPSConnectionPool(host='10.1.1.1', port=443): Max retries exceeded with url: /api/1.0/device/status (Caused by ConnectTimeoutError(, 'Connection to 10.1.1.1 timed out. (connect timeout=2)'))2018-06-14 15:51:55,003 - configuration_model - ERROR - HTTPSConnectionPool(host='10.1.1.1', port=443): Max retries exceeded with url: /api/1.0/device/status (Caused by ConnectTimeoutError(, 'Connection to 10.1.1.1 timed out. (connect timeout=2)'))

最初怀疑是网络原因,写了个定时脚本,看能否稳定复现这个问题:

#coding:utf-8

import requests

from requests.auth import HTTPBasicAuth

import json

import time

import logging

import logging.handlers

from mlogging import TimedRotatingFileHandler_MP

logging.captureWarnings(True)

def get_logger(logger_fie = "/home/test/testTacConnect.log"):

logger = logging.getLogger(logger_fie)

logger.setLevel(logging.DEBUG)

filehandle = TimedRotatingFileHandler_MP(logger_fie, 'midnight')

filehandle.suffix = "%Y-%m-%d"

filehandle.setLevel(logging.DEBUG)

consolehandle = logging.StreamHandler()

consolehandle.setLevel(logging.DEBUG)

formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

filehandle.setFormatter(formatter)

consolehandle.setFormatter(formatter)

logger.addHandler(filehandle)

logger.addHandler(consolehandle)

return logger

def getTacstatus(userName, password, tacip, EXITTIME = 2):

# 设备请求超时时间,默认为2秒

status = "0"

try:

url = "https://%s/api/1.0/device/status"%(tacip,)

req = requests.post(url, auth=HTTPBasicAuth(userName, password), timeout=EXITTIME, verify=False)

result = json.loads(req.content)

if result.get(u'code') == 200:

status = "1"

except Exception as e:

logger.error(e)

return status

if __name__ == '__main__':

tacip = '10.1.1.1'

userName = '*****'

password = '*******'

logger = get_logger()

while True:

status = getTacstatus(userName,password,tacip)

if status != 1:

logger.debug(status)

time.sleep(10)

运行了1个小时之后,异常状态出现了,request请求报了Max retries exceeded with url。

看了下网上的相关论坛,发现是http请求连接太多没有关闭造成的,在request对象获取session,然后设置下状态,如下:

sess = requests.session()

sess.keep_alive = False

更新了代码,在测试脚本运行,观察中。

python requests max retries_python requests报Max retries exceeded with url异常相关推荐

  1. 【爬虫 | Python】解决‘Requests Max Retries Exceeded With Url‘报错的问题

    [爬虫 | Python]解决'Requests Max Retries Exceeded With Url in Python'报错的问题 背景 解决方案 一.普遍方案 细致方案 一.问题重述 1. ...

  2. 【Bug】python requests发起请求,报“Max retries exceeded with url”

    在本地机器上,高频率重复调用一个API接口,出现"Max retries exceeded with url",拒绝连接的情况. 楼主讲一下,遇到这个bug的过程: 在服务器上开发 ...

  3. requests.exceptions.SSLError: HTTPSConnectionPool Max retries exceeded with url 报错解决方法

    import requests import jsonheaders = {"Content-Type": "application/json"} url = ...

  4. Max retries exceeded with URL in requests

    爬虫的时候遇到的一个错误 Max retries exceeded with URL in requests requests.exceptions.ConnectionError: HTTPSCon ...

  5. Max retries exceeded with URL报错小记

    from hyp mistake: 循环post请求第二遍报错 Max retries exceeded with URL for i in fac:url=******payload = ***** ...

  6. http请求报错SSLError: HTTPSConnectionPool:Max retries exceeded with url

    1.问题描述: 迭代请求http web服务,中途遇到异常报错: requests.exceptions.SSLError: HTTPSConnectionPool(host='xxx.com', p ...

  7. 【python爬虫错误】Max retries exceeded with url

    Max retries exceeded with url 寻找可用的ip 寻找可用的ip import time from lxml import html # 把lxml是解析xml语言的库 et ...

  8. Python Max retries exceeded with url错误

    进行requests库学习的时候,报Max retries exceeded with url错误,网上查询说是,用下面这个解决方法没用,后来关闭了fiddler后发现就可以了,可能fiddler开的 ...

  9. Python 中错误 ConnectionError: Max retries exceeded with url

    文章目录 使用 Retry 对象进行回退重试 使用 try/except 语句在发生错误时不重试 禁用 SSL 证书验证 使用 time.sleep() 方法实现请求之间的延迟 重复请求直到成功响应 ...

最新文章

  1. TensorFlow XLA优化与Memory
  2. 设置vue运行npm run dev时候,项目在浏览器自动打开页面的方法
  3. C#中使用DirectX编程
  4. Cordova error:npm install -g ios-deploy
  5. vim中使用sed去除网上copy的源代码行号和空格
  6. 试从微型计算机的硬件组成角度谈谈单片机,单片机原理及应用课后习题参考答案1~6章...
  7. C++ Socket编程步骤
  8. windows修改远程桌面RDP连接数
  9. 2021年中国移动游戏行业深度洞察报告
  10. apache虚拟主机、日志轮询、日志统计、去版本优化
  11. JAR包使用方法指南
  12. css3中transform-style的用法详解
  13. c# winform word模板 利用WORD 书签 定位替换 生成 WORD
  14. wine qq2011beta4
  15. 重磅!Google推出了AI人体图像分割工具,惊呆了小伙伴.....
  16. 一个大学生屌丝心中的seo梦
  17. 欧姆龙PLC以太网与西门子WINCC通讯
  18. oracle中drop和delete,oracle中delete drop truncate的用法和区别
  19. 构建整车七自由度模型
  20. 搜索中词权重计算及实践

热门文章

  1. 出海先锋,平均15-16薪,上市公司汇量科技不容错过!
  2. 破茧成蝶-用户体验设计师的成长之路之一设计理念
  3. labview学习-for循环、while循环、循环自动索引、移位寄存器
  4. 耐心看的人早晚会成人上人
  5. TikTok卖成人产品?热门标签累计播放超1.8亿次,好评不断
  6. shell运行html文件路径,PowerShell文件系统(二)访问文件和目录
  7. Thymeleaf+SpringBoot+Mybatis实现的齐贤易游网旅游信息管理系统
  8. 路由器故障排除之-如何进入路由器的后台设置路由器
  9. 如何在Windows 10 1709版本中保存锁屏壁纸
  10. 2016MWC前瞻 华为爱立信中兴大战5G黑科技