-1

I'm using BaseHTTPRequestHandler to implement my httpserver. How do a I read a multiline post data in my 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.

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

Rnet
  • 4,796
  • 9
  • 47
  • 83
  • 1
    can you please show your current research, homework, code etc.? – tenstar Jun 19 '13 at 13:55
  • @That1Guy post_data = urlparse(self.rfile.read(length).decode('utf-8')) returns a tuple, for multiline, each row is a tuple value, for single line (curl --data "test") the data is always the third tuple element, my question is at what point I should stop reading the tuple for multiline – Rnet Jun 19 '13 at 13:55
  • 1
    @user2433215 I wish I was that young for this to be my homework code, thank you – Rnet Jun 19 '13 at 13:56
  • Why are you trying to implement this yourself by extending BaseHttpRequestHandler? There are plenty of libraries to take care of this sort of thing for you, for example [webob](http://webob.org/) or [Werkzeug](http://werkzeug.pocoo.org/). – Daniel Roseman Jun 19 '13 at 14:00
  • @DanielRoseman unfortunately, I can't use any external libraries/modules, I have to make do with plain vanilla python 2.4, restrictive policies – Rnet Jun 19 '13 at 14:10

1 Answers1

5

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. Something like this will work if the client specifies the Content-Length header...

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.

Aya
  • 39,884
  • 6
  • 55
  • 55
  • Thanks, yes, now I see that my problem was chunked transfer encoding, I'm switching to writing a java servlet – Rnet Jun 19 '13 at 14:19