Creating a Python webserver - layout and setup

2019-04-13 03:02发布

问题:

am I going about this in the correct way? Ive never done anything like this before, so im not 100% sure on what I am doing. The code so far gets html and css files and that works fine, but images wont load, and will I have to create a new "if" for every different file type? or am I doing this a silly way...here is what I have:

import string,cgi,time
from os import curdir, sep
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import os
import mimetypes

#import pri
port = 888
host = "0.0.0.0"

class MyHandler(BaseHTTPRequestHandler):

def do_GET(self):
    try:
        #RequestedURL = self.path
        mimeType = mimetypes.guess_type(self.path)[0]
        fileType = mimetypes.guess_extension(mimeType)
        infoList = [mimeType, fileType]

        if infoList[1] != ".py":
            self.send_response(200)
            self.send_header('Content-type', mimeType)
            self.end_headers()
            f = open(curdir + sep + self.path, "rb")
            self.wfile.write(f.read())
            f.close()
            return

        if fileType == ".py":
            pythonFilename = self.path.lstrip("/")
            self.send_response(200)
            self.send_header('Content-type',    'text/html')
            self.end_headers()
            pyname = pythonFilename.replace("/", ".")[:-3]
            print pythonFilename
            print pyname
            temp1 = pyname.split(".")
            temp2 = temp1[-1]
            print temp2
            module = __import__(root.index)
            self.wfile.write(module.root.index.do_work())
            #module = __import__("test.index")
            #self.wfile.write( module.index.do_work())
            return

        return

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


def do_POST(self):
    global rootnode
    try:
        ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
        if ctype == 'multipart/form-data':
            query=cgi.parse_multipart(self.rfile, pdict)
        self.send_response(301)

        self.end_headers()
        upfilecontent = query.get('upfile')
        print "filecontent", upfilecontent[0]
        self.wfile.write("<HTML>POST OK.<BR><BR>");
        self.wfile.write(upfilecontent[0]);

    except :
        pass

def main():
try:
    server = HTTPServer((host, port), MyHandler)
    print 'started httpserver:'
    print  ("Host: "  + (host))
    print  ("Port: "  + str(port))

    server.serve_forever()
except KeyboardInterrupt:
    print '^C received, shutting down server'
    server.socket.close()

if __name__ == '__main__':
main()

html and css works, but png images do not load

回答1:

You are on the right track with it, though your ifs are very redundant. I suggest you refactor the code to check for type using a loop and a dict:

mime = {"html":"text/html", "css":"text/css", "png":"image/png"}
if RequestedFileType in mime.keys():
    self.send_response(200)
    self.send_header('Content-type', mime[RequestedFileType])
    self.end_headers()
    print RequestedFileType
    f = open(curdir + sep + self.path)             
    self.wfile.write(f.read())              
    f.close()
    return

Also, you are sending binary files as text. Instead of open(curdir + sep + self.path) use open(curdir + sep + self.path, "b")

Gergely from toptal.com



回答2:

As to a panoply of if statements, the usual approach is to have a file that handles the mapping between extensions and mime types (look here: List of ALL MimeTypes on the Planet, mapped to File Extensions?). Read that into an appropriate data structure.

You should probably be opening all files as binary unless they are a text/* mime type; for those you should ensure that your line endings are as specified in the appropriate RFC (if any - it's been years since I have needed to consult those standards, on account of not writing a web server for deployment in anger).

And a syntactical point:

>>> ('foo')
'foo'
>>> ('foo',)
('foo',)
>>>

Your brackets are redundant. You can index on the value you are extracting.