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
This because the
postVarsis locally affected in the MyHandler instance created by the HTTPServer. If you want to access it, declarepostVarsas a global variable at the start ofdo_POSTmethod.Anyway, I’m not sure what you want to achieve by using variables outside the server and requestHandler context.