0

Python newbie here!

I have written the below code in c#, I would like to achieve the same functionality in python.

string serviceURL = "https://someurl";
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceURL);
                request.Method = "GET";
                string path = @"C:\mycert.cer";
                X509Certificate Cert = X509Certificate.CreateFromCertFile(path);

                Logger.Error(path);

                request.ClientCertificates.Add(Cert);

                HttpWebResponse WebResp = request.GetResponse() as HttpWebResponse;

                var encoding = ASCIIEncoding.ASCII;
                string responseText;
                StreamReader reader = new System.IO.StreamReader(WebResp.GetResponseStream(), encoding);
                using (reader = new System.IO.StreamReader(WebResp.GetResponseStream(), encoding))
                {
                    responseText = reader.ReadToEnd();
                }

I did some search on google, but could not find any fitting examples.

1 Answers1

-2

I recommend look at [requests][1] library - it's very handy and cool.

>>> import requests
>>> r = requests.get('https://api.github.com/events')
>>> print(r.status_code)
200
print(r.text)
# ..... here response body goes ...

You can pass verify the path to a CA_BUNDLE file or directory with certificates of trusted CAs:

>>> requests.get('https://github.com', verify='/path/to/certfile')
Eugene Lisitsky
  • 12,113
  • 5
  • 38
  • 59