[PYTHON] Execute the command on the web server and display the result

It's delicate in terms of security, but it's troublesome to SSH just to type one command, so I often put it in the server secretly. The sample is ls, but even make works.

When \ x0a appears in the middle, the point is to replace it with<BR>to make the output easier to see.

#!/usr/bin/env python

import sys
from subprocess import Popen, PIPE

print "Content-Type: text/html\n\n"

p = Popen(["ls","-al"], stdout=PIPE)
while 1:
  c = p.stdout.read(1)
  if not c:
    break
  if c == bytes("\x0a"):
    print "<br>"
  sys.stdout.write(c)

Recommended Posts

Execute the command on the web server and display the result
Publish the current directory on the web server
Quickly display the QR code on the command line
Execute raw SQL using python data source with redash and display the result
Install django on python + anaconda and start the server
[Python3] Take a screenshot of a web page on the server and crop it further
Test.py is not reflected on the web server in Python3.
Display "Hello World" created in the local environment on the web
Let's execute the command on time with the bot of discord
Image Optimize on the server side using TinyPNG's Web API
[Python] Save the result of web scraping the Mercari product page on Google Colab to Google Sheets and display the product image as well.
Display the absolute path on the Finder
Fastest and strongest web server architecture
Notes on using matplotlib on the server
I tried to push the Sphinx document to BitBucket and it will be automatically reflected on the web server
Install Chrome on the command line on Sakura VPS (Ubuntu) and launch Chrome with python from virtual display and selenium
Procedure until you can create a general user and execute the sudo command on CentOs (memorial note)
I want to pass an argument to a python function and execute it from PHP on a web server
Connect to VPN with your smartphone and turn off / on the server
Get, test, and submit test cases on the command line in the AtCoder contest
Extract the TOP command result with USER and output it as CSV
[Personal memo] Get data on the Web and make it a DataFrame
[Android] Display images on the web in the info Window of Google Map
Deploy and use the prediction model created in Python on SQL Server
Summarize the titles of Hottentori at the end and look at the present on the Web
Install Python3 and Django on Amazon Linux (EC2) and run your web server
Keep getting RSS on the command line
Insufficient var space on the mail server
Install and Configure TigerVNC server on Linux
Display the graph of tensorBoard on jupyter
Build a web server on your Chromebook
Execute the COPY command using python's Psycopg
Add lines and text on the image
You search commandlinefu on the command line
Web server construction with Apache 2.4 (httpd 2.4.43) + PHP 7.4 on Linux ―― 4. Security (chown and firewalld)
Source compile Apache2.4 (httpd 2.4.43) + PHP7.4 on Linux and build a Web server ―― 1. Apache introduction
Source compile Apache2.4 (httpd 2.4.43) + PHP7.4 on Linux and build a Web server --2 PHP introduction
[Writing] I want to display the variables used in the program on the server side in real time on the browser and update them.
Install matplotlib and display graph on Jupyter Notebook
Run the task in the background on the sshed server
Execution environment on the Web by "Project Jupyter"
Drawing tips with matplotlib on the server side
Launch and use IPython notebook on the network
Frequently used (personally) notes on the tar command
Regularly monitor the HTTP response of the web server
Effective and simple Web server security measures "Linux"
Let's simulate the Izhikevich neuron model on the web!
Arduino development on the command line: vim + platformio
Load and execute command from yml in python
DISPLAY error in wxPython (and traitsui) on vscode
Image display and video playback on Neovim Terminal
Syntax highlighting on the command line using Pygments
Launch a web server with Python and Flask
Have pandas read the zip file on the web
Convert XLSX to CSV on the command line
Operate Route53 on the command line using AWS-CLI.
Remotely open Jupyter notebook launched on the server
Think about the selective interface on the command line
[CGI] Run the Python program on the server with Vue.js + axios and get the output data
It is easy to execute SQL with Python and output the result in Excel