且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

自定义简单的Python HTTP服务器不提供css文件

更新时间:2023-10-17 11:36:58

it seems to be returning the html mimetype for all files:

self.send_header('Content-type', 'text/html')

Also, it seems to be pretty bad. Why are you interested in this sucky server? Look at cherrypy or paste for good python implementations of HTTP server and a good code to study.


EDIT: Trying to fix it for you:

import os
import mimetypes

#...

    def do_GET(self):
        try:

            filepath = self.path
            print filepath, USTAW['rootwww']

            f = open(os.path.join('.', 'www', filepath))

        except IOError:
            self.send_error(404,'File Not Found: %s ' % filepath)

        else:
            self.send_response(200)
            mimetype, _ = mimetypes.guess_type(filepath)
            self.send_header('Content-type', mimetype)
            self.end_headers()
            for s in f:
                self.wfile.write(s)