linux 下python flask,python control linux (flask)

I was trying to build a web-based utility for Linux Management and I found those are not familiar to me with which Node.js, ShellJS, ... etc. Luckily I found another way to control the underlying Linux system in python, simply put, the flask web framework plus the python subprocess module would work.

Simple demo code

import subprocess

from flask import Flask

app = Flask(__name__)

def run_command(command):

return subprocess.Popen(command, shell=True, stdout=subprocess.PIPE).stdout.read()

@app.route('/')

def command_server(command):

return run_command(command)

$ export FLASK_APP=server.py

$ flask run

0894b2b31965

image.png

The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module intends to replace several older modules and functions

os.system

os.spawn*

Difference between Popen and os.system call

os.system is equivalent to Unix system command, while subprocess was a helper module created to provide many of the facilities provided by the Popen commands with an easier and controllable interface. Those were designed similar to the Unix Popen command.

system() executes a command specified in command by calling /bin/sh -c command, and returns after the command has been completed

The popen() function opens a process by creating a pipe, forking, and invoking the shell.

subprocess.popen vs subprocess.run

subprocess.run was added in Python 3.5 as a simplification over subprocess.Popen when you just want to execute a command and wait until it finishes, but you don't want to do anything else meanwhile. For other cases, you still need to use subprocess.Popen.

The main difference is that subprocess.run executes a command and waits for it to finish, while with subprocess.Popen you can continue doing your stuff while the process finishes and then just repeatedly call subprocess.communicate yourself to pass and receive data to your process.

Note that, what subprocess.run is actually doing is invoking for you the Popen and communicate, so you don't need to make a loop to pass/receive data nor wait for the process to finish.

Check the official documentation for information of which parameters of subprocess.run are passed to Popen and which to communicate.

Complete code demo from run shell command with flask gist

from flask import Flask

from flask import request

import subprocess

app = Flask('flaskshell')

ip_whitelist = ['192.168.1.2', '192.168.1.3']

query_success = "SELECT COUNT(*) FROM flasktest.tasks WHERE task_status='Success'"

query_pending = "SELECT COUNT(*) FROM flasktest.tasks WHERE task_status='Pending'"

query_failed = "SELECT COUNT(*) FROM flasktest.tasks WHERE task_status='Failed'"

def valid_ip():

client = request.remote_addr

if client in ip_whitelist:

return True

else:

return False

@app.route('/status/')

def get_status():

if valid_ip():

command_success = "mysql -uflaskuser -pflask123 -e '{0}'".format(

query_success)

command_pending = "mysql -uflaskuser -pflask123 -e '{0}'".format(

query_pending)

command_failed = "mysql -uflaskuser -pflask123 -e '{0}'".format(

query_failed)

try:

result_success = subprocess.check_output(

[command_success], shell=True)

result_pending = subprocess.check_output(

[command_pending], shell=True)

result_failed = subprocess.check_output(

[command_failed], shell=True)

except subprocess.CalledProcessError as e:

return "An error occurred while trying to fetch task status updates."

return 'Success %s, Pending %s, Failed %s' % (result_success, result_pending, result_failed)

else:

return """

404 Not Found

Not Found

The requested URL was not found on the server.

If you entered the URL manually please check your

spelling and try again.

""", 404

if __name__ == '__main__':

app.run()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值