简体   繁体   English

Python请求在某些站点上引发SSL错误

[英]Python Requests throws SSL Error on certain site

EDIT - FIXED tldr, semi-old version of python installed a couple years ago had ssl package that was not updated to handle newer SSL certificates. 编辑-已修复 tldr,几年前安装的python半旧版本,其ssl软件包尚未更新以处理较新的SSL证书。 After updating Python and making sure the ssl package was up to date, everything worked. 在更新Python并确保ssl软件包是最新的之后,一切正常。


I'm new to web scraping, and wanted to scrape a certain site, but for some reason I'm getting errors when using the Python's Requests package on this particular site. 我是Web抓取的新手,并且想抓取某个网站,但是由于某些原因,在该特定网站上使用Python的Requests包时出现错误。

I am working on secure login to scrape data from my user profile. 我正在进行安全登录,以从用户个人资料中抓取数据。 The login address can be found here: https://secure.funorb.com/m=weblogin/loginform.ws?mod=hiscore_fo&ssl=0&expired=0&dest= 登录地址可以在这里找到: https : //secure.funorb.com/m=weblogin/loginform.ws?mod=hiscore_fo&ssl=0&expired=0&dest=

I'm just trying to perform simple tasks at this point, like printing the text from a get request. 我现在只是在尝试执行简单的任务,例如从get请求中打印文本。 The following is my code. 以下是我的代码。

import requests

req = requests.get('https://secure.funorb.com/m=weblogin/loginform.ws?mod=hiscore_fo&ssl=0&expired=0&dest=',verify=False)
print req.text

When I run this, an error is thrown: 当我运行此命令时,将引发错误:

 File "/Library/Python/2.7/site-packages/requests/adapters.py", line 512, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: EOF occurred in violation of protocol (_ssl.c:590)

I've looked in this file to see what's going on. 我查看了该文件以查看发生了什么。 It seems the culprit is 罪魁祸首似乎是

    except (_SSLError, _HTTPError) as e:
        if isinstance(e, _SSLError):
            raise SSLError(e, request=request)
        elif isinstance(e, ReadTimeoutError):
            raise ReadTimeout(e, request=request)
        else:
            raise

I'm not really sure how to avoid this unfortunately, I'm kind of at my debugging limit here. 我不太确定如何避免这种情况,不幸的是,我在这里只能调试。

My code works just fine on other secure sites, such as https://bitbucket.org/account/signin/ . 我的代码在其他安全站点上也可以正常工作,例如https://bitbucket.org/account/signin/ I've looked at a ton of solutions on stack exchange and around the net, and a lot of people claimed adding in the optional argument "verify=False" should fix these types of SSL errors (ableit it's not the most secure way to do it). 我看过大量关于堆栈交换和网络的解决方案,很多人声称添加可选参数“ verify = False”应该可以修复这些类型的SSL错误(这不是最安全的方法)它)。 But as you can see from my code snippet, this isn't helping me. 但是,正如您从我的代码片段中看到的那样,这对我没有帮助。

If anyone can get this working/give advice on where to go it would be much appreciated. 如果有人可以从哪里获得工作上的建议,将不胜感激。

... lot of people claimed adding in the optional argument "verify=False" should fix these types of SSL errors ...很多人声称在可选参数“ verify = False”中添加应该修复这些类型的SSL错误

adding verify=False helps against errors when validating the certificate, but not against EOF from server, handshake errors or similar. 添加verify=False有助于防止在验证证书时出错,但不能防止来自服务器的EOF,握手错误或类似情况。

As can be seen from SSLLabs this specific server exhibits the behavior of simply closing the connection (ie "EOF occurred in violation of protocol") for clients which don't support TLS 1.2 with modern ciphers. SSLLabs可以看出,对于不支持使用现代密码的TLS 1.2的客户端,此特定服务器表现出仅关闭连接的行为(即“发生违反协议的EOF”)。 While you don't specify which SSL version you use I expect it to be a version less than OpenSSL 1.0.1, the first version of OpenSSL supporting TLS 1.2. 虽然您没有指定使用哪个SSL版本,但我希望它的版本低于OpenSSL 1.0.1(OpenTLS的第一个支持TLS 1.2的版本)。

Please check ssl.OPENSSL_VERSION for the version used in your code. 请检查ssl.OPENSSL_VERSION以获取代码中使用的版本。 If I'm correct your only fix is to upgrade the version of OpenSSL use by Python. 如果我是正确的,那么您唯一的解决办法就是升级Python使用的OpenSSL版本。 How this is done depends on your platform but there are existing posts about it, like Updating openssl in python 2.7 . 如何完成此操作取决于您的平台,但是有关于它的现有文章,例如在python 2.7中更新openssl

Seen it somewhere else. 在其他地方看到过它。 What if you try using sessions like this: 如果您尝试使用这样的会话怎么办:

import requests
sess = requests.Session()
adapter = requests.adapters.HTTPAdapter(max_retries = 20)
sess.mount('http://', adapter)

Then, change requests.get() with sess.get() 然后,更改requests.get()sess.get()

If you want to keep working with requests, maybe you need to install ndg-httpsclient package. 如果您想继续处理请求,则可能需要安装ndg-httpsclient软件包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM