42 lines
899 B
Python
42 lines
899 B
Python
'''
|
|
Typical use of urllib:
|
|
reponse = urllib.request.urlopen(
|
|
url,
|
|
data=None,
|
|
[timeout,]*,
|
|
cafile=None,
|
|
capath=None,
|
|
cadefault=False,
|
|
context=None)
|
|
|
|
|
|
'''
|
|
|
|
# using urllib to request data.
|
|
# TODO : import the urllib request class
|
|
import urllib.request
|
|
|
|
def main():
|
|
# the url to retrive our sample data from
|
|
url = "http://httpbin.org/xml"
|
|
|
|
# TODO : open the url and retrive some data
|
|
result = urllib.request.urlopen(url)
|
|
|
|
# TODO :print the result code from the request, should be 200 ok
|
|
print('Result code : {0}'.format(result.status))
|
|
|
|
# TODO : print the returned data headers
|
|
print("Headers : -------")
|
|
print(result.getheaders())
|
|
|
|
|
|
# TODO : print the returned data intself
|
|
print('Returned data : ---------')
|
|
print(result.read().decode('utf-8'))
|
|
|
|
|
|
if __name__=='__main__':
|
|
main()
|
|
|