Am I parsing this HTTP POST request properly?

前端 未结 3 1303
天涯浪人
天涯浪人 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:46

    You're trying to avoid reading documentation, but I think the best advice is to actually read:

    • rfc 2388 Returning Values from Forms: multipart/form-data
    • rfc 1867 Form-based File Upload in HTML

    to make sure you don't miss any cases. An easier route might be to use the poster library.

    0 讨论(0)
  • 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 ''
    
    0 讨论(0)
  • 2021-01-20 20:04

    The content-disposition header has no defined order for fields, plus it may contain more fields than just the filename. So your match for filename may fail - there may not even be a filename!

    See rfc2183 (edit that's for mail, see rfc1806, rfc2616 and maybe more for http)

    Also I would suggest in these kind of regexps to replace every space by \s*, and not to rely on character case.

    0 讨论(0)
提交回复
热议问题