【问题标题】:Syntax -- Popen arguments语法 -- Popen 参数
【发布时间】:2016-10-19 21:54:15
【问题描述】:

我有一个 PHP 脚本需要一个命令行参数。我需要从我的 python 脚本中调用这个脚本。

    Popen('php simplepush.php "Here's the argument"', shell=True, cwd="/home/ubuntu/web/firestopapp.com/app")  

^这行得通。但是,我想在 Python 脚本中传递一个变量,而不是“这是参数”。但是当我尝试时:

    var1 = "yes"

    Popen(['php', 'simplepush.php', var1], shell=True, cwd="/home/ubuntu/web/firestopapp.com/app")

它不再起作用了。这是通过 crontab 运行的,这就是导致我必须包含 cwd 参数的原因。

非常感谢任何帮助,这似乎是一个相当简单的句法问题。

根据 Eric 的建议:

Traceback (most recent call last): File "/home/ubuntu/web/mywebsite.com/app/email_parse.py", line 25, in <module> Popen('php simplepush.php "Here's the argument"', shell=False, cwd="/home/ubuntu/web/mywebsite.com/app") File "/usr/lib/python2.7/subprocess.py", line 711, in __init__ errread, errwrite) File "/usr/lib/python2.7/subprocess.py", line 1308, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory

Sihrc 的解决方案让我得到以下信息,因此它不是一个完整的解决方案。 /bin/sh: 1: cannot open my2ndemail@gmail.com: No such file

这是其余的代码。

#!/usr/bin/python
import email, getpass, imaplib, os, subprocess
from subprocess import Popen

detach_dir = '.'
m = imaplib.IMAP4_SSL("imap.gmail.com")
m.login("myemail@gmail.com","mypassword")
m.select('mailbox')

resp, items = m.search(None, "(UNSEEN)")
message = ""
items = items[0].split()
for emailid in items:
    resp, data = m.fetch(emailid, "(RFC822)")
    email_body = data[0][1]
    mail = email.message_from_string(email_body)

    message += "["+mail["From"]+"] :" + mail["Subject"] + "\n"
    for part in mail.walk():
        if part.get_content_type() == 'text/plain':
            message += part.get_payload()
        else:
            continue
    Popen('php simplepush.php ' + str(eval('message')), shell=True, cwd="/home/ubuntu/web/firestopapp.com/app")

【问题讨论】:

  • @EricUrban Tracebck 来自更新问题中 cron 的 MTA 响应。
  • @user2476581 请用回溯更新您的问题,作为评论阅读太难了
  • @MattDMo 已编辑,抱歉给您带来了困惑!
  • 为什么不 Popen('php simplepush.php ' + str(eval('var1')), shell = ....)
  • @sihrc 我相信已经解决了。感谢您的帮助!

标签: python subprocess crontab popen


【解决方案1】:

绕道!回避问题是我的专长!

var1 = "yes"
Popen('php simplepush.php ' + '"' + str(eval('var1')) + '"', shell=True, cwd="/home/ubuntu/web/firestopapp.com/app") 

【讨论】:

  • 所以实际上这在我定义 var1="yes" 时有效,但当我有一些看起来像我编辑到问题中的其余代码时它会中断。
  • 是的,我将其编辑为问题:/bin/sh: 1: cannot open my2ndemail@gmail.com: No such filemy2ndemail@gmail.com 是我在上面执行的消息 += 操作的一部分。如果我在 Popen 之前简单地 print message,整个字符串打印正确。
  • 对不起,我粘贴到了不同的版本。使用带引号的 eval 仍然给我上面给出的错误,它试图打开字符串而不是使用它作为参数。
  • 此时,我只建议将消息​​写入外部文件并在 php 中读取。
【解决方案2】:
#!/usr/bin/python
import email, getpass, imaplib, os, subprocess
import shlex
from subprocess import Popen

detach_dir = '.'
m = imaplib.IMAP4_SSL("imap.gmail.com")
m.login("myemail@gmail.com","mypassword")
m.select('mailbox')

resp, items = m.search(None, "(UNSEEN)")
message = ""
items = items[0].split()
for emailid in items:
    resp, data = m.fetch(emailid, "(RFC822)")
    email_body = data[0][1]
    mail = email.message_from_string(email_body)

    message += "["+mail["From"]+"] :" + mail["Subject"] + "\n"
    for part in mail.walk():
        if part.get_content_type() == 'text/plain':
            message += part.get_payload()
        else:
            continue
    for pdir in os.getenv('PATH'):
        if os.access("{}/{}".format(pdir, 'php'), os.X_OK):
            phppath = "{}/{}".format(pdir, 'php')
            break
    cmd = '{} simplepush.php "{}"'.format(phppath, message)
    Popen(shlex.split(cmd),
          cwd="/home/ubuntu/web/firestopapp.com/app")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    • 1970-01-01
    • 2018-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多