{"id":1619,"date":"2019-06-29T14:02:30","date_gmt":"2019-06-29T06:02:30","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=1619"},"modified":"2022-01-24T11:10:59","modified_gmt":"2022-01-24T03:10:59","slug":"how-to-send-a-http-request-with-client-certificate-private-key-password-secret-in-python-3","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-send-a-http-request-with-client-certificate-private-key-password-secret-in-python-3\/","title":{"rendered":"How to send a HTTP request with client certificate + private key + password\/secret in Python 3"},"content":{"rendered":"<p>When we need to create a <a href=\"https:\/\/www.techcoil.com\/glossary\/http-client\/\" rel=\"noopener\" target=\"_blank\">HTTP client<\/a>  that communicates with a <a href=\"https:\/\/www.techcoil.com\/glossary\/http-server\/\" rel=\"noopener\" target=\"_blank\">HTTP server<\/a>  through certificate-based authentication, we will typically have to download a certificate, in <code>.pem<\/code> format, from the server. <\/p>\n<p>After we had downloaded the <code>.pem<\/code> file, the HTTP client will use the private key and certificate to authenticate itself with the HTTP server. Sometimes, the HTTP client will need to decrypt the private key with a password\/secret first.<\/p>\n<p>So with a <code>.pem<\/code> file and a password\/secret, how can you create a HTTP client in Python 3 to send a <a href=\"https:\/\/www.techcoil.com\/glossary\/http-request\/\" rel=\"noopener\" target=\"_blank\">HTTP request<\/a> to the HTTP server?<\/p>\n<p>In case you need it, this post shows how to send a HTTP request with client certificate + private key + password\/secret in Python 3.<\/p>\n<h2>Creating the sample Python 3 code that send a HTTP Post request to a HTTP endpoint with client certificate + private key + password\/secret <\/h2>\n<p>Let's assume that there is an HTTP endpoint at <strong>https:\/\/example.com\/a\/http\/url<\/strong> that authenticates clients with client certificates and receives HTTP Post requests. <\/p>\n<p>In addition to that, the <code>.pem<\/code> file is named as <strong><code>a_certificate_file.pem<\/code><\/strong> and the certificate secret is <strong>your_certificate_secret<\/strong>.<\/p>\n<p>Given these points, you can create a Python 3 file in the same folder as the <code>.pem<\/code> file with the following content to send a HTTP Post request to that HTTP endpoint:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport http.client\r\nimport json\r\nimport ssl\r\n\r\n# Defining certificate related stuff and host of endpoint\r\ncertificate_file = 'a_certificate_file.pem'\r\ncertificate_secret= 'your_certificate_secret'\r\nhost = 'example.com'\r\n\r\n# Defining parts of the HTTP request\r\nrequest_url='\/a\/http\/url' \r\nrequest_headers = {\r\n    'Content-Type': 'application\/json'\r\n}\r\nrequest_body_dict={\r\n    'Temperature': 38,\r\n    'Humidity': 80\r\n}\r\n\r\n# Define the client certificate settings for https connection\r\ncontext = ssl.SSLContext(ssl.PROTOCOL_SSLv23)\r\ncontext.load_cert_chain(certfile=certificate_file, password=certificate_secret)\r\n\r\n# Create a connection to submit HTTP requests\r\nconnection = http.client.HTTPSConnection(host, port=443, context=context)\r\n\r\n# Use connection to submit a HTTP POST request\r\nconnection.request(method=&quot;POST&quot;, url=request_url, headers=request_headers, body=json.dumps(request_body_dict))\r\n\r\n# Print the HTTP response from the IOT service endpoint\r\nresponse = connection.getresponse()\r\nprint(response.status, response.reason)\r\ndata = response.read()\r\nprint(data)\r\n<\/pre>\n<h2>Understanding the sample Python 3 code that send a HTTP Post request to a HTTP endpoint with client certificate + private key + password\/secret<\/h2>\n<p>First of all, we indicate that we wish to use some functionalities from <a href=\"https:\/\/docs.python.org\/3\/library\/http.client.html\" rel=\"noopener\" target=\"_blank\">http.client<\/a>, <a href=\"https:\/\/docs.python.org\/3\/library\/json.html\" rel=\"noopener\" target=\"_blank\">json<\/a> and <a href=\"https:\/\/docs.python.org\/3\/library\/ssl.html\" rel=\"noopener\" target=\"_blank\">ssl<\/a> modules:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport http.client\r\nimport json\r\nimport ssl\r\n<\/pre>\n<p>After that, we define some variables for certificate related stuff, host of endpoint and parts of the HTTP request:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n# Defining certificate related stuff and host of endpoint\r\ncertificate_file = 'a_certificate_file.pem'\r\ncertificate_secret= 'your_certificate_secret'\r\nhost = 'example.com'\r\n\r\n# Defining parts of the HTTP request\r\nrequest_url='\/a\/http\/url' \r\nrequest_headers = {\r\n    'Content-Type': 'application\/json'\r\n}\r\nrequest_body_dict={\r\n    'Temperature': 38,\r\n    'Humidity': 80\r\n}\r\n<\/pre>\n<p>Once we had defined the variables, we create a <a href=\"https:\/\/docs.python.org\/3\/library\/ssl.html#ssl.SSLContext\" rel=\"noopener\" target=\"_blank\"><code>ssl.SSLContext<\/code><\/a> object and <a href=\"https:\/\/docs.python.org\/3\/library\/ssl.html#ssl.SSLContext.load_cert_chain\" rel=\"noopener\" target=\"_blank\">load the certificate chain<\/a> with what we have from the server:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n# Define the client certificate settings for https connection\r\ncontext = ssl.SSLContext(ssl.PROTOCOL_SSLv23)\r\ncontext.load_cert_chain(certfile=certificate_file, password=certificate_secret)\r\n<\/pre>\n<p>Given that we have the SSL context, we then create a <a href=\"https:\/\/docs.python.org\/3\/library\/http.client.html#http.client.HTTPSConnection\" rel=\"noopener\" target=\"_blank\"><code>http.client.HTTPSConnection<\/code><\/a> object for sending HTTP requests to the server:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n# Create a connection to submit HTTP requests\r\nconnection = http.client.HTTPSConnection(host, port=443, context=context)\r\n<\/pre>\n<p>At this point of time, we can then use the <code>http.client.HTTPSConnection<\/code> object to send a HTTP request to the server endpoint:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n# Use connection to submit a HTTP POST request\r\nconnection.request(method=&quot;POST&quot;, url=request_url, headers=request_headers, body=json.dumps(request_body_dict))\r\n<\/pre>\n<p>Finally, we simply print some parts of the <a href=\"https:\/\/www.techcoil.com\/glossary\/http-response\/\" rel=\"noopener\" target=\"_blank\">HTTP response<\/a> that the server returns back to our client:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n# Print the HTTP response from the IOT service endpoint\r\nresponse = connection.getresponse()\r\nprint(response.status, response.reason)\r\ndata = response.read()\r\nprint(data)\r\n<\/pre>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-q7\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-q7&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-q7&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F1619&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>When we need to create a <a href=\"https:\/\/www.techcoil.com\/glossary\/http-client\/\" rel=\"noopener\" target=\"_blank\">HTTP client<\/a>  that communicates with a <a href=\"https:\/\/www.techcoil.com\/glossary\/http-server\/\" rel=\"noopener\" target=\"_blank\">HTTP server<\/a>  through certificate-based authentication, we will typically have to download a certificate, in <code>.pem<\/code> format, from the server. <\/p>\n<p>After we had downloaded the <code>.pem<\/code> file, the HTTP client will use the private key and certificate to authenticate itself with the HTTP server. Sometimes, the HTTP client will need to decrypt the private key with a password\/secret first.<\/p>\n<p>So with a <code>.pem<\/code> file and a password\/secret, how can you create a HTTP client in Python 3 to send a <a href=\"https:\/\/www.techcoil.com\/glossary\/http-request\/\" rel=\"noopener\" target=\"_blank\">HTTP request<\/a> to the HTTP server?<\/p>\n<p>In case you need it, this post shows how to send a HTTP request with client certificate + private key + password\/secret in Python 3.<\/p>\n","protected":false},"author":1,"featured_media":1244,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375],"tags":[653,23,59,654,655,226,233,656],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Python-Logo.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-q7","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1619"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=1619"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1619\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1244"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=1619"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=1619"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=1619"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}