我有一個需要使用一個命令行參數的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")
嘗試從更新的問題的cron的MTA響應'殼= FALSE' –
@EricUrban Tracebck。 – user2476581
@ user2476581請使用回溯更新您的問題,這太難以閱讀 – MattDMo