Search code examples
pythongetpython-requestspython-3.6

Python requests: how to make http only request?


When I try to make a request using http:// some websites perform a redirection and I find the final destination starting with https://.

How can I make my request only support http:// and not accept https://?

For example, this code:

import requests
requests.packages.urllib3.disable_warnings() # to disable warnings

response = requests.get("http://facebook.com",verify=False,timeout=5)
responseURL = response.url 
print(responseURL)

Will provide this result:

https://www.facebook.com/

What if I want to use http:// only?


Solution

  • You can easily do this using the allow_redirects argument when sending the request.

    Like this:

    import requests
    requests.packages.urllib3.disable_warnings() # to disable warnings
    
    response = requests.get("http://facebook.com",allow_redirects=False, verify=False,timeout=5)
    responseURL = response.url 
    print(responseURL)
    

    Hope this helps!