使用 wincertstore 的 Python 请求 [英] Python Requests with wincertstore

查看:32
本文介绍了使用 wincertstore 的 Python 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 requests 包连接到我公司的内部网页,但由于 python 不使用 Windows 默认可信证书,因此连接被拒绝.我发现 wincertstore 可用于获取 Windows 默认证书.但我仍然不确定如何将它与我的请求一起使用.以下是我迄今为止尝试过的代码......

I'm trying to connect to my corporate's internal webpages through the requests package, but since python does not use the windows default trusted certificates the connection is denied. I found out that wincertstore can be used to fetch the windows default certificates. But I'm still not sure how to use that along with the my request. Below is the code I have tried so far.............

import requests, socket, atexit, ssl, wincertstore
from requests.auth import HTTPBasicAuth
certfile = wincertstore.CertFile()
certfile.addstore("CA")
certfile.addstore("ROOT")
atexit.register(certfile.close)
ssl_sock = ssl.wrap_socket(s,ca_certs=certfile.name, 
cert_reqs=ssl.CERT_REQUIRED)
requests.get(url)

我收到以下错误......................requests.exceptions.SSLError: HTTPSConnectionPool(host='myhost', port=443): Max retries exceeded with url: myurl (Caused by SSLError(SSLError("bad handshake: Error([('SSLroutines', 'tls_process_server_certificate', '证书验证失败')],)",),))

I get the following error................... requests.exceptions.SSLError: HTTPSConnectionPool(host='myhost', port=443): Max retries exceeded with url: myurl (Caused by SSLError(SSLError("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')],)",),))

我可以在同一个网址上使用 wget 并下载内容.

I am able to use wget on the same url and download the content.

wget --no check certificate --user=我的用户名 --password=我的密码网址

但我对下载内容不感兴趣,因为我只需要抓取网页内容的一小部分.

But I am not interested in downloading the content as I only need to scrape a small portion of the webpage content.

Python 版本 = 3.6.5

Pythin version = 3.6.5

Wincertstore 链接 - 链接

Wincertstore link - Link

在此先感谢您的帮助........

Thanks in advance for your help..............

推荐答案

我遇到了类似的问题,并使用 python-certifi-win32 包修复了它:

I had a similar issue and fixed it using the python-certifi-win32 package:

pip install python-certifi-win32

现在你可以使用:

requests.get(url, verify=True)

并且使用 Windows 证书存储检查证书.

and the certificate is checked using the Windows Certificate Store.

这仅在证书安装在 Windows 证书存储区时才有效...

This only works if the certificate is installed in the Windows Certificate Store...

这篇关于使用 wincertstore 的 Python 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆