The problem can be found below. I would like someone to run this. I don’t get it
ID: 3797318 • 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 1Explanation / Answer
There is an indentation issue
Pasting code here though indentation will get lost please use pastebin link http://pastebin.com/9u2Khz0A to access code
#import socket module
from socket import *
import sys
serverPort=80
serverSocket = socket(AF_INET, SOCK_STREAM)
#Prepare a server socket
#Fill in start
serverSocket.bind(('',serverPort))
serverSocket.listen(1)
print 'the web server is up on port:',serverPort
#Fill in end
while True:
# Establish the connection
print 'Read to serve...'
connectionSocket, addr = serverSocket.acept()
try:
message = connectionSocket.recv(1024)
print message,':',mssage.split()[0],':',message.split()[1]
filename = message.split()[1]
print filename,'||',filename[1:]
f = open(filename[1:])
outputdata = f.read()
print outputdata
# Send one HTTP header line into socket
# Fill in start
connectionSocket.send(' HTTP/1.1 200 OK ')
connectionSocket.send(outputdata)
#Fill in end
#send the content of the requested file to the client
for i in range(0, len(outputdata)):
connectionSocket.send(outputdata[i])
connectionSocket.close()
except IOError:
#send response for file not found
# Fill in start
connectionSocket.send(' HTTP/1.1 404 Not Found ')
connectionSocket.send(' HTTP/1.1 404 Not Found ')
connectionSocket.close()
serverSocket.close()
sys.exit()
# please note I majorly fixed some smallish issue in your code (majorly indentation issue because of which you were getting an error.
# though your program wil work but am not sure if thats what question is asking (like you are sending whole data along with header and then sending data line by line.
# but am not changing that as that might be what you intended to do
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.