我想要做一个subprocess.call
创建一个文件(并最终填充它)。这里是我的代码:有人可以解释两个Python命令的区别吗?
#!/usr/bin/python
import sys
import subprocess
import psycopg2
import base64
import urlparse
sys.path.append('/var/www/cgi-bin')
def index(req):
out = ""
mkFile = str("touch /etc/httpd/conf/ipForward/90.conf")
subprocess.call([mkFile],shell=True)
return out
mkFile = str("touch /etc/httpd/conf/ipForward/111.conf")
subprocess.call([mkFile],shell=True)
眼下只有底部命令的工作,但这两个子进程应该做同样的事情(我命名为不同的测试目的文件)。我知道有一个更好的方法来做到这一点,但是,为了我自己的教诲(和理智),有人可以解释这种差异吗?
~~~~~~~~~~~~~ 〜
def index(req):
out = ""
conn = psycopg2.connect("dbname='pwp' host='localhost' user='~~~' password='~~~~~'")
c = conn.cursor()
inClCN = str(req.subprocess_env['SSL_CLIENT_S_DN_CN'])
c.execute("select * from Users where cn = '{0}';".format(inClCN))
rows = c.fetchall()
for row in rows:
port = row[0]
mkFile = str("touch /etc/httpd/conf/ipForward/{0}.conf".format(port))
subprocess.call([mkFile],shell=True)
insert = r"""Listen {0}\n\n<VirtualHost _default_:{1}>\n\nDocumentRoot '/var/www/html/'\nDirectoryIndex indexAlex.py\n\nRewriteEngine On\n\nErrorLog /etc/httpd/logs/error_log\nTransferLog /etc/httpd/logs/access_log\nLogLevel warn\n\n\nNSSVerifyClient require\n\nNSSEngine on\nNSSFIPS on\nNSSProtocol(MORE CONF STRING)</VirtualHost>""".format(port,port)
confFile = str('echo "{0}" >> /etc/httpd/conf/ipForward/{1}.conf'.format(insert,port))
subprocess.call([confFile],shell=True)
return out
我需要req的唯一原因是从apache获取环境变量。我真的不知道这是如何工作的,但我一直在其他没有子流程的代码中使用它。
你有没有真的在任何地方调用函数'index'? –
如果你没有在任何地方调用函数'index',你为什么期望该函数的内容被调用? –
我怎么称呼它。我需要什么参数来请求? – alexs973