How to print two responses from one request using python http.client

64 views Asked by At

I am trying to send an request using the below code,

import http.client

conn = http.client.HTTPConnection("localhost")
payload = "GET /_hidden/index.html HTTP/1.1\r\nHost: notlocalhost\r\n\r\n"
headers = {'Host': 'localhost','Content-Length': '56','User-Agent':'-'}
conn.request("GET", "/", payload, headers)
res = conn.getresponse()
#ress = conn.getresponse()
data = res.read()
#data2 = res.read()
print(data.decode("utf-8"))
#print(data2.decode("utf-8"))

I am expecting two responses from the server and the server is also sending 2 responses but how can I print both of them? if I have an other conn.getresponse() I am getting an error. I have attached the server log for a reference both the request sent are accepted in the server but I want to print both the response. server log

1

There are 1 answers

5
X3R0 On

You can just follow the request chain

from http.client import HTTPSConnection
from urllib.parse import urljoin

def get(host, url):
    print(f'GET {url}')

    connection = HTTPSConnection(host)
    connection.request('GET', url)

    response = connection.getresponse()
    location_header = response.getheader('location')

    if location_header is None:
        return response
    else:
        location = urljoin(url, location_header)
        return get(host, location)

response = get('en.wikipedia.org', '/api/rest_v1/page/random/summary')

print(response.read())

once you understand the above code snippet. you may then include your specific needs into the code example, such as the code below in the second snippet.

import http.client
from http.client import HTTPSConnection
from urllib.parse import urljoin

def get(host, url, headers):
    print(f'GET {url}')

    connection = HTTPSConnection(host)
    connection.request('GET', url, {}, headers)

    response = connection.getresponse()

    location_header = response.getheader('location')

    if location_header is None:
        return response
    else:
        location = urljoin(url, location_header)
        return get(host, location, headers)

call it as such

headers = {'Host': 'localhost','Content-Length': '56','User-Agent':'-'}
response = get("localhost", "/_hidden/index.html", headers)

Extra

learn more about connection.request via the documentation