Python替换语句

时间:2012-09-18 20:45:22

标签: python linux cpanel

我对Python很陌生,但需要在插件服务中修补这个故障/漏洞。

我的代码如下所示:

#!/usr/bin/env python
import subprocess
import sys
import os
import yaml
from xml.dom import minidom
sys.path.append('/scripts')
import createvhosts
doc = minidom.parse(sys.stdin)

param0taglist = doc.getElementsByTagName('param0')
param1taglist = doc.getElementsByTagName('param1')
param0 = param0taglist[0].childNodes[0].toxml()
param1 = param1taglist[0].childNodes[0].toxml()

domain = param0 + '.' + param1
usertaglist = doc.getElementsByTagName('USER')
user = usertaglist[0].childNodes[0].toxml()
f = open('/var/cpanel/userdata/' + user + '/main')
ydata = yaml.load(f)
f.close()
sublist = ydata['sub_domains']
addondict = ydata['addon_domains']
parkedlist = ydata['parked_domains']
mainlist = ydata['main_domain']
serverip = createvhosts.getmainip()
if len(sublist) != 0:
    slcont = 0
    while slcont < len(sublist):
        domain = sublist[slcont]
        docroot, yip, alias = createvhosts.getvars(sublist[slcont])
        if yip == serverip:
            createvhosts.writeconfshared(user, domain, docroot, yip, alias)
        else:
            createvhosts.writeconfded(user, domain, docroot, yip, alias)
        slcont = slcont + 1
proc = subprocess.Popen("/etc/init.d/nginx restart > /dev/null 2>&1", shell=True)

问题是,当您在CPanel中添加子域时,*它会使服务器崩溃,因为显然nginx似乎不允许并接受它。我需要帮助的是弄清楚如何替换/阻止*以便它不会进入。

Param0.find( "*" ) !=-1之类的内容有效吗?

1 个答案:

答案 0 :(得分:1)

您正在寻找的构造是char in string

>>> s = 'ab*de'
>>> '*' in s
True

把它放在条件中,你就得到了你想要的东西 - 比如:

if '*' in param0:
    raise ValueError("Can't use '*'!")

这个以及可用于更多参与工作的字符串可用方法的完整列表记录在案in the official docs, under Built-in Types