2

I have created my own webserver using python and its built-in module sockets, now when I run the code the browser asks for favicon.ico earlier I was silencing it but now I decided to give a favicon.ico file to the browser but it doesn't seem to be working

server.py

...
SERVER_HOST = '0.0.0.0'
SERVER_PORT = 8000


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)
...
while True:
    client_connection, client_address = server_socket.accept()
    request = client_connection.recv(1024).decode()
    ...

    response = 'HTTP/1.1 200 OK\r\n'
    # the code here is a little bit clustered, I will fix it when I solve this favicon problem
    if filename == '/':
        filename = '/index.html'
    if filename != "/favicon.ico":
        response += "Content-Type: text/html\r\n"
        with open(f"htdocs/{filename}") as f:
            data = f.read()
        response += f"Content-Length: {len(data)}\r\n\r\n{data}"
        client_connection.sendall(response.encode())
    else:
        with open(f"htdocs/{filename}", "rb") as f:
            data = f.read()
        response += f"Content-Type: image/x-icon\r\nContent-Length: {len(data)}\r\n\r\n"
        response += (base64.b64encode(data)).decode('utf-8')
        client_connection.sendall(response.encode())
    client_connection.close()

Folder Structure

/Webserver$ ls
htdocs  server.py  venv
/Webserver$ ls htdocs
favicon.ico  home.html  index.html

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
      <meta charset="UTF-8">
      <title>Index</title>
      <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"/>
  </head>
  <body>
    <h1>Hello World!</h1>
    <p>Welcome to the index.html web page..</p>
    <p>Here's a link to <a href="home.html">home</a></p>
  </body>
</html>

enter image description here here the favicon.ico is even sent as image/x-icon but it is not displyed.

The htdocs/favicon.ico has dimensions 16x16

enter image description here

Ibrahim
  • 798
  • 6
  • 26
  • Perhaps your favicon.ico is defective. But you've got the basics wrong here. Headers are terminated with a CRLF (b'\r\n'), not just a LF. And python sockets require a more sophisticated recv strategy that you're using, as [this](https://stackoverflow.com/a/43420503/238704) answer explains. – President James K. Polk Feb 05 '22 at 13:56
  • The console tab in the browser debug view may contain more information about what is going wrong. – President James K. Polk Feb 05 '22 at 14:04
  • @PresidentJamesK.Polk I posted the image of the favicon file, I also added the `\r` (still the problem persists) and regarding about the link I can't seem to find a difference in the code give in the answer and the code I am using for the webserver except for closing the socket – Ibrahim Feb 05 '22 at 14:08
  • The link talks about why this line: `request = client_connection.recv(1024).decode()` is not correct. But that may or may not be the problem in this case, I'm just noticing that it's one thing that doesn't look right. Check the browser console debug view. – President James K. Polk Feb 05 '22 at 14:12
  • @PresidentJamesK.Polk how do I check the browser console debug view https://imgur.com/a/izMGvai, I don't see a debug option and cog wheel also doesn't has that option – Ibrahim Feb 05 '22 at 14:20
  • It's browser-specific, use a search engine to find this information for your browser. – President James K. Polk Feb 05 '22 at 14:26
  • @PresidentJamesK.Polk It seems like brave only has Verbose, Info, Warning and Errors, So I switched to firefox is this what you are talking about https://imgur.com/a/tU5HfQy – Ibrahim Feb 05 '22 at 14:40
  • Yes, that's what I was talking about. – President James K. Polk Feb 05 '22 at 14:43
  • @PresidentJamesK.Polk It spats out nothing I have tired to reload it multiple times by both enabling and disabling the debug view – Ibrahim Feb 05 '22 at 14:47
  • @PresidentJamesK.Polk you got any other idea? – Ibrahim Feb 05 '22 at 16:48
  • 1
    Why do you send the favicon as base64? It should be send as the original binary. – Steffen Ullrich Feb 05 '22 at 17:42
  • @SteffenUllrich by original binary do you mean the byte array that gets loaded from the file?I checked one of the sites and saw they convert it to base64 https://paste.pythondiscord.com/ojejerolup.kotlin – Ibrahim Feb 05 '22 at 17:44
  • 1
    @Good: nothing in what you link to suggest that the favicon should be encoded to base64 for a *HTTP response*. All it shows that it is encoded for *json* - simply because json cannot handle binary data. HTTP does handle binary data and cannot handle base64 encoding. – Steffen Ullrich Feb 05 '22 at 17:48
  • @SteffenUllrich Alright thanks for that it confused me so I used base64, and after just using binary data it works fine :) – Ibrahim Feb 05 '22 at 17:51

0 Answers0