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

Python3.7.3环境下如何禁用urllib3的SSL证书校验?

在Python 3.7.3中禁用SSL证书校验(urllib.request与urllib3)

针对你使用的urllib.request.Request

注意:urllib.request.Request构造函数本身并不接收context参数,该参数需要传递给urllib.request.urlopen()方法。具体实现步骤如下:

  1. 导入ssl模块,创建不验证SSL证书的上下文
  2. 构造Request对象后,调用urlopen时传入该上下文

代码示例:

import urllib.request
import ssl

# 创建不校验SSL证书的上下文
ssl_context = ssl._create_unverified_context()

# 构造请求对象
request = urllib.request.Request(url, data=data, headers=headers)

# 发起请求时传入上下文
response = urllib.request.urlopen(request, context=ssl_context)

针对urllib3的实现方式

如果你确实需要使用urllib3禁用SSL校验,有两种常用方式:

方式1:请求时单独设置verify=False

import urllib3

# 关闭不安全请求的警告(可选,避免控制台输出警告信息)
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

# 初始化PoolManager并发起请求,设置verify=False
http = urllib3.PoolManager()
response = http.request(
    'POST',  # 根据你的请求方法调整,比如GET
    url,
    body=data,
    headers=headers,
    verify=False
)

方式2:全局设置verify=False

如果多个请求都需要禁用校验,可以在创建PoolManager时全局配置:

import urllib3

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

# 创建全局禁用SSL校验的PoolManager
http = urllib3.PoolManager(verify=False)
response = http.request('POST', url, body=data, headers=headers)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 16:33:28