相关疑难解决方法(0)

cgi.parse_multipart函数在Python 3中抛出TypeError

我正在尝试从Udacity的Full Stack Foundations课程中进行练习.我do_POST在我的子类中有方法BaseHTTPRequestHandler,基本上我想获得一个名为messagesubmit with multipart form 的post值,这是方法的代码:

def do_POST(self):
    try:
        if self.path.endswith("/Hello"):
            self.send_response(200)
            self.send_header('Content-type', 'text/html')
            self.end_headers
            ctype, pdict = cgi.parse_header(self.headers['content-type'])
            if ctype == 'multipart/form-data':
                fields = cgi.parse_multipart(self.rfile, pdict)
                messagecontent = fields.get('message')
            output = ""
            output += "<html><body>"
            output += "<h2>Ok, how about this?</h2>"
            output += "<h1>{}</h1>".format(messagecontent)
            output += "<form method='POST' enctype='multipart/form-data' action='/Hello'>"
            output += "<h2>What would you like to say?</h2>"
            output += "<input name='message' type='text'/><br/><input type='submit' value='Submit'/>"
            output += "</form></body></html>"
            self.wfile.write(output.encode('utf-8'))
            print(output)
            return
    except: …
Run Code Online (Sandbox Code Playgroud)

forms cgi python-3.x

8
推荐指数
2
解决办法
3149
查看次数

如何访问 MultipartDecoder 中的字段名称

我正在使用request-toolbelt解码通过 HTTP POST 请求提交的表单字段。我成功实例化了此处MultipartDecoder描述的类似内容。现在我想通过发送请求时指定的名称来访问表单字段。

我能够得到这样的字段的名称

from requests_toolbelt.multipart import decoder
multipart_string = b"--ce560532019a77d83195f9e9873e16a1\r\nContent-Disposition: form-data; name=\"author\"\r\n\r\nJohn Smith\r\n--ce560532019a77d83195f9e9873e16a1\r\nContent-Disposition: form-data; name=\"file\"; filename=\"example2.txt\"\r\nContent-Type: text/plain\r\nExpires: 0\r\n\r\nHello World\r\n--ce560532019a77d83195f9e9873e16a1--\r\n"
content_type = "multipart/form-data; boundary=ce560532019a77d83195f9e9873e16a1"
decoder = decoder.MultipartDecoder(multipart_string, content_type)
field_name = decoder.parts[0].headers[b'Content-Disposition'].decode().split(';')[1].split('=')[1]
Run Code Online (Sandbox Code Playgroud)

但这似乎是完全错误的。访问表单字段名称的常用方法是什么?

multipartform-data python-requests

7
推荐指数
1
解决办法
5149
查看次数

我怎样才能在python cgi中找到上传的文件名

我制作了如下简单的网络服务器。

import BaseHTTPServer, os, cgi
import cgitb; cgitb.enable()

html = """
<html>
<body>
<form action="" method="POST" enctype="multipart/form-data">
File upload: <input type="file" name="upfile">
<input type="submit" value="upload">
</form>
</body>
</html>
"""
class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header("content-type", "text/html;charset=utf-8")
        self.end_headers()
        self.wfile.write(html)

    def do_POST(self):
        ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
        if ctype == 'multipart/form-data':
            query = cgi.parse_multipart(self.rfile, pdict)
            upfilecontent = query.get('upfile')
            if upfilecontent:
                # i don't know how to get the file name.. so i named it 'tmp.dat'
                fout = file(os.path.join('tmp', 'tmp.dat'), 'wb')
                fout.write (upfilecontent[0])
                fout.close() …
Run Code Online (Sandbox Code Playgroud)

python cgi

3
推荐指数
2
解决办法
1万
查看次数