'Python Socket Programming Simple Web Server

I am working on my first Python socket programming code and I cannot figure out what is wrong. I type in the IP address of the server that this program is running on along with the port number and the file I am trying to receive. I should receive the file in the browser and the socket should close. Instead, the server prints out the print line 'Ready to serve...' three times, displays '404 Not Found' on the browser, and never closes the socket. Does anyone have any ideas?

#import socket module
from socket import *
serverSocket = socket(AF_INET, SOCK_STREAM)
#Prepare a sever socket
serverSocket.bind(('', 12006))
serverSocket.listen(1)
while True:
    print 'Ready to serve...'
    #Establish the connection
    connectionSocket, addr = serverSocket.accept()
    try:
        message = connectionSocket.recv(1024)
        filename = message.split()[1]
        f = open(filename[1:])
        outputdata = f.read()
        f.close()
        #Send one HTTP header line into socket
        connectionSocket.send('HTTP/1.0 200 OK\r\n\r\n')
        #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 message for file not found
        connectionSocket.send('404 Not Found')
        #Close client socket
        connectionSocket.close()
serverSocket.close() 


Solution 1:[1]

Thank you everyone for all the help. I figured out what was wrong. I had renamed my HTML to "HelloWorld.html" and then Windows automatically added .html to end of the file. So in order to have accessed the file I would of needed to type in HelloWorld.html.html. I changed the file name and then this code worked perfectly.

Solution 2:[2]

This code should work:

# python 3
from socket import *
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind(('127.0.0.1', 5500))
serverSocket.listen(1)
while True:
    print("Server is running")
    connectionSocket, addr = serverSocket.accept()
    try:
        message = connectionSocket.recv(1024)
        filename = message.split()[1].decode('utf-8').strip("/")
        print(filename)
        f = open(filename)
        outputdata = f.read()
        f.close()
        connectionSocket.send('HTTP/1.0 200 OK\r\n\r\n'.encode())
        for i in range(0, len(outputdata)):
            connectionSocket.send(outputdata[i].encode())
        connectionSocket.close()
    except IOError:
        connectionSocket.send('404 Not Found'.encode())
        connectionSocket.close()
serverSocket.close() 

Solution 3:[3]

Before this line filename = message.split()[1]

print(message)

after this line filename = message.split()[1]

print(filename)

I thought that the error is in that line.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bill
Solution 2 Eswar sai Reddy C V
Solution 3 Rabbid76