简体   繁体   English

Python读取多行发布数据

[英]Python read multiline post data

I'm using BaseHTTPRequestHandler to implement my httpserver. 我正在使用BaseHTTPRequestHandler来实现我的httpserver。 How do a I read a multiline post data in my do_PUT/do_POST? 我如何在do_PUT / do_POST中读取多行发布数据?

Edit: I'm trying to implement a standalone script which sevices some custom requests, something like listener on a server, which consolidates/archives/extracts from various log files, I don't want implement something which requires a webserver, I don't have much experience in python, I would be grateful if someone could point any better solution. 编辑:我正在尝试实现一个独立的脚本,该脚本可以为一些自定义请求提供服务,例如服务器上的侦听器,它可以合并/归档/从各种日志文件中提取内容,我不想实现一些需要Web服务器的东西,我不希望这样做在python方面没有很多经验,如果有人可以提供更好的解决方案,我将不胜感激。

Edit2: I can't use any external libraries/modules, I have to make do with plain vanilla python 2.4/java1.5/perl5.8.8, restrictive policies, my hands are tied Edit2:我不能使用任何外部库/模块,我必须处理普通的python 2.4 / java1.5 / perl5.8.8,限制性策略,我的双手被束缚

Getting the request body is as simple as reading from self.rfile , but you'll have to know how much to read if the client is using Connection: keep-alive . 获取请求主体就像从self.rfile读取一样简单,但是如果客户端使用Connection: keep-alive则必须知道要读取多少内容。 Something like this will work if the client specifies the Content-Length header... 如果客户端指定Content-Length标头,则类似的事情将起作用。

from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler

class RequestHandler(BaseHTTPRequestHandler):

    def do_POST(self):
        content_length = int(self.headers['Content-Length'])
        post_data = self.rfile.read(content_length)
        print post_data

server = HTTPServer(('', 8000), RequestHandler)
server.serve_forever()

...although it gets more complicated if the client sends data using chunked transfer encoding . ...虽然客户端使用分块传输编码发送数据会变得更加复杂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM