BaseHTTPServer,从 POST 单个值中提取变量

BaseHTTPServer, extracting variable from POST single value

我想知道如何从位于外部服务器上的 html 表单发布的内容中获取变量。

我有这个代码:

myserver.py

import BaseHTTPServer

HOST_NAME = ''
PORT_NUMBER=8000

postVars = ''

class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):

    def do_POST(s):
        global postVars
        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

它目前在控制台上打印这个

user=jhon&domain=domain.com&pass=mypassword

我想提取每个变量并使用它们调用后续的 bash 脚本。

我想要完成的示例:

import os
value1="user"
value2="domain"
value3="pass"
os.system("./usersprivetes.sh %s %s %s" % (value1,value2,value3))

感谢您的评论和帮助。

你好通过添加一些代码解决了,一个只为我服务一次,在变量传回后不再起作用,我认为这就是我的顺序

#cat server.py
import BaseHTTPServer
import urlparse
import os

HOST_NAME = ''
PORT_NUMBER=8000

postVars = ''

class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):

    def do_POST(s):
        global postVars
        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

qs = dict( (k, v if len(v)>1 else v[0] )
           for k, v in urlparse.parse_qs(postVars).iteritems() )
#print qs

pase = qs['pass']
dominio = qs['domain']
usuario = qs['user']

os.system("./createuser.sh %s %s %s" % (pass,user,domain))
httpd.serve_forever()

谢谢 这创造了奇迹

qs = dict( (k, v if len(v)>1 else v[0] )
           for k, v in urlparse.parse_qs(postVars).iteritems() )