具有证书 SSLv3 警报握手失败的 URLLib
URLLib with cert SSLv3 alert handshake failure
我正在使用 Python 3.7.3 和 requests_pkcs12 库来抓取我必须通过证书和密码的网站,然后从页面上的链接下载并提取 zip 文件。我的第一部分工作正常。但是当我尝试使用 urllib 读取文件时,出现错误。
import urllib.request
from bs4 import BeautifulSoup
import requests
from requests_pkcs12 import get
# get page and setup BeautifulSoup
# r = requests.get(url) # old non-cert method
r = get(url, pkcs12_filename=certpath, pkcs12_password=certpwd)
# find zip files to download
soup = BeautifulSoup(r.content, "html.parser")
# Read files
i = 1
for td in soup.find_all(lambda tag: tag.name=='td' and tag.text.strip().endswith('DAILY.zip')):
link = td.find_next('a')
print(td.get_text(strip=True), link['href'] if link else '') # good
zipurl = 'https:\my.downloadsite.com" + link['href'] if link else ''
print (zipurl) # good
# Read zip file from URL
url = urllib.request.urlopen(zipurl) # ERROR on this line SSLv3 alert handshake failure
zippedData = url.read()
我看到过关于如何处理这个问题的 Python 2.x 的各种旧帖子,但想知道现在最好的方法是什么,在 Python 3 中有新的库.7.x.
下面是错误的堆栈跟踪。
答案是不使用 urllib,而是使用允许传递给它的 pfx 和密码的相同请求替换。
最后两行:
url = urllib.request.urlopen(zipurl) # ERROR on this line SSLv3 alert handshake failure
zippedData = url.read()
应替换为:
url = get(zipurl, pkcs12_filename=certpath, pkcs12_password=certpwd)
zippedData = url.content
我正在使用 Python 3.7.3 和 requests_pkcs12 库来抓取我必须通过证书和密码的网站,然后从页面上的链接下载并提取 zip 文件。我的第一部分工作正常。但是当我尝试使用 urllib 读取文件时,出现错误。
import urllib.request
from bs4 import BeautifulSoup
import requests
from requests_pkcs12 import get
# get page and setup BeautifulSoup
# r = requests.get(url) # old non-cert method
r = get(url, pkcs12_filename=certpath, pkcs12_password=certpwd)
# find zip files to download
soup = BeautifulSoup(r.content, "html.parser")
# Read files
i = 1
for td in soup.find_all(lambda tag: tag.name=='td' and tag.text.strip().endswith('DAILY.zip')):
link = td.find_next('a')
print(td.get_text(strip=True), link['href'] if link else '') # good
zipurl = 'https:\my.downloadsite.com" + link['href'] if link else ''
print (zipurl) # good
# Read zip file from URL
url = urllib.request.urlopen(zipurl) # ERROR on this line SSLv3 alert handshake failure
zippedData = url.read()
我看到过关于如何处理这个问题的 Python 2.x 的各种旧帖子,但想知道现在最好的方法是什么,在 Python 3 中有新的库.7.x.
下面是错误的堆栈跟踪。
答案是不使用 urllib,而是使用允许传递给它的 pfx 和密码的相同请求替换。
最后两行:
url = urllib.request.urlopen(zipurl) # ERROR on this line SSLv3 alert handshake failure
zippedData = url.read()
应替换为:
url = get(zipurl, pkcs12_filename=certpath, pkcs12_password=certpwd)
zippedData = url.content