Tornado和subprocess实现和外部程序通信-创新互联
主要涉及subprocess的使用,简单点,直接上代码吧,只是做一下记录。
创新互联是一家集网站建设,扬中企业网站建设,扬中品牌网站建设,网站定制,扬中网站建设报价,网络营销,网络优化,扬中网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。import tornado.ioloop, os, random
import urllib, time
import tornado.web
import tornado.httpserver
import tornado.options
import subprocess
from tornado.options import options, define
from tornado.web import url, RequestHandler, MissingArgumentError
from tornado.escape import json_encode
##
# 功能:文件上传、代码提交、执行代码、停止代码
#
# 路由:ip:8000/
# 演示功能:打开上传文件或者上传文件的浏览器界面,点及上传或者提交代码
# 路由:ip:8000/upload
# post上传文件的接口,将所选的文件中的内容转储到服务器中的/tmp/new_code.py文件中
# 路由:ip:8000/send
# post提交代码的接口,代码作为字符串形式提交
# 路由:ip:8000/run/start
# 启动服务器中文件名为new_code.py的代码文件,新建线程执行
# 路由:ip:8000/run/stop
# 停止服务器中文件名为new_code.py所开启的线程
##
# 设置服务的端口
define("port", default=8000, type=int, help="run server on the given port.")
# 静态资源配置
settings = {
"static_path": os.path.join(os.path.dirname(__file__), "static"),
"template_path": os.path.join(os.path.dirname(__file__), "views"),
}
##
# 设置允许跨域请求的base类
class BaseHandler(tornado.web.RequestHandler):
def set_default_headers(self):
self.set_header("Access-Control-Allow-Origin", "*");
self.set_header("Access-Control-Allow-Headers", "*");
self.set_header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE"); #请求允许的方法
self.set_header("Access-Control-Max-Age", "3600");
#处理OPTIONS请求
def options(self):
#返回方法1
#self.set_status(204)
#self.finish()
#返回方法2
self.write('{"errorCode":"00","errorMessage","success"}')
##
# localhost:8000/ 打开浏览器输入,
class MainHandler(BaseHandler):
def get(self):
self.write('
self.write('') #
#
# 文件上传的类,使用post发送
class UploadHandler(BaseHandler):
def get(self):
# a3_url = self.reverse_url("a3_url")
# self.write('this is a3' % a3_url)
self.write("get code:
")
def post(self):
files = self.request.files["fff"]
if files:
pyf = files[0]["body"]
# pyfname = files[0]["filename"]
# self.write(pyfname)
with open("/tmp/new_code.py", 'wb+') as up:
up.write(pyf)
self.write("OK")
else:
self.write("Error!")
# 发送代码字符串的类(带隐式格式)
class SendHandler(BaseHandler):
# 用URL的get的方式发送
def get(self):
code = self.get_argument("code")
if code:
with open("/tmp/new_code.py", 'w+') as up:
up.write(code)
else:
self.write("参数为空!")
# 用form表单或者ajax等的post加密方式发送
def post(self):
# URL编码
code = urllib.parse.quote(self.request.arguments["code"][0].decode("UTF-8"))
if code:
# name = str(random.randint(0,1000))+".py"
with open("/tmp/new_code.py", 'w+') as up:
# URL解码
up.write(urllib.parse.unquote(code))
# 回显到控制台
with open("/tmp/new_code.py", 'r') as r:
rs = r.readlines();
for x in rs:
print(x, end="")
else:
self.write("参数为空!")
# 存放线程对象
class G:
p = None
##
# 执行代码文件的类
class SsHandler(BaseHandler):
def get(self, cmd):
'''
cmd: 接收前端的正则表达式字符串
'''
print("Command:%s" % cmd)
if cmd == 'start':无锡妇科医院排行 http://www.0510bhyy.com/
# 将文件名修改
G.p = subprocess.Popen(["python","/tmp/new_code.py"], shell=False, stdout=subprocess.PIPE, stderr = subprocess.PIPE)
# this is a test server used to see whether it is running or not
# print("tornado l_server running at http://localhost:8001/a1")
print(type(G.p))
print(G.p)
# 会阻塞
# returncode = G.p.wait()
# print(returncode)
elif cmd == 'stop':
if G.p is not None:
poll = G.p.poll() #获取子进程的状态
if poll==0:
# print(G.p.stdout.read())
# print(G.p.stderr.read())
print("程序执行完毕!不需要手动结束!")
elif poll is None:
print("程序正在执行!马上退出...")
# 获取子进程的pid
pid = G.p.pid
print("pid: {}".format(pid))
# 杀死进程
G.p.kill()
print("killed.")
else:
print("状态码:{}".format(poll))
print("程序异常退出!")
else:
print("p is nothing")
else:
print("Command is not right!")
print("Done!")
# 没用
def post(self):
self.write("post-StartHandler")
# 定义路由设置
def make_app():
return tornado.web.Application([
(r"/", MainHandler),
# (r"/get", ItcastHandler, {"subject":"sub"}),
(r"/upload", UploadHandler),
(r"/send", SendHandler),
(r"/run/(\w*)", SsHandler),
(r"/(apple-touch-icon\.png)", tornado.web.StaticFileHandler,
dict(path=settings['static_path'])),
], **settings)
##
# main函数入口
if __name__ == "__main__":
tornado.options.parse_command_line()
app = make_app()
app.listen(options.port)
print("tornado server running at http://localhost:8000")
tornado.ioloop.IOLoop.current().start()
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
本文题目:Tornado和subprocess实现和外部程序通信-创新互联
URL地址:http://ybzwz.com/article/dcjhio.html