简体   繁体   中英

Subprocess not run in python cgi script

I have issue while running subprocess in Python CGI script.

I am going to run python file as subprocess in python CGI script.

script.py

#!enable debugging
import cgitb
cgitb.enable()

print("Content-Type: text/html;charset=utf-8")
print()

import subprocess
p = subprocess.Popen(["sudo", "/usr/bin/python3", "test.py"], stdout=subprocess.PIPE)

test.py

f.open("test.txt", "a")
f.write("This is test")
f.close()

If I run script.py in console, it creates test.txt file successfully. But If I run it on browser with Python CGI, it cannot create test.txt .

I thought, it can be caused by permission, so I tried to create test.txt in script.py directly, not on 'test.py', it is created successfully.

So, main issue is Python CGI script cannot run subprocess.

I cannot get any error while running on browser as Python CGI script.

How can I fix this issue?

Please if sudo does not work, look in the system log. There can be messages that help you with the debugging. The files are in /var/log and if you list by time ls -t you will see which ones changed just now.

First try without sudo. Make your file in a place where it does not need sudo permission like /tmp/test.txt . Then you know if the problem is sudo or something else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM