# Program to print contents of a URL # It assumes the resource that the URL points to returns # UTF-8 encoded characters # It also handles SSL # Matt Bishop, MHI 289I, Fall 2023 # import urllib.request import ssl # # ignore SSL/TLS certificate errors # try: _create_unverified_https_context = ssl._create_unverified_context except AttributeError: # Legacy Python that doesn't verify HTTPS certificates by default pass else: # Handle target environment that doesn't support HTTPS verification ssl._create_default_https_context = _create_unverified_https_context # # ask for the URL # try: urlname = input("Please enter the URL: ") except EOFError: print("Bye!") except: print("That's not a valid string. ") else: # # got it -- now try to read from the URL # try: webpage = urllib.request.urlopen(urlname) except Exception as msg: print("URL retrieval failed!", msg) else: # Read from the object, storing the page's contents in 's'. # and print it sa = str(webpage.read().decode().encode()) print(sa)