from socket import * def main(): serverPort = 6789 serverSocket = socket(AF_INET
ID: 3814448 • Letter: F
Question
from socket import *
def main():
serverPort = 6789
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind(('', serverPort))
serverSocket.listen(1)
print('Webserver port: ', serverPort)
while True:
print('Ready...')
connectionSocket, addr = serverSocket.accept()
try:
message = connectionSocket.recv(1024)
print(message, '::', message.split()[0], ':',
message.split()[1])
filename = message.split()[1]
print(filename, '||', filename[1:])
f = open(filename[1:])
outputdata = f.read()
print(outputdata)
connectionSocket.send(' HTTP/1.1 200 OK ')
connectionSocket.send(outputdata)
connectionSocket.close()
except IOError:
pass
print("Error 404 Not Found")
connectionSocket.send(' HTTP/1.1 404 Not Found ')
break
pass
if __name__ == '__main__':
main()
-----------------------------------------------------------------------------
<html>
<header>
<title>Hello World Page</title>
</header>
<body>
Hello World!!!
</body>
</html>
----------ERROR MESSAGE-----------------------------
Users U ikram Desktop Socket 2 revisited python Web Server. py Webserver port 6789 eady Hello HTTP 1.1 FAnHost localhost :6789 FAnConnection keep-aliv GET World.html NFAnca max-age r nUpgrade-Insecure-Requests: 1 r User-Agent Moz Cache-Control illa 5.0 (Windows NT 6.3 WOW64 AppleWebKit/537.36 KKHTML like Gecko) Chrome/5 0.2987-133 Safari 537-36 FAnAccept text/html application xhtml+xml applicatio xml Eq-0.9., image webp q-0.8 r Accept-Encoding gzip, deflate sdch, br n ccept-Language en-US en q 0.8 r n r n b GET b' /Hello World.html Hello World. .html' b' Hello World.html' html header ktitle Hello World PageExplanation / Answer
1) connectionSocket.send(' HTTP/1.1 200 OK ')
replace above line in your code with following code:
connectionSocket.send("HTTP/1.1 200 OK ")
this is for # Send the HTTP response header line to the connection socket
2)connectionSocket.send(' HTTP/1.1 404 Not Found ')
replace above line in your code with the following code::
# Send HTTP response message for file not found
connectionSocket.send("HTTP/1.1 404 Not Found ")
connectionSocket.send("<html><head></head><body><h1>404 Not Found</h1></body></html> ")
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.