scientific_comp_projects/CODE/[python]XML_JSON/reqauth_intro.py
2021-10-29 15:16:40 +02:00

45 lines
1.3 KiB
Python

# using the requests library to access internet data
'''
Making a simple request
reponse = requests.get(url)
params | Key-value pairs that will be sent in the query string.
header | dictionary of header vaues to send along with the request
auth | Authentication tuple to enable different forms of authentication
timeout | Value in seconds to wait for a server to respond
'''
import requests
from requests.auth import HTTPBasicAuth
def main():
# Access a url that requires authentication - the format of this
# URL is that you provide the username/password to auth against
url = 'http://httpbin.org/basic-auth/coyote/MySecret'
# TODO: Create a credentials object using HHTPBasicAuth
myCreds = HTTPBasicAuth('coyote','MySecret')
# TODO : Issue the request woth the authentication credential
result = requests.get(url, auth=myCreds)
printResults(result)
def printResults(resData):
print('Result code : {0}'.format(resData.status_code))
print('\n')
print('Headers : ------------------------------')
print(resData.headers)
print('\n')
print('Returned data : ------------------------')
print(resData.text)
if __name__=="__main__":
main()