gpt4 book ai didi

python - 使用 Mechanize 获取证书验证失败错误

转载 作者:太空狗 更新时间:2023-10-29 20:30:34 25 4
gpt4 key购买 nike

当我尝试运行代码时出现此错误:

  File "C:\Python27\lib\site-packages\mechanize\_urllib2_fork.py", line 1118, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)>

我在 Windows 8.1 上运行它,我可以打开带有请求的 https 没问题,但如果我使用 mechanize 则不会,我会收到错误。我的代码:

import mechanize


br = mechanize.Browser()
br.set_handle_robots(False)
br.addheaders = [('User-agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17')]

sign_in = br.open('https://www.amazon.com/ap/signin?_encoding=UTF8&openid.assoc_handle=usflex&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.mode=checkid_setup&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.com%2Fgp%2Fcss%2Fhomepage.html%3Fie%3DUTF8%26ref_%3Dnav_yam_ya')

br.select_form(name="signIn")
br["email"] = 'username'
br['password'] = 'password'
logged_in = br.submit()

最佳答案

如果没有最新版本的 python 或 mechanize,我发现以下解决方法对我有用。在调用 br.open() 之前添加如下代码:

import socket
import httplib
import ssl
def connect(self):
sock = socket.create_connection((self.host, self.port),
self.timeout, self.source_address)
if self._tunnel_host:
self.sock = sock
self._tunnel()

self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file, ssl_version=ssl.PROTOCOL_TLSv1)

httplib.HTTPSConnection.connect = connect

关于python - 使用 Mechanize 获取证书验证失败错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27736347/

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