- 论坛徽章:
- 0
|
本帖最后由 none311 于 2013-01-07 16:39 编辑
python新人,照着书上敲了一段代码,替换apache的配置 但是给了3个参数:./replace.py httpd.conf "0.0.0.0:80" "/test"
报错:
Traceback (most recent call last):
File "./replace.py", line 31, in ?
for line in replace_docroot(conf_string,vhost,docroot):
TypeError: iteration over non-sequence
求帮助- from cStringIO import StringIO
- import re
- vhost_start=re.compile(r'<VirtualHost\s+(.*?)>')
- vhost_end=re.compile(r'</VirtualHost')
- docroot_re=re.compile(r'(DocumentRoot\s+)(\S+)')
- def replace_docroot(conf_string,vhost,new_docroot):
- conf_file=StringIO(conf_string)
- in_vhost=False
- curr_vhost=None
- for line in conf_file:
- vhost_start_match=vhost_start.search(line)
- if vhost_start_match:
- curr_vhost=vhost_start_match.groups()[0]
- in_vhost=True
- if in_vhost and (curr_vhost == vhost):
- docroot_match=docroot_re.search(line)
- if docroot_match:
- sub_line=docroot_re.sub(r'\1%s' % new_docroot,line)
- line=sub_line
- vhost_end_match=vhost_end.search(line)
- if vhost_end_match:
- in_vhost=False
- if __name__=='__main__':
- import sys
- conf_file=sys.argv[1]
- vhost=sys.argv[2]
- docroot=sys.argv[3]
- conf_string=open(conf_file).read()
- for line in replace_docroot(conf_string,vhost,docroot):
- print line
复制代码 |
|