python 错误 Could not find a suitable TLS CA certificate bundle, invalid path 解决方法

最近用python写了一个爬虫的代码,源程序可以正常运行,但是打包会就会报错"Could not find a suitable TLS CA certificate bundle":

查了一下原因,因为爬虫都要用到

import requests

requests库打包之后就会出现"Could not find a suitable TLS CA certificate bundle"问题,、打包之后按默认路径无法找到cacert.pem文件

其实在打包程序的时候很容易出现这样的问题,requests库中还包含很多其它相关库和文件,在源程序中会自动调用,不用在开头引用,但是打包之后exe程序默认只调用requests。

第一想法是既然在这个目录下找不到这个文件,那我手动将这个文件拷过去是不是就可以了,将 python安装路径\Lib\site-packages\requests\cacert.pem文件复制,准备拷贝时发现ME12121962文件(打包生成的文件)下并没有requests文件,我自己建了一个:

结果仍然报错

解决办法:手动将所需库/文件添加到程序的引用中

1.将 python安装路径\Lib\site-packages\requests\cacert.pem复制到包含您的exe的目录

复制到exe所在文件下:

2.在代码中加入以下命令进行调用:

import os
import sys
os.environ['REQUESTS_CA_BUNDLE'] =  os.path.join(os.path.dirname(sys.argv[0]), 'cacert.pem')

然后重新打包就可以了

 

 

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Certainly, here's an example program that generates an RSA key pair, creates an X.509 certificate with the public key, and writes the key pair and certificate to files: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include "mbedtls/pk.h" #include "mbedtls/entropy.h" #include "mbedtls/ctr_drbg.h" #include "mbedtls/x509_crt.h" #include "mbedtls/x509_csr.h" #include "mbedtls/x509write_crt.h" #define KEY_SIZE 2048 #define CERT_DAYS 365 int main(int argc, char *argv[]) { int ret = 1; mbedtls_pk_context key; mbedtls_entropy_context entropy; mbedtls_ctr_drbg_context ctr_drbg; mbedtls_x509write_crt crt; mbedtls_x509_crt cacert; char key_file[] = "key.pem"; char cert_file[] = "cert.pem"; char cacert_file[] = "cacert.pem"; // Initialize contexts mbedtls_pk_init(&key); mbedtls_entropy_init(&entropy); mbedtls_ctr_drbg_init(&ctr_drbg); mbedtls_x509write_crt_init(&crt); mbedtls_x509_crt_init(&cacert); // Seed the random number generator ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0); if (ret != 0) { printf("error: mbedtls_ctr_drbg_seed returned %d\n", ret); goto exit; } // Generate an RSA key pair ret = mbedtls_pk_setup(&key, mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)); if (ret != 0) { printf("error: mbedtls_pk_setup returned %d\n", ret); goto exit; } ret = mbedtls_rsa_gen_key(mbedtls_pk_rsa(key), mbedtls_ctr_drbg_random, &ctr_drbg, KEY_SIZE, 65537); if (ret != 0) { printf("error: mbedtls_rsa_gen_key returned %d\n", ret); goto exit; } // Write the key pair to a file ret = mbedtls_pk_write_key_pem(&key, key_file, NULL, NULL, 0); if (ret != 0) { printf("error: mbedtls_pk_write_key_pem returned %d\n", ret); goto exit; } // Set up the X.509 certificate mbedtls_x509write_crt_set_version(&crt, MBEDTLS_X509_CRT_VERSION_3); mbedtls_x509write_crt_set_md_alg(&crt, MBEDTLS_MD_SHA256); mbedtls_x509write_crt_set_subject_name(&crt, "CN=example.com,O=Example Organization,C=US"); mbedtls_x509write_crt_set_issuer_name(&crt, "CN=example.com,O=Example Organization,C=US"); mbedtls_x509write_crt_set_validity(&crt, "20200101000000", "20301231235959"); mbedtls_x509write_crt_set_basic_constraints(&crt, 0, -1); mbedtls_x509write_crt_set_key_usage(&crt, MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN); mbedtls_x509write_crt_set_ns_cert_type(&crt, MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER | MBEDTLS_X509_NS_CERT_TYPE_EMAIL); mbedtls_x509write_crt_set_subject_key(&crt, &key); mbedtls_x509write_crt_set_issuer_key(&crt, &key); // Write the X.509 certificate to a file ret = mbedtls_x509write_crt_pem(&crt, cert_file, NULL, NULL, 0); if (ret != 0) { printf("error: mbedtls_x509write_crt_pem returned %d\n", ret); goto exit; } // Load the trusted CA certificate ret = mbedtls_x509_crt_parse_file(&cacert, cacert_file); if (ret != 0) { printf("error: mbedtls_x509_crt_parse_file returned %d\n", ret); goto exit; } // Verify the X.509 certificate against the trusted CA certificate ret = mbedtls_x509_crt_verify(&crt, &cacert, NULL, NULL, NULL); if (ret != 0) { printf("error: mbedtls_x509_crt_verify returned %d\n", ret); goto exit; } printf("RSA key pair and X.509 certificate generated successfully!\n"); exit: mbedtls_x509_crt_free(&cacert); mbedtls_x509write_crt_free(&crt); mbedtls_ctr_drbg_free(&ctr_drbg); mbedtls_entropy_free(&entropy); mbedtls_pk_free(&key); return ret; } ``` This program uses the mbedtls `pk` module to generate an RSA key pair, the `ctr_drbg` module to generate random numbers for the key pair, and the `x509write_crt` module to create an X.509 certificate with the public key. It also loads a trusted CA certificate and verifies the generated certificate against it. The key pair and certificate are written to files `key.pem` and `cert.pem`, respectively.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值