63 lines
1.7 KiB
Python
63 lines
1.7 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
|
|
|
|
def main():
|
|
# TODO : use requests to issue a standard HTTP GET request
|
|
url = 'http://httpbin.org/xml'
|
|
result = requests.get(url)
|
|
# printResults(result)
|
|
|
|
# TODO: Send some parameters to the URL via a get request
|
|
# Note that requests handles this for you, no manual decoding
|
|
url = 'http://httpbin.org/get'
|
|
dataValues = {
|
|
'key1' : 'value1',
|
|
'key2' : 'value2'
|
|
}
|
|
result = requests.get(url, params=dataValues)
|
|
#printResults(result)
|
|
|
|
|
|
url = 'http://httpbin.org/post'
|
|
dataValues = {
|
|
'key1' : 'value1',
|
|
'key2' : 'value2'
|
|
}
|
|
result = requests.post(url, data=dataValues)
|
|
# printResults(result)
|
|
|
|
|
|
# TODO : Pass a custom header to the server
|
|
url = 'http://httpbin.org/get'
|
|
headerValues = {
|
|
'User-Agent' : 'Coyote App / 9000.0.0'
|
|
}
|
|
result = requests.get(url, headers=headerValues)
|
|
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()
|