Python requests.exceptions.SSLError:违反协议发生EOF [英] Python requests.exceptions.SSLError: EOF occurred in violation of protocol

查看:81
本文介绍了Python requests.exceptions.SSLError:违反协议发生EOF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将从提供 RESTful JSON API 的 ABB G13 网关中检索一些信息.API 由网关通过 https 端点托管.使用基本认证机制进行认证.然而所有的交通通过 SSL 层.

I would retrieve some information from an ABB G13 gateway that offer a RESTful JSON API. API is hosted by the gateway via https endpoint. Basic authentication mechanism is used for authentication. However all traffic goes through SSL layers.

在 linux 上使用命令:

On linux with command:

curl -s -k -X GET -u user:password https://host/meters/a_serial/power

一切顺利!

我正在尝试使用请求 2.8.1 和以下代码在 Python 2.7.10 中为 Windows 编写脚本:

I'm trying to write a script for windows in Python 2.7.10 with Requests 2.8.1 and with this code:

import requests
requests.get('https://host/meters/a_serial/power', auth=('user', 'password'))

我有这个错误:

Traceback (most recent call last):
  File "C:/Users/mzilio/PycharmProjects/pwrgtw/test.py", line 20, in <module>
    requests.get('https://host/meters/a_serial/power', auth=('user', 'password'))
  File "C:Python27libsite-packages
equestsapi.py", line 69, in get
    return request('get', url, params=params, **kwargs)
  File "C:Python27libsite-packages
equestsapi.py", line 50, in request
    response = session.request(method=method, url=url, **kwargs)
  File "C:Python27libsite-packages
equestssessions.py", line 468, in request
    resp = self.send(prep, **send_kwargs)
  File "C:Python27libsite-packages
equestssessions.py", line 576, in send
    r = adapter.send(request, **kwargs)
  File "C:Python27libsite-packages
equestsadapters.py", line 433, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: EOF occurred in violation of protocol (_ssl.c:590)

我已经寻找了一个解决方案,并尝试使用此代码进行修复:

I've searched for a solution and I've tried to fix with this code:

import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.poolmanager import PoolManager
import ssl

class MyAdapter(HTTPAdapter):
    def init_poolmanager(self, connections, maxsize, block=False):
        self.poolmanager = PoolManager(num_pools=connections,
                                       maxsize=maxsize,
                                       block=block,
                                       ssl_version=ssl.PROTOCOL_TLSv1)

s = requests.Session()
s.mount('https://', MyAdapter())
s.get('https://host/meters/a_serial/power')

但它对我不起作用,因为我收到此错误:

But it doesn't work for me cause I get this error:

Traceback (most recent call last):
  File "C:/Users/mzilio/PycharmProjects/pwrgtw/test.py", line 16, in <module>
    s.get('https://host/meters/a_serial/power')
  File "C:Python27libsite-packages
equestssessions.py", line 480, in get
    return self.request('GET', url, **kwargs)
  File "C:Python27libsite-packages
equestssessions.py", line 468, in request
    resp = self.send(prep, **send_kwargs)
  File "C:Python27libsite-packages
equestssessions.py", line 576, in send
    r = adapter.send(request, **kwargs)
  File "C:Python27libsite-packages
equestsadapters.py", line 433, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: EOF occurred in violation of protocol (_ssl.c:590)

我被这个问题困住了.有人可以帮助我吗?谢谢!

I'm stuck on this problem. Could someone help me? Thanks!

推荐答案

这个东西对我有用,只要确定是否安装了这些模块,如果没有安装它们,以下是:

This thing worked for me, just make sure whether these modules are installed or not, if not then install them, following are:

pip install ndg-httpsclient

pip install pyopenssl

pip install pyasn1

它删除了我的 SSLError:EOF 发生违反协议 (_ssl.c:590) 错误.

It removed my SSLError: EOF occurred in violation of protocol (_ssl.c:590) error.

希望有帮助.

这篇关于Python requests.exceptions.SSLError:违反协议发生EOF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆