0

我有一个 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")
4

2 回答 2

0

車輛改道!回避问题是我的专长!

var1 = "yes"
Popen('php simplepush.php ' + '"' + str(eval('var1')) + '"', shell=True, cwd="/home/ubuntu/web/firestopapp.com/app") 
于 2013-08-04T01:35:34.383 回答
0
#!/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")
于 2016-10-19T13:54:15.827 回答