python requests报Max retries exceeded with url异常

项目中有个模块是定时获取设备状态的,通过设备的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

 

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

 

转载于:https://www.cnblogs.com/lihao7/p/9183707.html

你可能感兴趣的:(python,json)