0

I'm getting the following output

301 Moved Permanently --- when using http.client

200 --- when using requests

URL handling "https://i.sstatic.net/dJpHN.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)
  print(response.status_code)            
2
  • What do you want to do? Commented Sep 22, 2017 at 8:07
  • requests follows redirects by default. Commented Sep 22, 2017 at 8:09

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

The documentation on the http module includes in its first sentence "It is normally not used directly." Unlike requests it doesn't action the 301 response and follow the redirection in the headers. It instead returns the 301, which you would have to process yourself.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.