0

I'm stumped. The below code will return the index.html as well as the css asked for, but when the index.html is requested by Firefox, it won't render the page with the css applied. I have the same result in Konqueror (which in Chrome based)

class MyServer(BaseHTTPRequestHandler):
    def do_GET(self):
        if (self.path.endswith("/")):
            self.send_response(200)
            self.send_header("Content-type", "text/html")
            self.end_headers()
            with open('index.html', 'r') as file:
                template = file.read()
            self.wfile.write(bytes(eval_template(template, env).encode("utf-16")))
        
        elif (self.path.endswith(".css")):
            with open(os.path.join('.', self.path[1:]), 'rb') as file:
                response = file.read()
            self.send_response(200)
            self.send_header("Content-type", "text/css")
            self.end_headers()
            self.wfile.write(response)

The css is referenced in the index.html in this manner:

<link rel="stylesheet" type="text/css" href="style.css" />

When I look in the developer console, I can verify that the request for style.css file went without issue (200 OK).

When I open the file with Firefox as a file, it sees the css as well in the same directory and renders the file with the css as intended, so I'm inclined to think my code above is missing something.

Thanks for any help with this.

7
  • Try affixing something to the end of the css filename, e.g. href="style.css?xyz". If that resolves the issue it means the css file was stuck in the cache. For a permanent solution, affix something to it dynamically, such as the time, while in development. Commented Mar 29, 2024 at 15:41
  • Didn't help. Also, if a cached css was applied, I would as least expect to see its effects. Commented Mar 29, 2024 at 15:53
  • "When I open the file with Firefox as a file, it sees the css as well in the same directory and renders the file with the css as intended" ... where then are you having a problem? Commented Mar 29, 2024 at 15:57
  • When I run the code above and point my browsers to it, via localhost:8080. Commented Mar 29, 2024 at 15:59
  • Why do you write the css to the pipe differently from the html? I'm just curious, i'm not questioning your approach. Regarding these self.wfile.write() Commented Mar 29, 2024 at 16:02

1 Answer 1

0

After GetSet pointed me in the right direction, it appeared changing the css write line to:

self.wfile.write(bytes(response))

solve the problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.