Python:如何从BaseHTTPRequestHandler HTTP POST处理程序获取键/值对? [英] Python: How do I get key/value pairs from the BaseHTTPRequestHandler HTTP POST handler?

查看:2172
本文介绍了Python:如何从BaseHTTPRequestHandler HTTP POST处理程序获取键/值对?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

given the simplest HTTP server, how do I get post variables in a BaseHTTPRequestHandler?

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的值。谢谢!

I would simply like to able to get the values of param1 and param2. Thanks!

推荐答案

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处理程序获取键/值对?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆