python requests证书问题解决

yipeiwu_com5年前Python基础

用requests包请求https的网站时,我们偶尔会遇到证书问题。也就是常见的SSLerror,遇到这种问题莫慌莫慌。

这里没有找到合适的网站去报SSL证书的错误,所以就假装请求了一个https的网站,然后给报了SSLerror了,然后下面是解决方法

可以直接关闭验证ssl证书

import requests
'''
  :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
  :param verify: (optional) Either a boolean, in which case it controls whether we verify
      the server's TLS certificate, or a string, in which case it must be a path
      to a CA bundle to use. Defaults to ``True``.
      
'''
r = requests.get('https://kyfw.12306.cn',verify=False)

print(r.text)

这种方式直接在函数里面加如verify改变Ture或者False即可,因为post与get调用的都为request()函数,所以get与post都一样。

如果这种方式奏效就用这种方式,如果不奏效就用下面的一种

import requests
'''
  :param verify: (optional) Either a boolean, in which case it controls whether we verify
      the server's TLS certificate, or a string, in which case it must be a path
      to a CA bundle to use. Defaults to ``True``.
      
'''
## 证书路径
cert = '../cert/test.pem'

r = requests.get('https://kyfw.12306.cn',verify=cert)
print(r.text)

就用这种,直接把证书的路径丢给verify,请求即可

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

hmac模块生成加入了密钥的消息摘要详解

hmac模块生成加入了密钥的消息摘要详解

hmac模块 hmac模块用于生成HMAC码。这个HMAC码可以用于验证消息的完整性,其原理也很简单,就是一种加入了密钥的消息摘要,相比起MAC更加安全。JWT(JSON Web Tok...

解决django前后端分离csrf验证的问题

第一种方式ensure_csrf_cookie 这种方方式使用ensure_csrf_cookie 装饰器实现,且前端页面由浏览器发送视图请求,在视图中使用render渲染模板,响应给前...

举例讲解Linux系统下Python调用系统Shell的方法

时候难免需要直接调用Shell命令来完成一些比较简单的操作,比如mount一个文件系统之类的。那么我们使用Python如何调用Linux的Shell命令?下面来介绍几种常用的方法: 1....

python flask搭建web应用教程

python flask搭建web应用教程

python flask搭建web应用教程 1.flask介绍 2.所需工具和环境 3.搭建flaskApp 4.具体程序编写 5.综上 1.flask介绍 flask是一款十分轻量级的...

Python Mysql自动备份脚本

测试系统环境  Windows 2003   python 2.5.1  mysql ...