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

相关文章

python3 遍历删除特定后缀名文件的方法

python3 遍历删除特定后缀名文件的方法

U盘中毒了,U盘内的每个文件夹内都多了一个.lnk文件,处女座又犯了,实在不能忍,就写了个脚本把所有的.lnk文件删除了。 多级目录递归删除 import os n = 0 for...

记一次python 内存泄漏问题及解决过程

记一次python 内存泄漏问题及解决过程

最近工作中慢慢开始用python协程相关的东西,所以用到了一些相关模块,如aiohttp, aiomysql, aioredis等,用的过程中也碰到的很多问题,这里整理了一次内存泄漏的问...

python统计日志ip访问数的方法

本文实例讲述了python统计日志ip访问数的方法。分享给大家供大家参考。具体如下: import re f=open("/tmp/a.log","r") arr={} lines...

Python在Matplotlib图中显示中文字体的操作方法

1.    说明 本篇主要针对在Ubuntu系统中,matplotlib显示不了中文的问题,尤其是在无法安装系统字体的情况下,解决Python绘图时中文显示...

django用户登录验证的完整示例代码

django用户登录验证的完整示例代码

1,urls.py内容: from django.conf.urls import url from django.contrib import admin from myApp...