2013-08-04 39 views
0

我有一個需要使用一個命令行參數的PHP腳本。我需要從我的python腳本中調用這個腳本。語法 - Popen參數

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

^That works。但是,我想在Python腳本中傳遞一個變量,而不是「這是參數」。但是,當我嘗試:

var1 = "yes" 

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

它不再起作用。這是通過crontab運行的,這導致我必須包含cwd參數。

我真的很感謝任何幫助,看起來像一個相當直接的語法問題。

埃裏克的建議後:

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 [email protected]: 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("[email protected]","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") 
+1

嘗試從更新的問題的cron的MTA響應'殼= FALSE' –

+0

@EricUrban Tracebck。 – user2476581

+0

@ user2476581請使用回溯更新您的問題,這太難以閱讀 – MattDMo

回答

0

繞道而行!避免這個問題是我的專長!

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

所以實際上,當我定義var1 =「yes」時它有效,但當我看到類似於編輯到問題中的代碼的其餘部分時,它會中斷。 – user2476581

+0

你能告訴我輸出的信息是 – sihrc

+0

是的,我把它編輯成問題: '/ bin/sh:1:無法打開[email protected]:沒有這樣的文件' [email protected]是消息+ =我正在執行的操作。如果我在Popen之前只是「打印信息」,整個字符串打印就會正確。 – user2476581

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("[email protected]","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")