Web in python that interacts with linux
<p>Is there a way to make web in python (mixed with <code>html/php</code>) that interacts with PC (<code>linux</code> OS) ?</p>

<p>As example: you click button in webpage and It sends command to PC (<code>linux</code>) that activates another <code>python</code> script, but not in web format (example: I push button 'RUN PYTHON.PY SCRIPT' and it sends command to PC to activate it)</p>

<p><code>web server</code> will be on the same computer, <code>localhost</code></p>

<p>Yes, it is possible.  Here is one way to do it.</p>

<pre><code>#!/usr/bin/env python

import os

from flask import Flask
app = Flask(__name__)

@app.route('/')
def root():
    return '''
        &lt;html&gt;&lt;body&gt;&lt;form action="/run" method="post"&gt;
            &lt;input type="submit" value="Run Python Script!"/&gt;
        &lt;/form&gt;&lt;/body&gt;&lt;/html&gt;'''

@app.route('/run', methods=['POST'])
def run():
    os.system("python /tmp/script.py &gt; /tmp/out 2&gt;&amp;1")
    return '''&lt;html&gt;&lt;body&gt;&lt;p&gt;The script has run.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;'''

if __name__=='__main__':
    app.run(debug=True)
</code></pre>

<p>Here is an example which passes information from the form to the python script:</p>

<pre><code>#!/usr/bin/env python

import os

from flask import Flask, request
app = Flask(__name__)

@app.route('/')
def root():
    return '''
        &lt;html&gt;&lt;body&gt;&lt;form action="/run" method="post"&gt;
            Arg1: &lt;input name="Arg1"/&gt;&lt;br/&gt;
            Arg2: &lt;input name="Arg2"/&gt;&lt;br/&gt;
            &lt;input type="submit" value="Run Python Script!"/&gt;
        &lt;/form&gt;&lt;/body&gt;&lt;/html&gt;'''

@app.route('/run', methods=['POST'])
def run():
    # DANGER! DANGER! Example code only. Code injection bug in next line
    cmd = "python /tmp/script.py {} {} &gt; /tmp/out 2&gt;&amp;1".format(
        request.form.get('Arg1'),
        request.form.get('Arg2'),
    )
    os.system(cmd)
    return '''&lt;html&gt;&lt;body&gt;&lt;p&gt;The script has run.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;'''

if __name__=='__main__':
    app.run(debug=True)
</code></pre>

<p>In each of these examples, you'll want to consider the security requirements of your own application. I have highlighted one security vulnerability in the second example; there may be others.</p>

php python linux