Python请求ssl [Errno 1] _ssl.c:1428:错误:1408F10B:SSL例程:SSL3_GET_RECORD:版本号错误

时间:2014-01-30 16:58:25

标签: python ssl python-requests

我正在使用请求模块创建一个python应用程序。我最近添加了多处理来加快速度,但我开始遇到一些奇怪的错误,例如[Errno 1] _ssl.c:1428: error:1408F10B:SSL routines:SSL3_GET_RECORD:wrong version number[Errno 1] _ssl.c:1428: error:1408F119:SSL routines:SSL3_GET_RECORD:decryption failed or bad record mac。 代码如下所示:

def hometables_one(conn, request, s, hostname, payload, company):
    date1 = request.query.get('date1', '')      
    date2 = request.query.get('date2', '')

    prijmyCelk = 0;
    vydajeCelk = 0;
    neuhrPrijCelk = 0;
    neuhrVydCelk = 0;
    dph = 0;
    dbNazev = company['dbNazev'];
    nazev = company['nazev'];

    if date1 and date2:
        try:
            r = s.get("%s/c/%s/faktura-vydana/(duzpPuv between %s %s)/$sum.json" % (hostname, dbNazev, date1[0], date2[0]), params=payload, verify=False)
            r.raise_for_status()
        except requests.exceptions.RequestException as err:
            #response.write(ujson.dumps({ "success": False, "errors": { "reason": str(err)}}))
            #return response
            conn.send({ "success": False, "errors": { "reason": str(err)}})
            conn.close()
            return None

    else:
        try:
            r = s.get("%s/c/%s/faktura-vydana/$sum.json" % (hostname, dbNazev), params=payload, verify=False)
            r.raise_for_status()
        except requests.exceptions.RequestException as err:
            #response.write(ujson.dumps({ "success": False, "errors": { "reason": str(err)}}))
            #return response
            conn.send({ "success": False, "errors": { "reason": str(err)}})
            conn.close()
            return None

    obj_vydana = r.json()
    data_vydana = obj_vydana['winstrom']['sum']['sumDoklUcetni']['values']
    prijmyCelk = float(data_vydana['sumDoklCelkem']['value'])
    neuhrVydCelk = float(data_vydana['sumDoklZbyvaUh']['value'])
    dph_vydane = float(data_vydana['sumDoklDphCelk']['value'])


    if date1 and date2:
        try:
            r = s.get("%s/c/%s/faktura-prijata/(duzpPuv between %s %s)/$sum.json" % (hostname, dbNazev, date1[0], date2[0]), params=payload, verify=False)
            r.raise_for_status()
        except requests.exceptions.RequestException as err:
            #response.write(ujson.dumps({ "success": False, "errors": { "reason": str(err)}}))
            #return response
            conn.send({ "success": False, "errors": { "reason": str(err)}})
            conn.close()
            return None

    else:
        try:
            r = s.get("%s/c/%s/faktura-prijata/$sum.json" % (hostname, dbNazev), params=payload, verify=False)
            r.raise_for_status()
        except requests.exceptions.RequestException as err:
            #response.write(ujson.dumps({ "success": False, "errors": { "reason": str(err)}}))
            #return response
            conn.send({ "success": False, "errors": { "reason": str(err)}})
            conn.close()
            return None

    obj_prijata = r.json();
    data_prijata = obj_prijata['winstrom']['sum']['sumDoklUcetni']['values']
    vydajeCelk = float(data_prijata['sumDoklCelkem']['value'])
    neuhrPrijCelk = float(data_prijata['sumDoklZbyvaUh']['value'])
    dph_prijate = float(data_prijata['sumDoklDphCelk']['value'])

    if prijmyCelk != 0:
        result = {
            "corporation": nazev,
            "dbName": dbNazev,
            "prijmyCelk": "%s €" % prijmyCelk,
            "nakladyCelk": "%s €" % vydajeCelk,
            "ziskCelk": "%s €" % (prijmyCelk-vydajeCelk),
            "marzaCelk": ((prijmyCelk-vydajeCelk)/prijmyCelk*100),
            "neuhrVydCelk": "%s €" % neuhrVydCelk,
            "neuhrPrijCelk": "%s €" % neuhrPrijCelk,
            "dph": "%s €" % (dph_vydane-dph_prijate),
        }
    else:
        result = {
            "corporation": nazev,
            "dbName": dbNazev,
            "prijmyCelk": "%s €" % prijmyCelk,
            "nakladyCelk": "%s €" % vydajeCelk,
            "ziskCelk": "%s €" % (prijmyCelk-vydajeCelk),
            "marzaCelk": 0,
            "neuhrVydCelk": "%s €" % neuhrVydCelk,
            "neuhrPrijCelk": "%s €" % neuhrPrijCelk,
            "dph": "%s €" % (dph_vydane-dph_prijate),
        }
    conn.send(result)
    conn.close()
    return None
#####################################################################################

def hometables(request):
    s = requests.Session()
    response = HTTPResponse()
    hostname = request.query.get('hostname', '')[0]
    auth = request.query.get('auth', '')[0]
    p_queue = []
    result = []

    json_r = {"success": True}
    payload = {'authSessionId': request.query.get('auth', '')[0]}
    try:
        r = s.get("%s/c.json" % hostname, params=payload, verify=False)
        r.raise_for_status()
    except requests.exceptions.RequestException as err:
        response.write(ujson.dumps({ "success": False, "errors": { "reason": str(err)}}))
        return response

    obj = r.json()
    data = obj['companies']['company']
    data = make_list(data)

    parent_conn, child_conn = Pipe()
    for company in data:
        p_queue.append(Process(target=hometables_one, args=(child_conn, request, s, hostname, payload, company))) #create a new process with hometables_one function
        p_queue[-1].start()

    for p in p_queue:
        received_data = parent_conn.recv()
        if "success" not in received_data:
            result.append(received_data)s
            p.join()
        else:
            response.write(ujson.dumps(received_data)) #error in hometables_one function
            return response

    json_r["data"] = result
    response.write(ujson.dumps(json_r))
    return response

在这部分

    try:
        r = s.get("%s/c.json" % hostname, params=payload, verify=False)
        r.raise_for_status()
    except requests.exceptions.RequestException as err:
        response.write(ujson.dumps({ "success": False, "errors": { "reason": str(err)}}))
        return response

    obj = r.json()
    data = obj['companies']['company']
            data = make_list(data)

我收到了当前系统中所有公司的JSON请求,然后我为每个公司运行了hometables_one函数。最终的data可能如下所示:

[{"createDt":"2014-01-28T00:00:00+01:00","dbNazev":"sveatlo","id":"4","licenseGroup":"null","nazev":"Sveatlo","show":"true","stavEnum":"ESTABLISHED","watchingChanges":"false"}]

或者像这样:

[{"createDt":"2014-01-28T00:00:00+01:00","dbNazev":"sveatlo","id":"4","licenseGroup":"null","nazev":"Sveatlo","show":"true","stavEnum":"ESTABLISHED","watchingChanges":"false"},{"createDt":"2014-01-28T00:00:00+01:00","dbNazev":"sveatlo1","id":"4","licenseGroup":"null","nazev":"Sveatlo1","show":"true","stavEnum":"ESTABLISHED","watchingChanges":"false"}]

在第一种情况下,如果只有一个项目hometables_one函数运行没有任何问题,但添加另一个项会导致错误[Errno 1] _ssl.c:1428: error:1408F10B:SSL routines:SSL3_GET_RECORD:wrong version number[Errno 1] _ssl.c:1428: error:1408F119:SSL routines:SSL3_GET_RECORD:decryption failed or bad record mac。另一件事是当我在没有多处理的情况下运行代码时,即hometable_one函数的内容在hometables函数的for循环中,它运行没有任何问题。 为什么我会收到这些错误?请问有人帮帮我吗?

感谢您的回答

1 个答案:

答案 0 :(得分:2)

我遇到过类似的问题。我认为此错误是多个进程尝试访问同一SSL连接的结果。您可以尝试的是在每个进程触发请求之前为其引入随机延迟:

time.sleep(random.randrange(10))
r = s.get("%s/c.json" % hostname, params=payload, verify=False)