Python - 请求库 - 如何确保 HTTPS 请求

Python - Requests Library - How to ensure HTTPS requests

这可能是个愚蠢的问题,但我只想通过下面的内容来确定一下。

我目前正在使用 python 中的请求库。我正在使用它来调用托管在 Azure 云上的外部 API。

如果我使用来自虚拟机的请求库,并且请求库发送到 URL: https://api-management-example/run,这样做意味着我与此 API 的通信以及我发送的整个有效负载都是安全的?我在我的虚拟环境中的 Python 站点包中看到了一个 cacert.pem 文件。我需要更新吗?我是否需要在我这边做任何其他事情来确保通信安全,或者我调用 HTTPS URL 就意味着它是安全的?

任何 information/guidance 将不胜感激。

谢谢,

Post requests are more secure because they can carry data in an encrypted form as a message body. Whereas GET requests append the parameters in the URL, which is also visible in the browser history, SSL/TLS and HTTPS connections encrypt the GET parameters as well. If you are not using HTTPs or SSL/TSL connections, then POST requests are the preference for security. A dictionary object can be used to send the data, as a key-value pair, as a second parameter to the post method.

只要您 API 上有有效的 SSL 证书,HTTPS 协议就是安全的。如果你想更加安全,你可以实现端到端encryption/cryptography。基本上转换你所谓的明文,并将其转换为加密文本,称为密文。

您可以在 requests 库中明确启用验证:

import requests

session = requests.Session()
session.verify = True
session.post(url='https://api-management-example/run', data={'bar':'baz'})

这是默认启用的。您还可以根据请求验证证书:

requests.get('https://github.com', verify='/path/to/certfile')

或每个会话:

s = requests.Session()
s.verify = '/path/to/certfile'

阅读docs

  1. 具有有效签名证书的 HTTPS 是安全的。有些人使用自签名证书来维护 HTTPS。在请求库中,您明确验证您的证书。如果你有自签名的 HTTPS 那么你需要通过证书来交叉验证你的本地证书。
  • 验证=真
import requests

response = requests.get("https://api-management-example/run", verify=True)
  • 自签名证书
import requests

response = requests.get("https://api-management-example/run", verify="/path/to/local/certificate/file/")