python – 扩展BaseHTTPRequestHandler – 获取发布的数据
发布时间:2020-12-20 12:18:20 所属栏目:Python 来源:网络整理
导读:我已经看到了 this问题,但我希望能够从处理程序访问外部POST的数据. 有没有办法做到这一点? 以下是代码: import BaseHTTPServerHOST_NAME = ''PORT_NUMBER=8088postVars = ''class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler): def do_POST(s): s.s
我已经看到了
this问题,但我希望能够从处理程序访问外部POST的数据.
有没有办法做到这一点? 以下是代码: 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在Handler期间被评估,但不在MyHandler之后 解决方法
这是因为postVars在HTTPServer创建的MyHandler实例中受本地影响.如果要访问它,请在do_POST方法的开头将postVars声明为全局变量.
def do_POST(s): global postVars s.send_response(200) s.end_headers() varLen = int(s.headers['Content-Length']) postVars = s.rfile.read(varLen) 无论如何,我不确定你想通过在服务器和requestHandler上下文之外使用变量来实现什么. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |