嗨我有这个代码用于我的变量帖子,我希望这总是收听和接收帖子请求,工作正常,但收到第一个变量后跟着脚本运行但不再获得帖子变量。
只运行一次,然后必须重启python脚本。
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
pass = qs['pass']
domain = qs['domain']
user = qs['user']
os.system("./createuser.sh %s %s %s" % (pass,user,domain))
httpd.serve_forever()
感谢所有
答案 0 :(得分:1)
错误发生在脚本的末尾。你为vars取了一些名字,但得到另一个。
更改此
pase = qs['pass']
dominio = qs['domain']
usuario = qs['user']
os.system("./createuser.sh %s %s %s" % (pass,user,domain))
httpd.serve_forever()
这个
passwd = qs['pass']
domain = qs['domain']
username = qs['user']
os.system("./createuser.sh %s %s %s" % (passwd, username, domain))
httpd.serve_forever()
只是错字: - )
答案 1 :(得分:1)
如果您致电serve_forever
,则无法在此主题中执行任何操作。在另一个线程或循环中执行任务并调用handle_request
。
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)
while True:
try:
httpd.handle_request()
except KeyboardInterrupt:
pass
qs = dict( (k, v if len(v)>1 else v[0] )
for k, v in urlparse.parse_qs(postVars).iteritems() )
pass = qs['pass']
domain = qs['domain']
user = qs['user']
os.system("./createuser.sh %s %s %s" % (pass,user,domain))