简体   繁体   English

为什么subprocess.Popen阻塞?

[英]Why is subprocess.Popen blocking?

I have an python cgi script that handles login, this is because my website is three (school) websites combined and before my website can be used the data needs to be extracted from those websites. 我有一个处理登录的python cgi脚本,这是因为我的网站是三个(学校)网站的组合,在我的网站可以使用之前,需要从这些网站中提取数据。 This extraction takes 2 minutes so I want to make a fancy (semi-fake) loading screen. 这个提取需要2分钟,所以我想制作一个花哨的(半假的)加载屏幕。

My register code ends with: 我的注册码以:

import subprocess
token = "".join(random.choice(
                    string.ascii_lowercase + string.digits + string.ascii_uppercase)
                        for _ in range(5)) #generate 5 random characters
#run initScript
subprocess.Popen("python {}/python/initUser.py {} {}".format(
    os.getcwd(), uid,token), shell=True, stdin=None, stdout=None, stderr=None,
    close_fds=True)

print "Content-type: text/html"
print "Location: registerLoading.php?token={}".format(token)
print
sys.exit(0)

With the subprocess line stolen from: Run Process and Don't Wait 进程线被盗: 运行进程,不要等待

But the subprocess line is still blocking and I can't figure out why. 但是子流程线仍在阻塞,我无法弄清楚原因。

I'm developing on ubuntu 16.04, and it's going to run on an raspbarry pi 3 (that explains the loading time) 我正在开发ubuntu 16.04,它将在raspbarry pi 3上运行(这解释了加载时间)

close_fds has no effect on stdout. close_fds对stdout没有影响。 You want devnull file handles ( subprocess.DEVNULL in Python 3.3+), so that that the stdout of this script is closed with the call to exit : 你想devnull文件句柄( subprocess.DEVNULL在Python 3.3+),使这个脚本的标准输出与调用关闭exit

subprocess.Popen(
   ["python", "python/initUser.py", uid, token],
   stdin=None, stdout=open(os.devnull, 'wb'), stderr=open(os.devnull, 'wb'))

Note that I also replaced the shell command with a list form. 请注意,我还用列表表单替换了shell命令。 This makes the code safe against command injection - previously, every user could run arbitrary shell commands on your webserver. 这使得代码可以安全地防止命令注入 - 以前,每个用户都可以在您的Web服务器上运行任意shell命令。

In addition, you may also want to beef up the security of the token. 此外,您可能还想加强令牌的安全性。 5 characters can be brute-forced, but much more importantly, random.choice is not cryptographically secure. 5个字符可以强制使用,但更重要的是, random.choice不具有加密安全性。 Use random.SystemRandom().choice instead, or the much more modern secrets.token_urlsafe in Python 3.6+. 使用random.SystemRandom().choice ,或Python 3.6+中更现代的secrets.token_urlsafe

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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