Tornado和subprocess实现和外部程序通信

主要涉及subprocess的使用,简单点,直接上代码吧,只是做一下记录。

创新互联是一家网站设计公司,集创意、互联网应用、软件技术为一体的创意网站建设服务商,主营产品:响应式网站设计品牌网站建设营销型网站建设。我们专注企业品牌在网站中的整体树立,网络互动的体验,以及在手机等移动端的优质呈现。成都网站制作、成都网站设计、移动互联产品、网络运营、VI设计、云产品.运维为核心业务。为用户提供一站式解决方案,我们深知市场的竞争激烈,认真对待每位客户,为客户提供赏析悦目的作品,网站的价值服务。

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()


网站题目:Tornado和subprocess实现和外部程序通信
文章源于:http://myzitong.com/article/pposgc.html