Understanding httpd Request Handler's do_GET

I’m trying my first attempt to write an http server. I am baffled by do_GET and cannot find any worth while examples of how to use it. The documentation, to me, is severely lacking. I think my main problem is I don’t understand how css and js files are served out or links are handled. I found a simple example which I copied and while the main page is displayed, neither the css nor js files are included. Unfortunately, the example is too simple and does not show how do_GET really works but it is the only one I could find.

class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):

def do_GET(self):
    self.send_response(200)
    self.end_headers()
    fh=open('jindex.html','rb')
    string=fh.read()
    self.wfile.write(string)

I’m guessing that when a get request comes in for style.css that this is just reloading jindex.html. How do I get the requested filename so I can server it out? It also may be a directory issue as the directory containing the files is under the current directory as ‘html’. I do “os.chdir(’./html’)” and it finds the file but gives me an os.chdir error no such file or directory exists. If I leave that out it cannot find the html file.

TIA

I think I have figured out part of this. It seems for some reason the path variable includes a leading ‘/’ forcing the path to be absolute. If I remove that character I get closer but the relative path is still problematic. There must be some way, I am not finding, to set the document root so the leading ‘/’ works.

I guess my problem goes much deeper than just path. After researching it appears that I have to do my own translation of paths. I am able to serve out the primary page. However, that page also needs to pull in css and js files. When I just let my do_GET code output them, the browser apparently is not recognizing them. This is where the documentation fails big time. There must be a different way to handle those requests but I can find nothing that explains it. Since my code outputs those as a new page the browser does not actually add them as part of the original page. Is there no one here that knows how this works?

FWIW my current code is this:

class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):

def do_GET(self):
    fn='html/'+self.path[1:]
    if (fn=='html/'):
        fn+='jindex.html'
    print(fn)
    self.send_response(200)
    self.end_headers()
    fh=open(fn,'rb')
    string=fh.read()
    self.wfile.write(string)

I’m making some progress but I am still stuck. I realized I needed to set my own headers but although I think it is something I needed to do, I am still unable to get the browser to load the files. Here is my latest code:

    def do_GET(self):
    fn='html/'+self.path[1:]
    if (fn=='html/'):
        fn+='jindex.html'
    self.send_response(200)
    ftype=fn.split('.')[-1].lower()
    print(ftype)
    ctype=None
    if (ftype=='html'):
        ctype='text/html'
    elif (ftype=='css'):
        ctype='text/css'
    elif (ftype=='js'):
        ctype='text/javascript'
    elif (ftype=='json'):
        ctype='application/json'
    print(ctype)
    self.send_header('Content-type',ctype)
    self.end_headers()
    fh=open(fn,'rb')
    string=fh.read()
    self.wfile.write(string)

I still need help figuring out what I am still missing. TIA.

My bad. Apparently this was working. I was just expecting a different page than the one I got. Sorry.