Python:如何从 BaseHTTPRequestHandler HTTP POST 处理程序获取键/值对?

标签 python http post

给定最简单的 HTTP 服务器,如何在 BaseHTTPRequestHandler 中获取 post 变量?

from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer

class Handler(BaseHTTPRequestHandler):
    def do_POST(self):
        # post variables?!

server = HTTPServer(('', 4444), Handler)
server.serve_forever()

# test with:
# curl -d "param1=value1&param2=value2" http://localhost:4444

我只想获得 param1 和 param2 的值。谢谢!

最佳答案

def do_POST(self):
    ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
    if ctype == 'multipart/form-data':
        postvars = cgi.parse_multipart(self.rfile, pdict)
    elif ctype == 'application/x-www-form-urlencoded':
        length = int(self.headers.getheader('content-length'))
        postvars = cgi.parse_qs(self.rfile.read(length), keep_blank_values=1)
    else:
        postvars = {}
    ...

关于Python:如何从 BaseHTTPRequestHandler HTTP POST 处理程序获取键/值对?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4233218/

相关文章:

python - 使用python从html中提取文本

python - Jupyter Notebook 服务器的用户名和密码两因素身份验证

html - Apache 中的重定向循环

delphi - shellexecute 无法为某些用户打开 http 链接

node.js - 如何限制 Node 中简化HTTP请求的内容长度响应?

json - 使用 jQuery 发布 json 来表达

Python多处理存储数据直到在每个进程中进一步调用

node.js - Angular 4 HTTP GET 不包括 HTTP header 中用于授权的 JWT

rest - RESTful 发布多个实体的策略

python 3.X : How to insert new row in excel using Xlrd3 library