我正在尝试从特定网址获取图片数据(请参阅下面的代码)。他们的安全性已经过时了(请参阅下面的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
时,我收到同样的错误。
我使用requests
安装了pip install requests[security]
。这是相关的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'
答案 0 :(得分:0)
与此同时,我使用库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