Python Forum
HTML file uploaded through python screen doesn't look as expected
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
HTML file uploaded through python screen doesn't look as expected
#1
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
# 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
additionally, here is what I see and should see:
[Image: LlDwbab.png]

what I should get:
[Image: 98NScab.png]
Reply
#2
What is the name of the book, and exercise number?
or alternatively, post the assignment verbatim.
Reply
#3
(Aug-03-2018, 04:39 PM)Larz60+ Wrote: What is the name of the book, and exercise number?
or alternatively, post the assignment verbatim.
The ebook is in hebrew
assignment at page 93
The assigment is to write a code which eventually allow me to browse a website when I write the ipaddress 127.0.0.1:80 (port 80)

1) write a server which waits for the client in TCP protocol at port 80, after the client closes the connection the code ends
2)add support for subsequent connections (once one client closes the server will wait for another client)
3)verify that the packet is HTTP GET, therefore the message which will arrive is a string which starts with GET (space) URL (space) PROTOCOL_VERSION (HTTP/1.1) and at the end /r and /n
4) in case the server receives HTTP GET correctly and there is a file name, return the file name, at this moment do not send the file but only the name
* notice that in windows you use "\" and in linux and the browser you use "/"
note: the file name you have to send as a requested resource name and not in a seperate Header
* note: at this moment do not send HTTP headers like version and response code
5) now return the file itself (its contents)
*if the file doesn't exist just close the connection (([[ for some reason they tell me now to do it.. but later tell me to redirect to index.html..which confused me]]))
* unlike the previous assignments, here you have to send the whole file and not in parts
6)add response line and HTTP Header
*HTTP version 1.0
response code 200 (OK)
the line Content-Length (fill it with the size of the file)
7) in case there is no such requested file return code 404 (not found)
8) if the server gets a GET request to root ("/") then return the file index.html, of course confirm this file exists
9)if the header receives requests with different file types add to the header Content Type for the file type: you can use the following infos
files with ending txt or html:
Content-Type: txt/html; charset=utf-8
files with ending jpg:
Content-Type: image/jpeg
files with ending js:
Content-Type: text/javascript; charset=UTF8
files ending with css:
Content-Type: text/css

10) now add support Status Codes (use the help of wikipedia)
1. 403 forbidden - add additional files which you will not grant access to them
2. 302 moved temporarily - add files which had their located moved, and by this let the redirect to the new location and at the end they will receive OK 200
3. 500 internal server error - in case the server receive a request it doesn't understand instead of closing the connection return code 500

tips: notice that the field you return are all by the field of HTTP do not miss any spaces or new lines
2. sometimes it might happen that the server and the browser are expecting information, to avoid this situtation you can define SOCKET_TIMEOUT, notice that if the time you set has passed you'll receive exception, you'll have to deal with it so the server won't stop the run.



this is it by the best way I could translate (by myself without translators)

i'll add link for the ebook and the additional files they gave for the server:

ebook: the ebook
the files : DirectDownload

additional python shell with TODO on each function:
Directdownload

understanding what I didn't do right is very important for me as I need this code to advance farther
Thank you very much for the help
Reply
#4
The best I can do is point you to some documentation, here: https://docs.python.org/3.4/howto/sockets.html
and perhaps: https://users.cs.duke.edu/~chase/cps196/...ockets.pdf
Hope this proves useful
Reply
#5
Oh, writing a webserver from scratch.
Handling sockets is not easy, but solvable with less code in Python.
But your code is for Python 2.7. Support ends in year 2020

Secret Tip: python3 -m http.server

You see garbage on the screen, because even the request is not finished. All other files are not loaded.
So JavaScript, CSS and other content is missing.

Normally each request is a new independent connection.
After a file has been transferred, close the socket and wait for the next new connection.
Set a webroot variable, but don't change into the webroot. Use instead os.path.join(webroot, path_from_url) to get the requested file path.

Converting your programm to Python3 needs handwork, because since Python 3 we differ between str (unicode) and bytes (raw bytes).
Sockets are using bytes. So all strings must be encoded or prefixed by b'your string'.
I tried to run your Script and got it so far, that the first request is completed with status 200.
After this the programm crashes, because you're reusing the old socket. I closed the socket to finish the first request.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
(Aug-03-2018, 06:17 PM)Larz60+ Wrote: The best I can do is point you to some documentation, here: https://docs.python.org/3.4/howto/sockets.html
and perhaps: https://users.cs.duke.edu/~chase/cps196/...ockets.pdf
Hope this proves useful

Thank you very appreciated, I have a the main reason for my failure is the way I construct the response.. I have to read more about HTTP
Quote:Oh, writing a webserver from scratch.
Handling sockets is not easy, but solvable with less code in Python.
But your code is for Python 2.7. Support ends in year 2020

Secret Tip: python3 -m http.server

You see garbage on the screen, because even the request is not finished. All other files are not loaded.
So JavaScript, CSS and other content is missing.

Normally each request is a new independent connection.
After a file has been transferred, close the socket and wait for the next new connection.
Set a webroot variable, but don't change into the webroot. Use instead os.path.join(webroot, path_from_url) to get the requested file path.

Converting your programm to Python3 needs handwork, because since Python 3 we differ between str (unicode) and bytes (raw bytes).
Sockets are using bytes. So all strings must be encoded or prefixed by b'your string'.
I tried to run your Script and got it so far, that the first request is completed with status 200.
After this the programm crashes, because you're reusing the old socket. I closed the socket to finish the first request.

Thank you, My code is quite broken.. I barely managed to run it
I'd use python 3, but the ebook is on python 2, and they also provided me with all the softwares.. I'm disappointed.. but got to use what I can
I'll try to reanalyze what you've said.. it's just that i'm very very tired right now
my closed conclusion is that I do not construct my GET request correctly.. maybe i'm missing some letters, spaces or blank lines to make sure the browser knows how to differet between the info.

PS: If I want the browser to print basic text, I need it to look like html file or I can simply send the text after the 200 response?
I seem very uneducated in this field.. I'll re-read more about http to become better at solving my problems. would still apreciate any assistance, any advice helps
a little update: I ran another project I wrote in minutes, almost the save structure but instead it prints to the browser an area of a triangle if I send it a height and width..my browser looped like there is no response from the server, but once the timeout function triggered the number showed and the server closed connection..
I might be close to the solution (Sadly I really must get some sleep)
Reply
#7
Just an FYI, the free book of the day at Packt Pub just happens to be Mastering Python Networking
you can get it here (good for next 19 hours): https://www.packtpub.com/packt/offers/free-learning
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  file doesn't exist mcgrim 39 15,675 Oct-24-2019, 12:43 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020