python - 为什么使用 ssl 认证请求返回 html?

标签 python python-2.7 ssl python-requests

我有一个问题

我正在尝试将一些数据发布到网络服务器并获得响应。 Webserver 有 SSL 证书验证,所以我为它提供了文件。在响应中我应该收到 xml 文件,但我收到一些 html。

我的小脚本是这样的:

import requests, sys, os

program_directory=sys.path[0]
verify = os.path.join(program_directory, "test.pem")
data='some data'

print requests.post('https://somewebsite', data=data, verify=verify).text

这就是我在 html 之前得到的:

C:\Python27\lib\site-packages\requests\packages\urllib3\connection.py:251: SecurityWarning: Certificate has no `subjectAltName`, falling back to check for a `commonName` for now. This feature is being removed by major browsers and deprecated by RFC 2818. (See https://github.com/shazow/urllib3/issues/497 for details.) SecurityWarning

如果我使用 verify=False 并且不提供文件,它可以正常工作并返回正确的数据。可能有什么问题,为什么我会收到此警告和 html?

最佳答案

引用 GrahamDumpleton :

This is because in older Python versions the _ssl module does not return 'subjectAltName' in the set of fields in the SSL certificate. That the field isn't returned even if present means that even if the SSL certificate is valid, the check will fail and the warning logged.

To eliminate the warnings you can disable urllib3 warnings globally.

requests 模块包含 urllib3 的捆绑版本,您很可能需要使用:

import requests
requests.packages.urllib3.disable_warnings()

如果你只想忽略那个特定的警告,你可以使用:

from requests.packages.urllib3.exceptions import SubjectAltNameWarning
requests.packages.urllib3.disable_warnings(SubjectAltNameWarning)

关于python - 为什么使用 ssl 认证请求返回 html?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28707134/

相关文章:

java - 从 .crt 和 .key 文件创建 .jks 是可能的

mysql - 如何使用 Go 创建到 Cloud SQL 数据库的 TLS 连接?

python - Django 没有额外的表?

Python Pywin 和 OneNote COM : OneNote. Application.15 'cannot automate the makepy process'

Python 'if x is None' 未捕获 NoneType

python - 如何获取文件的父目录?

python - 绘制 x 和 y 值,结果为颜色/热图

python - Ckan备份和恢复

image - python : Concatenate 2 irregular sized images by automatically detecting the sizes

amazon-web-services - 使用 google 域的 AWS SSL 配置