Search code examples
pythonsocketshttptcpget

Send image over http python


I need to build a http server without using an HTTP library.

I have the server running and an html page beeing loaded but my <img src="..."/> tags are not beeing loaded, I recive the call but cannot preset the png/JPEG in the page.

httpServer.py

# Define socket host and port
SERVER_HOST = '0.0.0.0'
SERVER_PORT = 8000

# Create socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((SERVER_HOST, SERVER_PORT))
server_socket.listen(1)
print('Listening on port %s ...' % SERVER_PORT)

while True:
    # Wait for client connections
    client_connection, client_address = server_socket.accept()

    # Handle client request
    request = client_connection.recv(1024).decode()
    content = handle_request(request)

    # Send HTTP response
    if content:
        response = 'HTTP/1.1 200 OK\n\n'
        response += content
    else:
        response = 'HTTP/1.1 404 NOT FOUND\n\nFile Not Found'

    client_connection.sendall(response.encode())
    client_connection.close()

# Close socket
server_socket.close()

Function where handles the call

 def handle_request(request):
    http = HttpHandler.HTTPHandler

    # Parse headers
    print(request)
    headers = request.split('\n')
    get_content = headers[0].split()

    accept = headers[6].split()
    type_content = accept[1].split('/')

    try:
        # Filename
        filename = get_content[1]

        if get_content[0] == "GET":
            content = http.get(None, get_content[1], type_content[0])


        return content
    except FileNotFoundError:
        return None

class to handle the http verbs

class HTTPHandler:

    def get(self, args, type):
        if args == '/':
            args = '/index.html'
            fin = open('htdocs' + args)
        if type != "image":
            fin = open('htdocs/' + args)

        if type == "image":
            fin = open('htdocs/' + args, 'rb')

        # Read file contents
        content = fin.read()
        fin.close()
        return content

Realize that I´m trying to make an HTTP 1.1, if you see anything out of pattern fell free to say thanks in advance.


Solution

  • httpServer.py

    Ended up like:

    while True:
    # Wait for client connections
    client_connection, client_address = server_socket.accept()
    
    # Handle client request
    request = client_connection.recv(10240).decode()
    content = handle_request(request)
    
    # Send HTTP response
    if content:
        if str(content).find("html") > 0:
            client_connection.send('HTTP/1.1 200 OK\n\n'.encode())
            client_connection.send(content.encode())
        else:
            client_connection.send('HTTP/1.1 200 OK\r\n'.encode())
            client_connection.send("Content-Type: image/jpeg\r\n".encode())
            client_connection.send("Accept-Ranges: bytes\r\n\r\n".encode())
            client_connection.send(content)
    else:
        response = 'HTTP/1.1 404 NOT FOUND\r\nFile Not Found'
    
    client_connection.close()
    

    And the Get method like:

    class HTTPHandler:
    
    def get(self, args, type):
        if args == '/':
            args = '/index.html'
            fin = open('htdocs' + args)
        if type != "image":
            fin = open('htdocs/' + args)
    
        if type.find("html") == -1:
            image_data = open('htdocs/' + args, 'rb')
            bytes = image_data.read()
    
            # Content-Type: image/jpeg, image/png \n\n
            content = bytes
            fin.close()
            return content
    
        # Read file contents
        content = fin.read()
        fin.close()
        return content