开发者

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 the开发者_Go百科re 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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜