Before you judge me.. know that I've started looking for an answer 4 hours ago
I'm studying by myself, and got an assignment from an ebook I'm using, it didn't show any solution code or anything close to it.. (not really anything,just what to do)
I got an assignment to make an HTTP server from scratch I have the files (images and index.html file)
I need to connect to the server using the chrome browser and this way when I connect I'll see the html file, (if I specify something else, i'll see another file)
The problem is, when I run the script and connect to the website I see a broken html code.. (many lines of text with some kind of code)
Here is my code, I hope anyone can help me.
When I close the connection the script ends and says that the directory or file "/imgs/abstratc.jpg" doesn't exist. but it surely dies and in the correct location, I also chose to go for a raw path to avoid problems
additionally, here is what I see and should see:I'm studying by myself, and got an assignment from an ebook I'm using, it didn't show any solution code or anything close to it.. (not really anything,just what to do)
I got an assignment to make an HTTP server from scratch I have the files (images and index.html file)
I need to connect to the server using the chrome browser and this way when I connect I'll see the html file, (if I specify something else, i'll see another file)
The problem is, when I run the script and connect to the website I see a broken html code.. (many lines of text with some kind of code)
Here is my code, I hope anyone can help me.
When I close the connection the script ends and says that the directory or file "/imgs/abstratc.jpg" doesn't exist. but it surely dies and in the correct location, I also chose to go for a raw path to avoid problems
# TO DO: import modulesv # TO DO: set constantsv import socket import os IP = "127.0.0.1" PORT = 80 def get_file_data(filename): f = open(r"C:\webroot\index.html") return f.read() def handle_client_request(resource, client_socket): """ Check the required resource, generate proper HTTP response and send to client""" # TO DO : add code that given a resource (URL and parameters) generates the proper responsev url = resource if url != "/": pass else: url = "index.html" filename = os.path.basename(url) print url # TO DO: check if URL had been redirected, not available or other error code. For example:v if not os.path.exists(url) and os.path.exists("uploads/"+filename): http_header = "HTTP/1.0 302 FOUND\nLocation: /uploads/"+filename+"\n" elif not os.path.exists(url) and os.path.exists("js/"+filename): http_header = "HTTP/1.0 302 FOUND\nLocation: /js/"+filename+"\n" elif not os.path.exists(url) and os.path.exists("imgs/"+filename): http_header = "HTTP/1.0 302 FOUND\nLocation: imgs/"+filename+"\n" elif not os.path.exists(url) and os.path.exists("css/"+filename): http_header = "HTTP/1.0 302 FOUND\nLocation: /css/"+filename+"\n" else: pass # TO DO: send 302 redirection responsev # TO DO: extract requested file tupe from URL (html, jpg etc)v if os.path.exists("") or os.path.exists("index.html"): http_header = "HTTP/1.0 200 OK\nContent-Type: text/html; charset=utf-8\n" elif os.path.exists("js/"): http_header = "HTTP/1.0 200 OK\nContent-Type: text/javascript; charset=UTF-8\n" elif os.path.exists("imgs/"): http_header = "HTTP/1.0 200 OK\nContent-Type: image/jpeg\n" elif os.path.exists("css/"): http_header = "HTTP/1.0 200 OK\nContent-Type: text/css\n" else: http_header = "HTTP 404 NOT FOUND\n" # TO DO: handle all other headersv # TO DO: read the data from the file data = get_file_data(url) http_response = http_header + data client_socket.send(http_response) def validate_http_request(resource): """ Check if request is a valid HTTP request and returns TRUE / FALSE and the requested URL """ # TO DO: write functionv newresource = resource.split(" ") command = newresource[0] if command == "GET": return True, newresource[1] else: return False, "" def handle_client(client_socket): """ Handles client requests: verifies client's requests are legal HTTP, calls function to handle the requests """ print 'Client connected' while True: # TO DO: insert code that receives client requestv # ... client_request = client_socket.recv(1024) 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: Not a valid HTTP request' break print 'Closing connection' client_socket.close() def main(): # Open a socket and loop forever while waiting for clientsv 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: os.chdir("C:/webroot") client_socket, client_address = server_socket.accept() print 'New connection received' client_socket.settimeout(120) handle_client(client_socket) if __name__ == "__main__": # Call the main handler functionv main()I would really appreciate any help I can get, I'm really tired and want to end with this code
[Image: LlDwbab.png]
what I should get:
[Image: 98NScab.png]