1

I am trying to use Requests to connect with a Proxy to a site and extract the Certificate information. I am replicating the code produced here : https://stackoverflow.com/a/52072170/1709587

But when I try to run it I get an error :

AttributeError: 'Response' object has no attribute 'peer_certificate'

Can someone explain why I am getting this error and how I can fix the issue and get the certificate information?

Here is the code replicated :

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))

1 Answers1

1

I ran your code exactly copied from above and it worked. Then I removed the "s" from your https://google.com

Is the code above what you actually ran? Is it possible you replaced the actual site which was http with https://google.com as a demo for us?

If so, it being http would cause exactly that error.

google with http

brazosFX
  • 342
  • 1
  • 11
  • I did use HTTPS, I also have to use a proxy parameter in the get request. If I do not use it, I receive a timeout error. If I do use it, I receive the AttributeError. It seems like I am not ever going to get it working behind my proxy. The proxy is defined and used like : https://2.python-requests.org/en/master/user/advanced/#proxies – Virtual Penman Aug 14 '19 at 21:18
  • 1
    How does your proxy handle https? Is it MIM? Is it enforced by a security package installed on the workstation? – brazosFX Aug 15 '19 at 05:45
  • That is a great question. I am actually not 100% sure. Like I said I am really new to Python, and very new to this role.(my first software engineering position) I know the proxy is set up on the Win 10 machine as a script address .dat file that holds all the proxy information. – Virtual Penman Aug 15 '19 at 17:27