Running python script from webpy

I am installing a lighttpd server along with webpy and fastcgi. I am trying to just run a python script every time a wenpy application is available. Although it seems that even when I give normal python code to execute the script, it does nothing. So Id would like to run this script, any idea would be helpful.

#!/usr/bin/env python

import web, os

urls = (
    '/(.*)', 'hello'
)
app = web.application(urls, globals())

class hello:
    def GET(self, name):
        os.system("python /srv/http/script/script.py")
        if not name:
            name = 'world'
        return "Running"

web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr)
if __name__ == "__main__":
    app.run()
+3
source share
4 answers

Assuming your method triggers my main concern, an error will occur and you will not get the standard output explaining the problem (os.system will get a return value, like an exit code). Python docs recommend replacing it with a subprocess, I like to do this:

from subprocess import Popen, PIPE
proc = Popen('ls', shell=True, stdout=PIPE)
proc.wait()
proc.communicate()
+3
source

, script - , .

0

Probably the reason it doesn't work is because lighttpd is a daemon, and daemons close the file descriptors stdin / stdout / stderr. To run the program, a terminal with open descriptors is required, but they are inherited from the caller and, therefore, are closed. Therefore, when calling an external program, you must provide them yourself. For instance:

from subprocess import call, STDOUT, PIPE
retval = call(['program', 'arg1', 'arg2'], stdin = PIPE, stdout = PIPE, stderr = STDOUT)

See explanations and examples in Python Docs

0
source

What are you looking to see what your operation has led to:

print proc.stdout.read()

After team Popen

0
source

Source: https://habr.com/ru/post/1734802/


All Articles