pythonapachefastcgiwsgifcgid

Run Python script from another and pass the variables and output


When looking for a tutorial on passing variables and output between Python scripts I couldn't find any example that would work with the WSGI Server in my example.

I want the output (and variables) returned in HTML instead of seeing it only in the console.

The best solution for calling python script from another I found is subprocess, but I still can't see the merged output of Script 1 and Script 2 of in my web browser and only in console.

Script 1:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

from cgi import escape
import sys, os
from flup.server.fcgi import WSGIServer
import subprocess

def app(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/html')])

    yield '<h1>Test - Python pass output and variables</h1>'
    yield '<p>Script 1</p>'
    yield subprocess.check_output(["python", "script2.py"])

WSGIServer(app).run()

Script 2:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

print "<p>Script 2</p>";

Solution

  • If you want to pass variables between scripts in python, do something like this:

    Script1.py:

    def passVars():
        variable = "bla"
        return variable
    

    Script2.py:

    import Script1 as sc1
    
    var = sc1.passVars()