0

I am trying to run a python script within my flask web app. Basically the user can click on a button and the script should run on the server that is hosting my flask python web app. I am hosting it on Windows Server 2016 and IIS (i know, I should consider linux)

enter image description here

When the user clicks on the button, the server executes the following script using the os.system function which is configured as below.

def run_python_script(script_path_and_arguments):    
    try:
        command = 'python {}'.format(script_path_and_arguments)
        system(command)
        return True, 'Success'
    except Exception as e:
        logger.error('Failed to run script, exception: {}'.format(e))
        return False, 'Unknown Error'

enter image description here

When I run the script locally on my workstation it executes ok. When I run the script from the server, from the command line, it runs ok, When I try to run it through the website, by clicking the button it fails.

It seems to me that it must be some permision/security issue when it runs under the IIS. Would you have any idea where I should be looking at to resolve it?

on the server the python.exe is in c:\Anaconda3\

Edit: I have now tried to capture the output by running the script with the subprocess module: it gives a plain exception "Command 'python ...' returned non-zero exit status 1"

def run_python_script(tscript_path_and_arguments):
    try:
        command = 'python {}'.format(script_path_and_arguments) 
        output_bytes = subprocess.check_output(command, stderr=subprocess.STDOUT)
        output = output_bytes.decode('utf-8')
        lines = output.split('\n')
        for line in lines:
            logger.info(msg='Script output =  {}'.format(line))
        return True, 'Success'
    except Exception as e:
        logger.error('Failed to run script, exception: {}'.format(e))
        return False, 'Unknown Error'
4
  • Why are you calling python using the command line using python? Why not just import the modules or scripts you need into your flask app and call them from there? It also seems safer then just letting users run python in the command line. Commented Dec 5, 2018 at 11:21
  • @Joost To be honest, it's because the scripts are part of another project, i could of course have done that. At the time of implementation it seemed the right thing to do. Commented Dec 5, 2018 at 11:54
  • After some testing and stripping of code (because a plain hello world script runs ok) it turns out that the problem is in the imports of the script being executed. I have some modules that the script is importing but those are in the PYTHONPATH. Is the PYTHONPATH not being considered when i run the script as demonstrated above? When i strip the script of those imports it runs ok. Commented Dec 5, 2018 at 11:57
  • 1
    BTW, based on recent changes from Microsoft side, in the near future, it would be a must to move to Linux, blog.lextudio.com/… than merely "I should consider Linux". Commented Dec 5, 2018 at 18:48

1 Answer 1

1

it turns out that the flask app is not using the pythonpath system environment variable but the one that is configured in fastcgi settings: enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.