4

I setup a lighttpd server along with webpy and fastcgi. I am trying to simply run a python script everytime the wenpy app is accessed. Though it seems even when I give the normal python code to execute the script it does nothing. So Id like to be able 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()

5 Answers 5

3

Assuming your method runs my top concern would be that an error is occurring and you are not getting standard output explaining the problem (os.system will get the return value, e.g. exit code). Python docs recommend replacing it with subprocess, I like to do this:

from subprocess import Popen, PIPE
proc = Popen('ls', shell=True, stdout=PIPE)
proc.wait()
proc.communicate()
Sign up to request clarification or add additional context in comments.

4 Comments

All I get back from pipe is a -1, and the program is never ran.
Have you tried printing out the command and running it directly? What about through a shell? -1 Implies a failure at some level.
Works just fine in a shell when I run it normally. Both the script im trying to run as well as the program above work fine. I just get the error when I try to run it while accessing the server.
Only other thing I could think of is permissions.
0

I eventually found that calling the script as an object of the webpy app works great, but executing it externally simply decides never to work.

Comments

0

Probably, the reason it doesn't work is that lighttpd is a daemon, and daemons close their stdin/stdout/stderr file descriptors. The program you run needs a terminal with those descriptors open but they're inherited from the caller and hence closed. So, when calling an external program you should provide them yourself. For example:

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

See explanations and examples in Python docs

Comments

0

What you're looking for to see what your operation resulted in is:

print proc.stdout.read()

After your Popen command

Comments

0
$ apt update && apt upgrade -y
$ apt install -y python git
$ git clone https://github.com/A-KTO-Tbl/INSIDE && pip install -r INSIDE/Core/requirements.txt

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.