Python HTTP Client Request – GET, POST

Filed Under: Python
Python Http Client

Python HTTP module defines the classes which provide the client-side of the HTTP and HTTPS protocols. In most of the programs, the HTTP module is not directly used and is clubbed with the urllib module to handle URL connections and interaction with HTTP requests. Today we will learn how to use a Python HTTP client to fire HTTP request and then parse response status and get response body data.

Python HTTP Client

In this post on python HTTP module, we will try attempting making connections and making HTTP requests like GET, POST and PUT. Let’s get started.

Making HTTP Connections

We will start with the simplest thing HTTP module can do. We can easily make HTTP connections using this module. Here is a sample program:

import http.client

connection = http.client.HTTPConnection('', 80, timeout=10)

Let’s see the output for this program:
python http connection
In this script, we connected to the URL on Port 80 with a specific timeout.


Now, we will use HTTP client to get a response and a status from a URL. Let’s look at a code snippet:

import http.client

connection = http.client.HTTPSConnection("")
connection.request("GET", "/")
response = connection.getresponse()
print("Status: {} and reason: {}".format(response.status, response.reason))


In above script, we used a URL and checked the status with the connection object. Let’s see the output for this program:

Remember to close a connection once you’re done with the connection object. Also, notice that we used a HTTPSConnection to establish the connection as the website is served over HTTPS protocol.


When I first executed above program, I got following error related to SSL certificates.

$ python3.6 
Traceback (most recent call last):
  File "", line 4, in <module>
    connection.request("GET", "/")
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/", line 1026, in _send_output
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/", line 964, in send
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/", line 1400, in connect
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/", line 401, in wrap_socket
    context=self, session=session)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/", line 808, in init
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/", line 1061, in do_handshake
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/", line 683, in do_handshake
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:748)

From the output, it was clear that it has to do something with the SSL certificates. But website certificate is fine, so it has to be something with my setup. After some googling, I found that on MacOS, we need to run Install Certificates.command file present in the Python installation directory to fix this issue. Below image shows the output produced by this command execution, it looks like it’s installing latest certificates to be used when making SSL connections.


Note that I got this error on Mac OS. However, on my Ubuntu system, it worked perfectly fine.

Python HTTP Client Ubuntu

Python HTTP Client Ubuntu

Getting the Header list from Response

From the response we receive, the headers usually also contain important information about the type of data sent back from the server and the response status as well. We can get a list of headers from the response object itself. Let’s look at a code snippet which is a little-modified version of the last program:

import http.client
import pprint

connection = http.client.HTTPSConnection("")
connection.request("GET", "/")
response = connection.getresponse()
headers = response.getheaders()
pp = pprint.PrettyPrinter(indent=4)
pp.pprint("Headers: {}".format(headers))

Let’s see the output for this program:
python http request headers


We can POST data to a URL as well with the HTTP module and get a response back. Here is a sample program:

import http.client
import json

conn = http.client.HTTPSConnection('')

headers = {'Content-type': 'application/json'}

foo = {'text': 'Hello HTTP #1 **cool**, and #1!'}
json_data = json.dumps(foo)

conn.request('POST', '/post', json_data, headers)

response = conn.getresponse()

Let’s see the output for this program:
python http post
Feel free to use the HTTP Bin library to try more requests.

Python HTTP PUT Request

Of course, we can also perform a PUT request using the HTTP module itself. We will use the last program itself. Let’s look at a code snippet:

import http.client
import json

conn = http.client.HTTPSConnection('')

headers = {'Content-type': 'application/json'}

foo = {'text': 'Hello HTTP #1 **cool**, and #1!'}
json_data = json.dumps(foo)

conn.request("PUT", "/put", json_data)
response = conn.getresponse()
print(response.status, response.reason)

Let’s see the output for this program:
python http request, python http put example


In this lesson, we studied simple HTTP operations which can be done using http.client. We can also create python http server using SimpleHTTPServer module.

Reference: API Doc


  1. Ameya Marathe says:

    connection = HTTPSConnection(forwarder_host, port=9030, context=context, timeout=30)
    connection.request(method=”POST”, url=event_request_url, headers=request_headers, body=json.dumps(request_body_dict_events))
    response = connection.getresponse()
    connection.request(method=”POST”, url=event_request_url, headers=request_headers, body=json.dumps(request_body_dict_events))

    Even after closing the connection the next request is getting sent without any error.

  2. gaurav says:

    thanks. this was very helpful

  3. CAGB says:

    It work fine for me,
    Windows 10 Pro

    Thank you very much

  4. kushal says:

    i want to send the data from python to php
    i am not getting any errors in my python code but results are going to php

    can please help me and give me a proper idea to solve this problem

    my python code:

    import requests
    data = {‘name’: jhon}
    r =“url.php”, params=data)

    my php code:
    $name = htmlspecialchars($_GET[“name”]);
    echo “name: $name”;

  5. Pallavinoolu says:

    Can u write a python script for servlet asking user to enter username and password

  6. Jai K says:

    You just explained that ‘SSL Certificate’ issue only on the Mac system.. Could you please post for other platform also.. better for ‘linux’ machine.

    1. Pankaj says:

      I tried it on my Ubuntu machine, but it worked perfectly fine. I have updated the article with this information.

Leave a Reply

Your email address will not be published. Required fields are marked *

Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages