Skip to content

Instantly share code, notes, and snippets.

@vinovator
Last active October 2, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinovator/b4e73d0e9471c5f16369 to your computer and use it in GitHub Desktop.
Save vinovator/b4e73d0e9471c5f16369 to your computer and use it in GitHub Desktop.
Get the request header and response header from a http request-response sequence. Assumes that the url accepts digest authentication
# getHttpHeader.py
# Python 2.7.6
import requests
from requests.auth import HTTPDigestAuth
import getpass # To mask the password typed in
# Replace with the correct URL
url = "http://some_url"
# It is a good practice not to hardcode the credentials. So ask the user to enter credentials at runtime
# The default prompt, if none is specified is “Password:”
myResponse = requests.get(url,auth=HTTPDigestAuth(raw_input("username: "), getpass.getpass()), verify=True)
#print (myResponse.status_code)
myRequestHeader = myResponse.request.headers
myResponseHeader = myResponse.headers
print ("Request headers: ") # Gets the header sent in Request
for key in myRequestHeader:
print (key, myRequestHeader[key])
print ("Response headers: ") # Gets the header from Response
for key in myResponseHeader:
print (key, myResponseHeader[key])
# For successful API call, response code will be 200 (OK)
if(myResponse.ok):
print ("ok")
else:
# If response code is not ok (200), print the resulting http error code with description
myResponse.raise_for_status()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment