Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending BaseHTTPRequestHandler - getting the posted data

I've seen this question, but I want to be able to access the data that's POST'd, external from the handler.

Is there a way to do this?

Following is the code:

import BaseHTTPServer

HOST_NAME = ''
PORT_NUMBER=8088

postVars = ''

class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):

    def do_POST(s):
        s.send_response(200)
        s.end_headers()
        varLen = int(s.headers['Content-Length'])
        postVars = s.rfile.read(varLen)
        print postVars

server_class = BaseHTTPServer.HTTPServer
httpd = server_class((HOST_NAME, PORT_NUMBER), MyHandler)

try:
    httpd.handle_request()
except KeyboardInterrupt:
    pass

print postVars
httpd.server_close()

postVars is valued during the Handler, but not after MyHandler

like image 882
KevinDTimm Avatar asked Jun 01 '11 15:06

KevinDTimm


2 Answers

This because the postVars is locally affected in the MyHandler instance created by the HTTPServer. If you want to access it, declare postVars as a global variable at the start of do_POST method.

def do_POST(s):
  global postVars
  s.send_response(200)
  s.end_headers()
  varLen = int(s.headers['Content-Length'])
  postVars = s.rfile.read(varLen)

Anyway, I'm not sure what you want to achieve by using variables outside the server and requestHandler context.

like image 58
Kaltezar Avatar answered Oct 23 '22 14:10

Kaltezar


The BaseHTTPServer is available as an attribute on the BaseHTTPRequestHandler. That means that you can make the postVars available on the server.

If you subclass the BaseHTTPServer like:

class MyServer(BaseHTTPServer.HTTPServer):
    def __init__(self, *args, **kwargs):
         # Because HTTPServer is an old-style class, super() can't be used.
         BaseHTTPServer.HTTPServer.__init__(self, *args, **kwargs)
         self.postVars = None

You can use this server to save the postVars on the server like:

class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):

    def do_POST(s):
        s.send_response(200)
        s.end_headers()
        varLen = int(s.headers['Content-Length'])
        s.server.postVars = s.rfile.read(varLen)
        print postVars

And then off course use MyServer instead of BaseHTTPServer:

server_class = MyServer
httpd = server_class((HOST_NAME, PORT_NUMBER), MyHandler)

print httpd.postVars

In general that solution would be preferable to having a global variable.

like image 44
BakaKuna Avatar answered Oct 23 '22 14:10

BakaKuna