do_POST 方法在 Python 3.6 中失败

do_POST method failing in Python 3.6

我一直在尝试在 Python 3.6 中制作一个基本的多部分表单。 do_GET 方法运行良好,但 do_POST 方法一直失败。

当我在 Chrome 中提交表单时,它说 localhost 没有发送任何数据。 ERR_EMPTY_RESPONSE,但是当我在 Developer Console 中查看“网络”选项卡时,我可以看到表单值。

该代码似乎与 Python 2.7 完美配合。我不确定代码哪里出错了。

这是我写的代码:

from http.server import BaseHTTPRequestHandler, HTTPServer
import cgi


class WebServerHandle(BaseHTTPRequestHandler):
    def do_GET(self):
        try:
            if self.path.endswith("/new"):
                self.send_response(200)
                self.send_header('Content-type', 'text/html')
                self.end_headers()
                output = ""
                output += "<html><head><style>body {font-family: Helvetica, Arial; color: #333}</style></head>"
                output += "<body><h2>Add new Restaurant</h2>"
                output += "<form method='POST' enctype='multipart/form-data' action='/new'>"
                output += "<input name='newRestaurantName' type='text' placeholder='New Restaurant Name'> "
                output += "<input type='submit' value='Add Restaurant'>"
                output += "</form></html></body>"
                self.wfile.write(bytes(output, "utf-8"))
                return
            if self.path.endswith("/restaurant"):
                self.send_response(200)
                self.send_header('Content-type', 'text/html')
                self.end_headers()
                output = ""
                output += "<html><head><style>body {font-family: Helvetica, Arial; color: #333}</style></head>"
                output += "<body><h3>Restaurant name added successfully!</h3>"
                output += "</html></body>"
                self.wfile.write(bytes(output, "utf-8"))
                return
        except IOError:
            self.send_error(404, 'File Not Found: %s' % self.path)

    def do_POST(self):
        try:
            if self.path.endswith("/new"):
                ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
                if ctype == 'multipart/form-data':
                    fields = cgi.parse_multipart(self.rfile, pdict)
                    restaurant_name = fields.get('newRestaurantName')
                    print("Restaurant name is ", restaurant_name)
                    self.send_response(301)
                    self.send_header('Content-type', 'text/html')
                    self.send_header('Location', '/restaurant')
                    self.end_headers()
        except:
            print("Something went wrong, inside exception..")


def main():
    try:
        server = HTTPServer(('', 8080), WebServerHandle)
        print("Starting web server on the port 8080..")
        server.serve_forever()
    except KeyboardInterrupt:
        print('^C entered. Shutting down the server..')
        server.socket.close()

if __name__ == '__main__':
    main()

更改为从表单解码字段值。

self.headers.getheader('content-type') 方法更改为

self.headers.('content-type').

然后添加以下行,解码 pdict 值:

pdict['boundary'] = bytes(pdict['boundary'], "utf-8"),

然后为了通过从字节转换为字符串来打印字段值,我将 print 行更改为

print("Restaurant name is ", restaurant_name[0].decode("utf-8")).

因此最终代码如下所示:

    def do_POST(self):
        try:
            if self.path.endswith("/new"):
                ctype, pdict = cgi.parse_header(self.headers['content-type'])
                pdict['boundary'] = bytes(pdict['boundary'], "utf-8")
                if ctype == 'multipart/form-data':
                    fields = cgi.parse_multipart(self.rfile, pdict)
                    print("Fields value is", fields)
                    restaurant_name = fields.get('newRestaurantName')
                    print("Restaurant name is ", restaurant_name[0].decode("utf-8"))
                    self.send_response(301)
                    self.send_header('Content-type', 'text/html')
                    self.send_header('Location', '/restaurant')
                    self.end_headers()
        except:
            print("Inside the exception block")

当我 运行 你的最终代码时,同样的问题再次发生“本地主机没有发送任何数据”。

你必须在下面添加content_len = int(self.headers.get('Content-length')) ctype, pdict = cgi.parse_header(self.headers['content-type'])。然后,您需要在 pdict['boundary'] = bytes(pdict['boundary'], "utf-8") 下方添加 pdict['CONTENT-LENGTH'] = content_len。还有一个缩进错误。

最终代码:

from http.server import BaseHTTPRequestHandler, HTTPServer
import cgi


class WebServerHandle(BaseHTTPRequestHandler):
    def do_GET(self):
        try:
            if self.path.endswith("/new"):
                self.send_response(200)
                self.send_header('Content-type', 'text/html')
                self.end_headers()
                output = ""
                output += "<html><head><style>body {font-family: Helvetica, Arial; color: #333}</style></head>"
                output += "<body><h2>Add new Restaurant</h2>"
                output += "<form method='POST' enctype='multipart/form-data' action='/new'>"
                output += "<input name='newRestaurantName' type='text' placeholder='New Restaurant Name'> "
                output += "<input type='submit' value='Add Restaurant'>"
                output += "</form></html></body>"
                self.wfile.write(bytes(output, "utf-8"))
                return
            if self.path.endswith("/restaurant"):
                self.send_response(200)
                self.send_header('Content-type', 'text/html')
                self.end_headers()
                output = ""
                output += "<html><head><style>body {font-family: Helvetica, Arial; color: #333}</style></head>"
                output += "<body><h3>Restaurant name added successfully!</h3>"
                output += "</html></body>"
                self.wfile.write(bytes(output, "utf-8"))
                return
        except IOError:
            self.send_error(404, 'File Not Found: %s' % self.path)

    def do_POST(self):
        try:
            if self.path.endswith("/new"):
                ctype, pdict = cgi.parse_header(self.headers['content-type'])
                content_len = int(self.headers.get('Content-length'))
                pdict['boundary'] = bytes(pdict['boundary'], "utf-8")
                pdict['CONTENT-LENGTH'] = content_len
                if ctype == 'multipart/form-data':
                    fields = cgi.parse_multipart(self.rfile, pdict)
                    print("Fields value is", fields)
                    restaurant_name = fields.get('newRestaurantName')
                self.send_response(301)
                self.send_header('Content-type', 'text/html')
                self.send_header('Location', '/restaurant')
                self.end_headers()

        except:
            print("Inside the exception block")


def main():
    try:
        server = HTTPServer(('', 8080), WebServerHandle)
        print("Starting web server on the port 8080..")
        server.serve_forever()
    except KeyboardInterrupt:
        print('^C entered. Shutting down the server..')
        server.socket.close()


if __name__ == '__main__':
    main()