Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The problem can be found below. I would like someone to run this. I don’t get it

ID: 3797321 • Letter: T

Question

The problem can be found below. I would like someone to run this. I don’t get it because there’s an error. Please, fix this because I don’t know what is happening. What version of python should I use?

Please, if you don’t know what is happening let someone else to do it.

Bp Project untitled PycharmProjects/untitled 3 import sys ServerPort 80 new server Socket socket (AF INET, S0CK STREAM) Illi External Libraries 6 #Prepare a sever socket 7 #Fill in start server Socket.bind server Port)) server Socket.listen (1) print the web server is up on port server Port 10 #Fill in end 11 while True: Establish the connection 13 print "Ready to serve... 14 connectionSocket add J server Socket. accept() 15 16 try: message connectionSocket.recv(1024) 17 print message message split 01, message. split 1] 18 filename 3 message. split [1] print filename 'II filename [1:] 20 open (filename (1:1) 21 output data f.read 22 print outputdata 23 Send one HTTP header line into socket Fill in start 25 connection Socket.send(' InHTTP/1.1 200 0KNnNn 26 connectionSocket.send(outputdata) 27 Fill in end 28 Send the content of the requested file to the client 29 for i in range (0, len(outputdata)): 30 connection Socket.send(outputdata [i]) 31 connection Socket.close 32 33 except IOError w Send response message for file not found 34 Fill in start 35 connectionSocket.send NnHTTP/1.1 404 Not Found nAn 36 connectionSocket.send 'InHTTP/1.1 404 Not Found n ') 37 38 39 /Library/Frameworks/Python, framework/Versions/2.7/bin/python2.7 /Users. File User S untitled new py", line 33 except IOError: SyntaxError: invalid syntax Process finished with exit code 1

Explanation / Answer

Update your code as below :

#!/bin/python
import sys
import socket

serverPort=80
serverSocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

serverSocket.bind(('',serverPort))
serverSocket.listen(1)

while True :
   connectionSocket, addr = serverSocket.accept()
   try :
       message = connectionSocket.recv[1024]
       filename = message.split()[1]
       f = open(filename[1:])
       outputdata = f.read()
       connectionSocket.send(' HTTP/1.1 200 OK ')
       connectionSocket.send(outputdata)
       for i in range(0,len(outputdata)):
           connectionSocket.send(outputdata[i])
       connectionSocket.close()
   except IOError : #indentation is not correct here
       connectionSocket.send(' HTTP/1.1 404 Not Found ')
serverSocket.close();