当前位置: 代码迷 >> java >> urllib的SSL错误
  详细解决方案

urllib的SSL错误

热度:61   发布时间:2023-07-26 14:14:04.0

尝试使用BeautifulSoup4抓取网站时出现SSLError ,但我使用urllib只是打开了链接。 通过在我要从 JSoup的网站生成证书,我使其与JSoup (Java的刮板程序)一起在Java中工作。 是否可以在Python中使用该证书? 生成的文件称为jssecacerts,但重命名为cacerts。

这是我正在使用的代码:

def open(url, postdata=None):
    if postdata is not None:
        postdata = urlencode(postdata)
        postdata = postdata.encode('utf-8')
    return browser.open(url, postdata).read()

def login(i):
    cj = http.cookiejar.CookieJar()
    global browser
    browser = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))

    POST = {'timezoneOffset': TIMEZONEOFFSET,
            'userid': USERID,
            'pwd': PWD,
            }

    open(LOGIN_URL, POST)

错误:

Process Process-1:
Traceback (most recent call last):
Process Process-2:
Traceback (most recent call last):
  File "C:\Python34\lib\urllib\request.py", line 1182, in do_open
    h.request(req.get_method(), req.selector, req.data, headers)
  File "C:\Python34\lib\urllib\request.py", line 1182, in do_open
    h.request(req.get_method(), req.selector, req.data, headers)
  File "C:\Python34\lib\http\client.py", line 1088, in request
    self._send_request(method, url, body, headers)
  File "C:\Python34\lib\http\client.py", line 1088, in request
    self._send_request(method, url, body, headers)
  File "C:\Python34\lib\http\client.py", line 1126, in _send_request
    self.endheaders(body)
  File "C:\Python34\lib\http\client.py", line 1126, in _send_request
    self.endheaders(body)
  File "C:\Python34\lib\http\client.py", line 1084, in endheaders
    self._send_output(message_body)
  File "C:\Python34\lib\http\client.py", line 922, in _send_output
    self.send(msg)
  File "C:\Python34\lib\http\client.py", line 1084, in endheaders
    self._send_output(message_body)
  File "C:\Python34\lib\http\client.py", line 857, in send
    self.connect()
  File "C:\Python34\lib\http\client.py", line 1231, in connect
    server_hostname=server_hostname)
  File "C:\Python34\lib\http\client.py", line 922, in _send_output
    self.send(msg)
  File "C:\Python34\lib\http\client.py", line 857, in send
    self.connect()
  File "C:\Python34\lib\http\client.py", line 1231, in connect
    server_hostname=server_hostname)
  File "C:\Python34\lib\ssl.py", line 365, in wrap_socket
    _context=self)
  File "C:\Python34\lib\ssl.py", line 583, in __init__
    self.do_handshake()
  File "C:\Python34\lib\ssl.py", line 810, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed     (_ssl.c:600)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python34\lib\ssl.py", line 365, in wrap_socket
    _context=self)
  File "C:\Python34\lib\ssl.py", line 583, in __init__
    self.do_handshake()
  File "C:\Python34\lib\ssl.py", line 810, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:600)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python34\lib\multiprocessing\process.py", line 254, in _bootstrap
    self.run()
  File "C:\Python34\lib\multiprocessing\process.py", line 93, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Python34\lib\multiprocessing\process.py", line 254, in _bootstrap
    self.run()
  File "C:\Python34\lib\multiprocessing\process.py", line 93, in run
    self._target(*self._args, **self._kwargs)
  File "E:\ENG_CFR_PS_Map\eng_cfr_map.py", line 159, in main
    login(i)
  File "E:\ENG_CFR_PS_Map\eng_cfr_map.py", line 159, in main
    login(i)
  File "E:\ENG_CFR_PS_Map\eng_cfr_map.py", line 109, in login
    open(LOGIN_URL, POST)
  File "E:\ENG_CFR_PS_Map\eng_cfr_map.py", line 109, in login
    open(LOGIN_URL, POST)
  File "E:\ENG_CFR_PS_Map\eng_cfr_map.py", line 33, in open
    return browser.open(url, postdata).read()
  File "C:\Python34\lib\urllib\request.py", line 463, in open
    response = self._open(req, data)
  File "C:\Python34\lib\urllib\request.py", line 481, in _open
    '_open', req)
  File "E:\ENG_CFR_PS_Map\eng_cfr_map.py", line 33, in open
    return browser.open(url, postdata).read()
  File "C:\Python34\lib\urllib\request.py", line 441, in _call_chain
    result = func(*args)
  File "C:\Python34\lib\urllib\request.py", line 463, in open
    response = self._open(req, data)
  File "C:\Python34\lib\urllib\request.py", line 1225, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "C:\Python34\lib\urllib\request.py", line 1184, in do_open
    raise URLError(err)
  File "C:\Python34\lib\urllib\request.py", line 481, in _open
    '_open', req)
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED]     certificate verify failed (_ssl.c:600)>
  File "C:\Python34\lib\urllib\request.py", line 441, in _call_chain
    result = func(*args)
  File "C:\Python34\lib\urllib\request.py", line 1225, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "C:\Python34\lib\urllib\request.py", line 1184, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED]     certificate verify failed (_ssl.c:600)>

可以与代理相关。 如果您在代理后面,请尝试为HTTP_PROXY和HTTPS_PROXY设置系统环境变量。 根据您的建议,还应修改cacert。

代理通常类似于HTTP_PROXY =

等等

  相关解决方案