Using cgi.FieldStorage with enctype=multipart/form-data in SimpleHTTPServer = nothing

Thomas Weholt thomas at weholt.org
Sun Feb 10 14:11:41 EST 2002


I'm using a simple webserver based BaseHTTPServer.HTTPServer and I want to
be able to post files to a page served by this server, using
ENCTYPE="multipart/form-data". I've got this code in my handler, subclassed
from  BaseHTTPServer.BaseHTTPRequestHandler :

def do_POST(self):
    # read in posted parameteres and data
    if self.headers.dict.has_key('content-length') and
string.lower(self.headers.dict['content-type']) ==
'application/x-www-form-urlencoded':
        print "Parsing data, content-type: %s" %
'application/x-www-form-urlencoded'
        content_length = string.atoi(self.headers.dict['content-length'])
        parameters = cgi.parse_qs(self.rfile.read(content_length))
        # do something
    else:
        print "Parsing multipart/form-data"
        import cgi
        fs = cgi.FieldStorage() # also tried : fs =
cgi.FieldStorage(self.rfile)
        # do something with the data in fs

When I use plain post, no ENCTYPE specified it works fine ( the first
if-sentence), but parsing input from multipart/form-data shows nothing in
the fs-object. I've looked into the data sent to the server and it's there,
but nothing seem to appear using the cgi-module. What is the easiest way of
doing this ( ie. uploading files to a webpage using these modules) ? How can
I parse the posted data manually, using mimetools etc. ?

Best regards,
Thomas Weholt







More information about the Python-list mailing list