gpt4 book ai didi

python - 请求 ssl.SSLEOFError : EOF occurred in violation of protocol (_ssl. c:777)

转载 作者:行者123 更新时间:2023-11-28 19:04:57 35 4
gpt4 key购买 nike

我正在尝试从特定网址获取图像数据(请参阅下面的代码)。他们的安全性已经过时(请参阅下面的 SSL 报告),但无论如何我都需要连接到它。我可以使用我的浏览器获取图像。

这是我的尝试:

import requests
url = 'https://www.bestseller.com/webseller/psp.show_picture?picturesId=2367737&thumb=false'
requests.get(url)

我得到的错误是:

...
File "/path/Python.framework/Versions/3.6/lib/python3.6/ssl.py", line 689, in do_handshake
self._sslobj.do_handshake()
ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:777)

我在添加参数 verify=False 时遇到同样的错误。

我已经使用 pip install requests[security] 安装了 requests。这是相关的 pip freeze 输出:

asn1crypto==0.24.0
certifi==2017.11.5
cryptography==2.1.4
ndg-httpsclient==0.4.3
pyasn1==0.4.2
pyOpenSSL==17.5.0
requests==2.8.1
urllib3==1.22

其他配置打印:

>>> import ssl
>>> print(ssl.OPENSSL_VERSION)
OpenSSL 1.0.2n 7 Dec 2017
>>> from cryptography.hazmat.backends.openssl.backend import backend
>>> print(backend.openssl_version_text())
OpenSSL 1.1.0g 2 Nov 2017

有没有一种方法可以禁用设置/验证 SSL,或者如果没有,我如何找出我需要添加的密码以及如何执行此操作?

当我尝试编写自己的适配器时:

from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.ssl_ import create_urllib3_context

# This is the 2.11 Requests cipher string, containing 3DES.
CIPHERS = (
'ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+HIGH:'
'DH+HIGH:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+HIGH:RSA+3DES:!aNULL:'
'!eNULL:!MD5'
)

class DESAdapter(HTTPAdapter):
def init_poolmanager(self, *args, **kwargs):
context = create_urllib3_context(ciphers=CIPHERS)
kwargs['ssl_context'] = context
return super(DESAdapter, self).init_poolmanager(*args, **kwargs)

s = requests.Session()
s.mount(url, DESAdapter())

我收到以下错误:

AttributeError: 'super' object has no attribute 'init_poolmanager'

https://www.ssllabs.com/ssltest/analyze.html?d=www.bestseller.com

最佳答案

与此同时,我使用库 pycurl 规避了这个问题。

import urllib.request
from io import BytesIO
from urllib.error import URLError

import pycurl
from django.core.files import File
from django.core.files.base import ContentFile


def _create_attachment_from_url(url: str):
"""
Fetch the image from the given URL and save it as an attachment
"""

try:
file = _fetch_file_using_urllib(url)
except URLError:
file = _fetch_file_using_pycurl(url)

return file


def _fetch_file_using_urllib(url: str) -> (File, str):
response = urllib.request.urlretrieve(url)
contents = open(response[0], 'rb')
file = File(contents)
return file


def _fetch_file_using_pycurl(url: str) -> (File, str):
buffer = BytesIO()
c = pycurl.Curl()
try:
c.setopt(c.URL, url)
c.setopt(c.WRITEDATA, buffer)
c.perform()
contents = buffer.getvalue()
file = ContentFile(contents)
finally:
c.close()
return file

关于python - 请求 ssl.SSLEOFError : EOF occurred in violation of protocol (_ssl. c:777),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48112400/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com