Am I parsing this HTTP POST request properly?

前端 未结 3 1328
天涯浪人
天涯浪人 2021-01-20 19:02

Let me start off by saying, I\'m using the twisted.web framework. Twisted.web\'s file uploading didn\'t work like I wanted it to (it only included

3条回答
  •  春和景丽
    2021-01-20 19:52

    My solution to this Problem was parsing the content with cgi.FieldStorage like:

    class Root(Resource):
    
    def render_POST(self, request):
    
        self.headers = request.getAllHeaders()
        # For the parsing part look at [PyMOTW by Doug Hellmann][1]
        img = cgi.FieldStorage(
            fp = request.content,
            headers = self.headers,
            environ = {'REQUEST_METHOD':'POST',
                     'CONTENT_TYPE': self.headers['content-type'],
                     }
        )
    
        print img["upl_file"].name, img["upl_file"].filename,
        print img["upl_file"].type, img["upl_file"].type
        out = open(img["upl_file"].filename, 'wb')
        out.write(img["upl_file"].value)
        out.close()
        request.redirect('/tests')
        return ''
    

提交回复
热议问题