我正在将Python2.6应用程序转换为Python3应用程序,但我被服务器卡住了。我已经设法让它能够很好地处理get请求,但POST继续躲避着我。这是我在2.6版本中开始使用的,但在3.x版本中,正常的服务器不处理POST请求。根据我对Python手册的阅读,我似乎必须使用CGI服务器类,并将脚本映射到该目录。我不想这样做,但我找不到其他方法。我是不是遗漏了什么?
def do_POST(self):
ctype, pdict = cgi.parse_header(self.headers.get('content-type'))
if ctype == 'multipart/form-data':
query = cgi.parse_multipart(self.rfile, pdict)
self.send_response(301)
self.end_headers()
upfilecontent = query.get('upfile')
print("filecontent", upfilecontent[0])
self.wfile.write("<HTML>POST OK.<BR><BR>");
self.wfile.write(upfilecontent[0]);
发布于 2010-01-24 04:11:22
经过戳和几个多小时的谷歌搜索,我发现了以下作品。
def do_POST(self):
length = int(self.headers['Content-Length'])
post_data = urllib.parse.parse_qs(self.rfile.read(length).decode('utf-8'))
# You now have a dictionary of the post data
self.wfile.write("Lorem Ipsum".encode("utf-8"))
我很惊讶这是多么的简单。
https://stackoverflow.com/questions/2121481
复制相似问题