我想知道如何使用位于外部服务器上的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))
感谢您的评论和帮助。
答案 0 :(得分:0)
#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() )