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

Python requests基础命令执行报错,求问题原因与解决方法

SSL Certificate Verification Failed: Fix for Self-Signed Certificates

Hey there, let's break down what's going on here and how to fix it!

The Problem

The error message clearly points to the core issue:

ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain

This means the website somedomain.xyz is using a self-signed SSL certificate instead of one issued by a globally trusted Certificate Authority (CA). By default, the requests library blocks connections to such sites—this is a security safeguard to prevent man-in-the-middle attacks, since self-signed certs don't have a trusted chain to confirm the site's legitimate identity.

Fix Options

1. Temporary Testing Fix (Not for Production!)

If you're just experimenting and don't need full security for this test, you can disable certificate verification entirely. You'll also want to suppress the warning that comes with this risky setting:

import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning

# Turn off the insecure request warning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

result = requests.get("https://somedomain.xyz/start_page.html", verify=False)
print(result.status_code)

⚠️ Critical Note: Never use this in production! Disabling verification strips away the SSL security layer, leaving your requests open to interception and tampering.

2. Proper Production Fix (Trust the Certificate)

The secure, long-term solution is to add the site's self-signed certificate to your trusted list:

  1. Export the certificate: Use your browser to visit https://somedomain.xyz, download the certificate file (usually saved as .pem or .crt format).
  2. Reference the certificate in your request:
import requests

# Replace "/path/to/your/certificate.pem" with the actual file path on your machine
result = requests.get("https://somedomain.xyz/start_page.html", verify="/path/to/your/certificate.pem")
print(result.status_code)

Alternatively, you can add the certificate to your system's trusted root CA store for global recognition:

  • Windows: Use the Certificate Manager to import the certificate into the "Trusted Root Certification Authorities" store.
  • Linux: Copy the certificate to /usr/local/share/ca-certificates/, then run sudo update-ca-certificates.
  • macOS: Double-click the certificate file, add it to the "System" keychain, then mark it as trusted in the certificate settings.

Once added, Python will automatically recognize the certificate, and you won't need to specify the verify parameter anymore.


内容的提问来源于stack exchange,提问作者Tomáš

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 18:42:39