SO community, please help!
I was doing the exercise 1 for chapter 12 of PY4E and could not figure out why the file wouldn't print out in terminal and I got the response of "HTTP/1.1 404 Not Found"
Question: prompt the user for the URL so it can read any web page. You can use split('/') to break the URL into its component parts so you can extract the host name for the socket connect call. Add error checking using try and except to handle the condition where the user enters an improperly formatted or non-existent URL.
Code I wrote:
import sockettry: url = input('Enter URL here: ') new_url = url.split('/') host = new_url[2] path = '/'+'/'.join(new_url[3:]) #print(new_url) #print(host) #print(path) mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) mysock.connect((host, 80)) cmd = f'GET {path} HTTP/1.0\r\n\r\n'.encode() mysock.send(cmd) while True: data = mysock.recv(512) if len(data) < 1: break print(data.decode(),end='') mysock.close()except: print('url not found or has issue')
The url that i'm trying to retreive: http://data.pr4e.org/romeo.txt - the link can be opened in the browser so I'm very confused that it returned 404 not found