cgi lib question

Robert Brewer fumanchu at amor.org
Thu Mar 18 11:35:16 EST 2004


Gandalf wrote:
> I'm using IIS with Python (as an ActiveScript language). I 
> would like to upload files with multipart/form-data.

No time for analysis at the moment, but here's a working reader from an
app of mine--maybe you can hack it to your purposes:

    def read(self, request):
        """Parse CGI params into a paramset."""
        self.requestParams.clear()
        
        if str(request.ServerVariables("REQUEST_METHOD")).upper() ==
'POST':
            # Note that although 'name' is Unicode, there's no good way
            # to handle non-ASCII chars in the values--you'll just
            # have to make sure all chars over ord(127) get escaped
            # via something like urllib.quote_plus().
            env = {}
            for name in request.ServerVariables:
                try:
                    env[name] = str(request.ServerVariables(name))
                except UnicodeEncodeError, x:
                    if name not in self.skipVarsIfErrors:
                        raise StandardError("Server Variable '%s' could
not be decoded." % name)
            
            contentLength = int(env['CONTENT_LENGTH'])
            content, size = request.BinaryRead(contentLength)
            
            content = StringIO.StringIO(content)
            form = cgi.FieldStorage(content, None, "", env, True)
            content.close()
            
            for key in form:
                value = form[key]
                filename = value.filename
                if filename:
                    filename = self.unicode(filename)
                    key = self.unicode(key)
                    self.requestParams[key] = (filename, value.value)
                else:
                    for subValue in form.getlist(key):
                        key = self.unicode(key)
                        subValue = self.unicode(subValue)
                        self.requestParams[key] = subValue
        else:
            content = self.unicode(request.QueryString())
            inParams = cgi.parse_qs(content, True, False)
            for eachName, eachValue in inParams.items():
                for eachSubValue in eachValue:
                    self.requestParams[eachName] = eachSubValue


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org




More information about the Python-list mailing list