4

I'm writing a pretty basic webserver (well, trying) and while it's now serving HTML fine, my CSS files don't seem to be recognized at all. I have Apache2 running on my machine as well, and when I copy my files to the docroot, the pages are served correctly. I've also checked permissions and they seems to be fine. Here's the code I have so far:

class MyHandler(BaseHTTPRequestHandler):
     def do_GET(self):
           try:
                if self.path == "/":
                     self.path = "/index.html"
                if self.path == "favico.ico":
                     return
                if self.path.endswith(".html"):
                     f = open(curdir+sep+self.path)
                     self.send_response(200)
                     self.send_header('Content-type', 'text/html')
                     self.end_headers()
                     self.wfile.write(f.read())
                     f.close()
                     return
                return
            except IOError:
                self.send_error(404)
      def do_POST(self):
            ...

Is there anything special I need to be doing in order to serve CSS files?

Thanks!

2 Answers 2

7

You could add this to your if clause

            elif self.path.endswith(".css"):
                 f = open(curdir+sep+self.path)
                 self.send_response(200)
                 self.send_header('Content-type', 'text/css')
                 self.end_headers()
                 self.wfile.write(f.read())
                 f.close()
                 return

Alternatively

import os
from mimetypes import types_map
class MyHandler(BaseHTTPRequestHandler):
    def do_GET(self):
       try:
            if self.path == "/":
                 self.path = "/index.html"
            if self.path == "favico.ico":
                 return
            fname,ext = os.path.splitext(self.path)
            if ext in (".html", ".css"):
                 with open(os.path.join(curdir,self.path)) as f:
                     self.send_response(200)
                     self.send_header('Content-type', types_map[ext])
                     self.end_headers()
                     self.wfile.write(f.read())
            return
        except IOError:
            self.send_error(404)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I figured it was something like this, but wasn't sure if I needed a separate case. Added this and it worked like a charm!
This was very instructive code for me -- thanks, gnibbler. One minor note: in python 2.7 at least, it's mimetypes.types_map (plural), not .type_map
0

You need to add a case that handles css files. Try changing:

if self.path.endswith(".html") or self.path.endswith(".css"):

2 Comments

Well, you'll probably want to add it as a separate case -- you'll want to set the Content-Type differently for CSS. But this should get you started.
Thanks for the reply. I ended up adding a separate case and it worked like a charm.

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.