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

34 lines
923 B
Python

# Send data to a server using urllib
# TODO : import the request and parse modules
import urllib.request
def main():
url = "http://httpbin.org/get"
# TODO : create some data to pass to the GET request.
args = {
'name': 'Armando FEMAT',
'is_author' : True
}
# TODO : the data needs to be url-encoded before passing as arguments.
data = urllib.parse.urlencode(args)
# TODO : issue the request with the data params. as part of the url
# result = urllib.request.urlopen(url + "?" + data)
# TODO : issue the request with a data parameter to use post.
url = "http://httpbin.org/post"
data = data.encode()
result = urllib.request.urlopen(url, data=data)
print('Result code (0)'.format(result.status))
print('Returned data: -----------')
print(result.read().decode('utf-8'))
if __name__=='__main__':
main()