我正在尝试使用请求通过代理连接到站点并提取证书信息。我正在复制此处生成的代码:https://stackoverflow.com/a/52072170/1709587
但是当我尝试运行它时出现错误:
AttributeError: 'Response' object has no attribute 'peer_certificate'
有人可以解释一下为什么我会收到此错误以及如何解决该问题并获取证书信息吗?
这是复制的代码:
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
HTTPResponse = requests.packages.urllib3.response.HTTPResponse
orig_HTTPResponse__init__ = HTTPResponse.__init__
def new_HTTPResponse__init__(self, *args, **kwargs):
orig_HTTPResponse__init__(self, *args, **kwargs)
try:
self.peer_certificate = self._connection.peer_certificate
except AttributeError:
pass
HTTPResponse.__init__ = new_HTTPResponse__init__
HTTPAdapter = requests.adapters.HTTPAdapter
orig_HTTPAdapter_build_response = HTTPAdapter.build_response
def new_HTTPAdapter_build_response(self, request, resp):
response = orig_HTTPAdapter_build_response(self, request, resp)
try:
response.peer_certificate = resp.peer_certificate
except AttributeError:
pass
return response
HTTPAdapter.build_response = new_HTTPAdapter_build_response
HTTPSConnection = requests.packages.urllib3.connection.HTTPSConnection
orig_HTTPSConnection_connect = HTTPSConnection.connect
def new_HTTPSConnection_connect(self):
orig_HTTPSConnection_connect(self)
try:
self.peer_certificate = self.sock.connection.get_peer_certificate()
except AttributeError:
pass
HTTPSConnection.connect = new_HTTPSConnection_connect
r = requests.get('https://google.com', verify=False)
print(dir(r.peer_certificate))
最佳答案
我运行了从上面完全复制的代码并且它有效。然后我从你的 https://google.com 中删除了“s”
上面的代码是您实际运行的吗?您是否有可能将实际的 http 站点替换为 https://google.com作为我们的演示?
如果是这样,它是 http 就会导致该错误。
关于python - 无法使用 Python 中的 Requests 模块复制代码以获取证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57501882/