Pbch Pbch - 5 months ago 50
Python Question

python http client module error / inconsistent

I'm getting the following output

301 Moved Permanently --- when using http.client

200 --- when using requests

URL handling "http://i.imgur.com/fyxDric.jpg" passed as arg through command

What I expect is give me 200 status ok response.

This is the body

if scheme == 'http':
print('Ruuning in the http')
conn = http.client.HTTPConnection("www.i.imgur.com")
conn.request("GET", urlparse(url).path)
conn_resp = conn.getresponse()
body = conn_resp.read()
print(conn_resp.status, conn_resp.reason, body)

When using the requests

headers = {'User-Agent': 'Mozilla/5.0 Chrome/54.0.2840.71 Safari/537.36'}
response = requests.get(url, allow_redirects=False)

Answer Source

You are trying to hit imgur over http, but imgur redirects all its request to process over https.

Due to this redirect the issue is occurring.

http module doesnt inherently handle the redirects you need to handle the redirects, where as requests module handles these redirects by itself.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download