简体   繁体   English

文件名的Python子进程传递参数

[英]Python Subprocess Pass Argument of Filename

I know there is a better way to do this than subprocess.Popen but for the sake of time, thats what I am using. 我知道有一个比subprocess.Popen更好的方法,但是为了节省时间,这就是我正在使用的方法。 I have a script called save_input.cgi which saves a file to my linux box through a web app. 我有一个名为save_input.cgi的脚本,该脚本通过Web应用程序将文件保存到我的linux盒中。 When it has saved the file, I need it to pass that filename as an argument to another script. 保存文件后,我需要它将该文件名作为参数传递给另一个脚本。 This is probably something simple I am missing, but here is what I have so far: 这可能是我想念的简单事情,但是到目前为止,这是我所拥有的:

#!/usr/bin/python
import cgi, os
import cgitb; cgitb.enable()

form = cgi.FieldStorage()

# Get filename here.
fileitem = form['filename']

# Test if the file was uploaded
if fileitem.filename:
  # strip leading path from file name to avoid 
  # directory traversal attacks
  fn = os.path.basename(fileitem.filename.replace("\\", "/" ))
  open('/tmp/' + fn, 'wb').write(fileitem.file.read())

  message = 'The file "' + fn + '" was uploaded successfully'
  subprocess.Popen(["python", "mod_check.py", '/tmp/' + fn])      

else:
   message = 'No file was uploaded'

print """\
Content-Type: text/html\n
<html>
<body>
   <p>%s</p>
</body>
</html>
""" % (message,)

Thoughts? 有什么想法吗? Thanks a lot, I owe this community a lot haha. 非常感谢,我欠这个社区很多哈哈。

*I just update it to include the subprocess in the if fileitem.filename block, but it does not seem to pass the filename and location to the script as an argument. *我只是对其进行了更新,以在if fileitem.filename块中包括该子进程,但似乎没有将文件名和位置作为参数传递给脚本。 To answer another one of the comments, yes this script does work if I manually enter "sudo python mod_check.py filename.txt" 要回答另一条评论,是的,如果我手动输入“ sudo python mod_check.py filename.txt”,此脚本确实可以工作

Just a simple thing I overlooked. 只是我忽略的一件简单的事情。 Resolved using jordanm's response. 通过约旦的回应解决。 Added to the codeblock 'if fileitem.filename' 添加到代码块“ if fileitem.filename”

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

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