本文介绍了Python GET 请求上的 SSL 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对此网址执行简单的 GET 请求:链接

I am trying to do a simple GET request to this url: link

这是我已经用于其他网址和作品的基本 python 代码.

Here's the basic python code which I already use for other urls and works.

url = 'http://www.bbvafrances.com.ar/francesGo2-Portal/institucional/busqueda.do?m=&page=1'
r = requests.get(url)
print r.text

问题是使用这个特定的 url 我得到一个 SSL 错误:

The thing is that with this particular url I get an SSL error:

Traceback (most recent call last):
  File "frances.py", line 134, in <module>
    r = requests.get(url,verify=False)
  File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 55, in get
    return request('get', url, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 44, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 335, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 454, in send
    history = [resp for resp in gen] if allow_redirects else []
  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 144, in   resolve_redirects
    allow_redirects=False,
  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 438, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/adapters.py", line 331, in send
raise SSLError(e)
 requests.exceptions.SSLError: [Errno 1] _ssl.c:504: error:1408F119:SSL     routines:SSL3_GET_RECORD:decryption failed or bad record mac

关于这个特定 url 发生了什么导致请求崩溃的任何想法?我尝试添加自定义标题,但仍然没有运气.

Any ideas on what is going on with this particular url that makes the request crash?I tried adding custom headers but still no luck.

提前致谢.

现在我正在尝试使用这种建议作为答案的 SSL 方式.

Now I'm trying with this SSL way suggested as an answer.

url = 'https://www.bbvafrances.com.ar/francesGo2-Portal/institucional/busqueda.do?m=&page=1'
s = requests.Session()
s.mount(url, SSLAdapter(ssl.PROTOCOL_SSLv3))
r = s.get(url)
print r.text

这是新的跟踪:(几乎与第一个相同)

This is the new trace: (almost identical to the first one)

Traceback (most recent call last):
  File "frances.py", line 138, in <module>
    r = s.get(url)
  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 463, in get
    return self.request('GET', url, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 451, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 557, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/adapters.py", line 420, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: [Errno 1] _ssl.c:504: error:1408F119:SSL         routines:SSL3_GET_RECORD:decryption failed or bad record mac

推荐答案

问题是,服务器宣布支持 TLS1.0,但如果您实际尝试使用 TLS1.0 与它对话,它就会中断.相反,您必须使用 SSL3.

The problem is, that the server announces support for TLS1.0 but if you actually try to talk to it using TLS1.0 it will break. Instead you have to use SSL3.

您可以使用 requests_toolbelt 包中包含的 SSLAdapter 来实现:http://toolbelt.readthedocs.org/en/latest/user.html#ssladapter.

You can do so by using the SSLAdapter included with the requests_toolbelt package:http://toolbelt.readthedocs.org/en/latest/user.html#ssladapter.

然后使用 ssl.PROTOCOL_SSLv3 而不是文档中显示的那个.

Then use ssl.PROTOCOL_SSLv3 instead of the one shown in the documentation.

这篇关于Python GET 请求上的 SSL 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 00:38