# import a library for openning URL import urllib.request # prepare URL to be opened filename = 'index.html' url = 'http://cs1110.cs.virginia.edu/' + filename # let's look at the URL print("url = " + url) # open the specified URL to read # store the response object received from the server in a variable stream = urllib.request.urlopen(url) # print(stream) # http.client.HTTPResponse object # Tips: # - Instead of "open()" when opening a file, # we use "urlopen()" when opening a url # - Once opening a url, we must decode the data # -- unlike reading the file, we can simply read() or readline() print("\n==== Start processing data ==== \n") # read each line from the response object for line in stream: # decode the string using the standard encoding scheme (UTF-8) # then, strip leading and tailing spaces, and split the string using "," decoded_data = line.decode("UTF-8").strip() print(decoded_data)