import socket
import os.path
IP = "127.0.0.1"
PORT = 80
DEFAULT_URL = "C:\webroot\index.html"
SOCKET_TIMEOUT = 0.2
def get_file_data(filename):
""" Get data from file """
source_file = open(filename, 'rb')
data = source_file.read()
source_file.close()
return data
def handle_client_request(resource, client_socket):
""" Check the required resource, generate proper HTTP response and send to client"""
if resource == '/':
url = DEFAULT_URL
else:
url = resource
if os.path.isfile(url):
http_header = "HTTP/1.0 200 OK\r\n"
else:
client_socket.send("404 (Not Found)\r\n" + "connection close")
client_socket.close()
file_type = url.split(".")[-1]
if file_type == 'html' or file_type == 'txt':
http_header += "Content-Type: text/html; charset=utf-8\r\n"
elif file_type == 'jpg':
http_header += "Content-Type: image/jpeg\r\n"
elif file_type == 'js':
http_header += "Content-Type: text/javascript; charset=UTF-8\r\n"
elif file_type == 'css':
http_header += "Content-Type: text/css\r\n"
data = get_file_data(url)
http_header += "Content-Length:" + str(len(data)) + "\r\n"
http_response = http_header + "\r\n" + data
client_socket.send(http_response)
def validate_http_request(request):
""" Check if request is a valid HTTP request and returns TRUE / FALSE and the requested URL """
request_li = request.split("\r\n")[0].split(" ")
if request_li[0] != "GET" or request_li[2] != "HTTP/1.1" '/':
return False, ''
return True, request_li[1]
def handle_client(client_socket):
""" Handles client requests: verifies client's requests are legal HTTP, calls function to handle the requests """
print 'Client connected'
try:
while True:
client_request = client_socket.recv(1024)
print client_request.split("\r\n")[0]
valid_http, resource = validate_http_request(client_request)
if valid_http:
print 'Got a valid HTTP request'
handle_client_request(resource, client_socket)
else:
print "Error: HTTP request isn't valid"
break
print "closing connection"
client_socket.close()
except socket.timeout:
print "closing connections"
client_socket.close()
def main():
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((IP, PORT))
server_socket.listen(10)
print "Listening for connections on port %d" % PORT
while True:
client_socket, client_address = server_socket.accept()
client_socket.settimeout(SOCKET_TIMEOUT)
print 'New connection received'
handle_client(client_socket)
if __name__ == "__main__":
main()
I'm building a HTTP server for an assignment, the server is supposed to run local files from my computer on the browser. Right now I'm trying to run the default url.
First I get "/" as a request which is good, but then I receive an empty request which is an invalid request that closes the connection. After the server creates a new connection it gets "/css/doremon.css" as a request. doreomn.css is a file of the website I'm trying to run. This will create an error at get_file_data because the path is supposed to be: "C:\webroot\css\doremon.css".
This raises two questions: 1. Why does the client sends empty requests to the server? How can I prevent them from interrupting the connection? 2. From the third request it seems the client first sends the requested url and then requests files related to it, Is there a way to receive all of them at once? If not how can I fix the path for the requested files?