python requests证书问题解决

yipeiwu_com6年前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设计】。

相关文章

对Python中数组的几种使用方法总结

二维数组的初始化 matirx_done = [[0 for i in range(0, len(matirx))]for j in range(0, len(matirx[0]))...

Django 连接sql server数据库的方法

Django 连接sql server数据库的方法

*在此注明,本人使用的是python3.5和Django2.0.4 sql server是微软出的一款闭源的可运行于windows和linux平台上的 关系型数据库 。由于其闭源的特性...

Django工程的分层结构详解

Django工程的分层结构详解

前言 传统上我们都知道在Django中的MTV模式,具体内容含义我们再来回顾一下: M:是Model的简称,它的目标就是通过定义模型来处理和数据库进行交互,有了这一层或者这种类型的对象...

python list中append()与extend()用法分享

1. 列表可包含任何数据类型的元素,单个列表中的元素无须全为同一类型。 2.  append() 方法向列表的尾部添加一个新的元素。只接受一个参数。3.  exten...

python计算圆周长、面积、球体体积并画出圆

python计算圆周长、面积、球体体积并画出圆

输入半径,计算圆的周长、面积、球体体积,并画出这个圆。拖动条、输入框和图像控件的数据保持一致! Fedora下测试通过 复制代码 代码如下:#https://github.com/Rob...