关于简单的Python Web服务器的问题

时间:2014-03-06 08:40:31

标签: python .htaccess

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

class MyHandler(BaseHTTPRequestHandler):

    def do_GET(self):
        try:
            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
            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(('', 80), MyHandler)
        print 'started httpserver...'
        server.serve_forever()
    except KeyboardInterrupt:
        print '^C received, shutting down server'
        server.socket.close()

if __name__ == '__main__':
    main()

upload.html

<HTML><BODY>

<form method='POST' enctype='multipart/form-data' action='http://127.0.0.1/'>

File to upload: <input type=file name=upfile><br>

<br>
<input type=submit value=Press> to upload the file!
</form>

</BODY>
</HTML>

问题

  1. 在upload.html中,操作是POST,因此POST会在服务器Python端转换为do_POST吗?

  2. Python服务器是否需要某种类型的htaccess规则,如Apache服务器?

  3. send_response(200)的含义是什么?

  4. 什么是self.wfile.write(f.read())

0 个答案:

没有答案