Python3 http.server后检查

2024-05-18 23:26:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在将Python2.6应用程序转换为Python3应用程序,我被服务器卡住了。我已经设法让它服务的get请求只是罚款,但邮政继续躲避我。这是我在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]);

Tags: brself服务器getparsectypequerypost
1条回答
网友
1楼 · 发布于 2024-05-18 23:26:43

在戳和几个小时的谷歌搜索之后,我发现了以下的作品。

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"))

我很惊讶这么简单。

相关问题 更多 >

    热门问题