You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中使用urllib绕过SSL证书时遇域名解析错误求解决

问题:Python urllib绕过SSL证书访问HTTPS站点时出现域名解析错误

尝试使用Python的urllib库绕过SSL证书访问HTTPS站点,但所有目标站点均返回相同错误,以下是测试代码及报错信息,两段代码问题一致,寻求解决方案。

代码1

import urllib.request as ur
import urllib.parse, urllib.error, ssl


url_is = 'https://finance.yahoo.com'
url_google = 'https://www.google.co.in'

req = ur.Request(url_google)

ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

info = ur.urlopen(req, context=ctx).read()
Message.Chat.SendMessage("" + info)

报错信息

Traceback (most recent call last):
  File "/usr/lib/python3.8/urllib/request.py", line 1354, in do_open
    h.request(req.get_method(), req.selector, req.data, headers,
  File "/usr/lib/python3.8/http/client.py", line 1256, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/lib/python3.8/http/client.py", line 1302, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.8/http/client.py", line 1251, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.8/http/client.py", line 1011, in _send_output
    self.send(msg)
  File "/usr/lib/python3.8/http/client.py", line 951, in send
    self.connect()
  File "/usr/lib/python3.8/http/client.py", line 1418, in connect
    super().connect()
  File "/usr/lib/python3.8/http/client.py", line 922, in connect
    self.sock = self._create_connection(
  File "/usr/lib/python3.8/socket.py", line 787, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "/usr/lib/python3.8/socket.py", line 918, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -3] Temporary failure in name resolution

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "main.py", line 10, in <module>
    info = ur.urlopen(req, context=gcontext).read()
  File "/usr/lib/python3.8/urllib/request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.8/urllib/request.py", line 525, in open
    response = self._open(req, data)
  File "/usr/lib/python3.8/urllib/request.py", line 542, in _open
    result = self._call_chain(self.handle_open, protocol, protocol +
  File "/usr/lib/python3.8/urllib/request.py", line 502, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.8/urllib/request.py", line 1397, in https_open
    return self.do_open(http.client.HTTPSConnection, req,
  File "/usr/lib/python3.8/urllib/request.py", line 1357, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [Errno -3] Temporary failure in name resolution>

代码2

from bs4 import BeautifulSoup
import urllib.request as ur
import urllib.parse, urllib.error, ssl

ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE


url_is = 'https://finance.yahoo.com'
url_google = 'https://www.google.co.in'

read_data = urllib.request.urlopen(url_google, context=ctx).read() 
soup_is= BeautifulSoup(read_data,'lxml')

解决方案

错误根源

报错信息中的socket.gaierror: [Errno -3] Temporary failure in name resolution表明问题出在域名解析失败,和SSL证书绕过无关。你的代码中SSL绕过的逻辑是正确的,但当前运行环境无法将目标域名解析为IP地址。

排查与解决步骤

  1. 验证网络连通性
    • 在当前运行环境的终端中执行ping www.google.co.in或ping finance.yahoo.com,检查是否能正常解析域名并收到响应。如果ping不通,说明网络本身存在问题。
  2. 检查DNS配置
    • 如果是服务器环境,查看DNS配置文件(如/etc/resolv.conf),尝试更换公共DNS服务器(比如谷歌的8.8.8.8、8.8.4.4或国内的114.114.114.114)。
  3. 修正代码中的小问题
    • 代码1中最后一行的Message.Chat.SendMessage是自定义模块调用,确保当前环境已正确导入该模块,否则会引发额外错误。
    • 代码2中导入了urllib.request as ur,但后续使用了urllib.request.urlopen,可以统一改为ur.urlopen,避免冗余调用。
  4. 简化测试代码
    先运行以下最简代码验证网络和解析是否正常,再添加SSL绕过和其他逻辑:
    import urllib.request as ur
    import ssl
    
    ctx = ssl.create_default_context()
    ctx.check_hostname = False
    ctx.verify_mode = ssl.CERT_NONE
    
    try:
        response = ur.urlopen('https://www.google.com', context=ctx)
        print("访问成功,状态码:", response.getcode())
    except Exception as e:
        print("错误详情:", str(e))
    

内容的提问来源于stack exchange,提问作者Sujay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 08:05:25