Python请求requests.exceptions.SSLError:[Errno 8]

我在Ubuntu 12.10上使用OpenSSL 1.0.1c,python 2.7.3,请求 1.0.3和1.0.4(尝试过两者),并尝试使用以下代码连接到url变量中的网站。


def SendInitialRequest(xmlmessage, redirecturl):

    url = 'https://centineltest.cardinalcommerce.com/maps/txns.asp'


    payload = 'cmpi_msg=' + ET.tostring(xmlmessage)

    headers = {

        'Content-Type': 'application/x-www-form-urlencoded',

    }

    r = requests.post(url, data=payload, headers=headers, verify=None)

    print r.text

它会引发以下错误:


Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

  File "clams/libs/centinel/thinclient.py", line 134, in SendInitialRequest

    r = requests.post(url, data=payload, headers=headers, verify=None)

  File "/home/jasonamyers/.virtualenv/clams/lib/python2.7/site-packages/requests/api.py", line 87, in post

    return request('post', url, data=data, **kwargs)

  File "/home/jasonamyers/.virtualenv/clams/lib/python2.7/site-packages/requests/api.py", line 44, in request

    return session.request(method=method, url=url, **kwargs)

  File "/home/jasonamyers/.virtualenv/clams/lib/python2.7/site-packages/requests/sessions.py", line 269, in request

    resp = self.send(prep, stream=stream, timeout=timeout, verify=verify, cert=cert, proxies=proxies)

  File "/home/jasonamyers/.virtualenv/clams/lib/python2.7/site-packages/requests/sessions.py", line 364, in send

    r = adapter.send(request, **kwargs)

  File "/home/jasonamyers/.virtualenv/clams/lib/python2.7/site-packages/requests/adapters.py", line 163, in send

    raise SSLError(e)

requests.exceptions.SSLError: [Errno 8] _ssl.c:504: EOF occurred in violation of protocol

尝试使用openssl连接会返回以下内容:


$ openssl s_client -connect centineltest.cardinalcommerce.com:443

CONNECTED(00000003)

140019346777760:error:140790E5:SSL routines:SSL23_WRITE:ssl handshake failure:s23_lib.c:177:

---

no peer certificate available

---

No client certificate CA names sent

---

SSL handshake has read 0 bytes and written 226 bytes

---

New, (NONE), Cipher is (NONE)

Secure Renegotiation IS NOT supported

Compression: NONE

Expansion: NONE

---



森栏
浏览 1999回答 3
3回答

红糖糍粑

从请求问题页面将此处重新发布给其他人:请求'不支持在版本1之前执行此操作。在版本1之后,您应该继承HTTPAdapter,如下所示:from requests.adapters import HTTPAdapterfrom requests.packages.urllib3.poolmanager import PoolManagerimport sslclass MyAdapter(HTTPAdapter):&nbsp; &nbsp; def init_poolmanager(self, connections, maxsize, block=False):&nbsp; &nbsp; &nbsp; &nbsp; self.poolmanager = PoolManager(num_pools=connections,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;maxsize=maxsize,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;block=block,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ssl_version=ssl.PROTOCOL_TLSv1)完成后,您可以这样做:import requestss = requests.Session()s.mount('https://', MyAdapter())然后,通过该会话对象的任何请求都将使用TLSv1。

宝慕林4294392

安装“安全”软件包附加功能requests为我解决:sudo apt-get install libffi-devsudo pip install -U requests [security]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python