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

如何在Windows CLI中将.crt文件追加到.pem文件(解决Python requests SSL问题)

解决方案:Windows CLI下追加自定义证书到certifi证书库

问题原因分析

  1. copy命令无效:Windows的copy file1+file2 dest语法不允许目标文件是其中一个源文件,读取源文件时无法同时写入,导致命令执行失败。
  2. type命令乱码:自定义.crt证书编码可能与certifi的cacert.pem(UTF-8编码)不匹配(比如Windows导出的证书常为UTF-16),直接追加会引发编码冲突产生乱码。
  3. 第三个命令错误:Windows原生CLI无cp命令(需用copy),且$(...)语法仅在PowerShell中生效,CMD下无法识别,导致路径参数为空报错。

推荐解决方案(PowerShell 一行命令)

用PowerShell处理编码匹配问题,确保追加的证书以UTF-8编码写入certifi的证书文件:

Get-Content (pip config get global.cert) -Encoding UTF8 | Add-Content (python -m certifi) -Encoding UTF8
  • 若证书为其他编码(如Unicode),将-Encoding UTF8替换为对应编码(如-Encoding Unicode)即可。

CMD下兼容方案

如需在CMD中执行,可嵌套PowerShell命令解决编码问题:

powershell -Command "Get-Content (pip config get global.cert) -Encoding UTF8 | Add-Content (python -m certifi) -Encoding UTF8"

更可靠的长期方案(避免certifi更新覆盖)

直接修改certifi的证书文件会在包更新时被覆盖,建议通过环境变量指定合并后的证书:

  1. 复制certifi默认证书到自定义路径:
Copy-Item (python -m certifi) -Destination "C:\custom_certs\cacert.pem"
  1. 追加自定义证书:
Get-Content (pip config get global.cert) -Encoding UTF8 | Add-Content "C:\custom_certs\cacert.pem" -Encoding UTF8
  1. 设置环境变量(永久生效):
    • CMD:
      setx REQUESTS_CA_BUNDLE "C:\custom_certs\cacert.pem"
      
    • PowerShell:
      [Environment]::SetEnvironmentVariable("REQUESTS_CA_BUNDLE", "C:\custom_certs\cacert.pem", "User")
      

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 09:55:15