1

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>";
Peter G.
  • 7,816
  • 20
  • 80
  • 154
  • I don't think it's a good idea to call some scripts from a web server. The loading time could increase so no one will wait for the content to load. – ForceBru Oct 06 '15 at 17:54
  • The script is called after the page is loaded. It is in template stage, the sole purpose will be to process input from a form. – Peter G. Oct 06 '15 at 18:01

1 Answers1

1

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()
Radagast
  • 509
  • 10
  • 23
  • Works fine when I run it as standalone script, but not inside the app method in WSGIServer. – Peter G. Oct 06 '15 at 18:14
  • this can work: create a text file in script directory and: script1.py: f = open("./a.txt", "w") f.write(variable) /////// appmethod.py: f = open("./a.txt", "r") var = f.read() – Radagast Oct 06 '15 at 18:26
  • In the end it worked when running the code example, but I accept your both solutions. – Peter G. Oct 06 '15 at 18:32