42121 发表于 2015-11-2 10:59:39

python 常用的模块 optparse与ConfigParser

一、optparse 模块
功能:optparse模块用于处理命令行参数
      使用流程:
    1、首先,必须 import OptionParser 类,创建一个 OptionParser 对象:

1
2
3
4
from optparse import OptionParser

def Opt ():
   parser=OptionParser("Usage: %prog -o option -d domain -s subject")




   2、使用 add_option 来定义命令行参数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
   parser.add_option("-d",
                      "--domain",
                      dest="domain",
                      default=False,
                      action="store_true",
                      help="special domain_name or domain_name/user",)
   parser.add_option("-s",
                      "--subject",
                      dest="subject",
                      default=False,
                      action="store_true",
                      help="special subject for record operational log",)
   parser.add_option("-o",
                      "--operation",
                      dest="operation",
                      default=False,
                      action="store_true",
                      help="special operation Del or Insert Key ",)
   options,args = parser.parse_args()
   return options,args
   #以上是定义参数与选项,返回对象与参数




    选项说明:
   每个命令行参数就是由参数名字符串和参数属性组成的。如 -d 或者 --domain 分别是长短参数名
   dest: 是可选的。如果没有指定 dest 参数,将用命令行的参数名来对 options 对象的值进行存取
   store:有两种形式:store_true和store_false,用于处理带命令行参数后面不带值的情况。如 -v,-q [-v 也可以认为查看版本-q表示退出时后面可以不带值]
   default:可以为参数指定默认值;如果-f表示文件,但没有指定值,那么在解析时会直接找到default定义的默认值。
   action是parse_args()方法的参数之一,它指示optparse当解析到一个命令行参数时该如何处理。actions 有一组固定的值可供选择,默认是'store',表示将命令行参数值保存在 options 对象里。
   help:定义帮助信息,是对参数的描述

   3、生产中使用的完整例子:
    这个脚本的功能其实就是对redis添加删除操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python
#coding:utf-8
#Date: 2015-09-09
#Author:king.gp
################描述#################
#
#查找是否设置此kye,有则跳过
#如果没有设置此kye,并将操作写入/var/log/redis_add_pause_domain_list.log中
#如果恢复指定的存在的key,也要写入到/var/log/redis_retrieve_pause_domain_list.log
#
####################################

import redis
import datetime
import os
import sys
from optparse import OptionParser

def Opt ():
   parser=OptionParser("Usage: %prog -o option -d domain -s subject")
   parser.add_option("-d",
                      "--domain",
                      dest="domain",
                      default=False,
                      action="store_true",
                      help="special domain_name or domain_name/user",)
   parser.add_option("-s",
                      "--subject",
                      dest="subject",
                      default=False,
                      action="store_true",
                      help="special subject for record operational log",)
   parser.add_option("-o",
                      "--operation",
                      dest="operation",
                      default=False,
                      action="store_true",
                      help="special operation Del or Insert Key ",)
   options,args = parser.parse_args()
   return options,args,parser
   #以上是定义参数与选项,返回对象与参数

def Connect_Redis(options,hostname,parser,*argv):
   r = redis.Redis(host='localhost',port=6379,db='0')
   if options.domain:
   key_string = hostname+'/Conf/MailBox/'+argv+'/Status_Pause_All'
   key_list=r.keys(key_string)
   if (options.operation andargv=='search'):
       if len(key_list) > 0 :
         print key_list+' Existing'
       else:
         print key_string+' Not Existing'
       sys.exit(0)   

   if (options.operation and options.subject and argv=='delete'):
      iflen(key_list)>0:
         r.delete(key_string)
         #print key_string
         print key_string+' alrealy Del!'
         with open('/var/log/redis_retrieve_pause_domain_list.log','a') as f:
             now = datetime.datetime.now()
             date_string = now.strftime("%Y-%m-%d %H:%M:%S")
             f.write(date_string+'\t'+argv+'\t'+key_string+'\n')
         sys.exit(0)
      else:
         print key_string+' Not Existing'
         sys.exit(0)
   elif (options.operation and options.subject and argv =='insert'):
          r.set(key_string,'1')
          print key_string+' alrealy Set!'
          with open('/var/log/redis_add_pause_domain_list.log','a') as f:
            now = datetime.datetime.now()
            date_string = now.strftime("%Y-%m-%d %H:%M:%S")
            f.write(date_string+'\t'+argv+'\t'+key_string+'\n')
          sys.exit(0)
   else:
      parser.error("domain or subject not empty")

   else:
      sys.exit(0)

def Get_Host_Name():
host = os.popen('echo $HOSTNAME')
hostname = host.read().strip()
if len(hostname) > 4:
    hostname = hostname.split('.')
return hostname

def main():
    options,args,parser=Opt()
    if args:
      keys=tuple(args)
      hostname=Get_Host_Name()
      Connect_Redis(options,hostname,parser,*keys)
    else :
      parser.error("Usage: %prog -o option -d domain -s subject")
   
if __name__ == '__main__':
    main()




4、查看帮助

1
2
3
4
5
6
7
8
# python change_redis_pause_key.py-h
Usage: change_redis_pause_key.py -o option -d domain -s subject

Options:
-h, --help       show this help message and exit
-d, --domain   special domain_name or domain_name/user
-s, --subject    special subject for record operational log
-o, --operationspecial operation Del or Insert Key




二、ConfigParser模块
1、功能:配置文件解析模块【我是这么理解的】
2、说明:配置文件要mysql的配置文件一样分区段;配置文件的格式:中括号“[ ]”内包含的为section。section 下面为类似于key-value 的配置内容。
      3、函数说明:

[*]

[*]读取配置文件时的函数


1
2
3
4
5
6
read(filename) 直接读取ini文件内容
sections() 得到所有的section,并以列表的形式返回
options(section) 得到该section的所有option
items(section) 得到该section的所有键值对
get(section,option) 得到section中option的值,返回为string类型
getint(section,option) 得到section中option的值,返回为int类型,还有相应的getboolean()和getfloat() 函数。





[*]

[*]写入文件时的函数


1
2
add_section(section) 添加一个新的section
set( section, option, value) 对section中的option进行设置,需要调用write将内容写入配置文件。





[*]

[*]保存文件



1
conf.write(open(network_path+network_card,'w'))




4、写个例子
   说明:迁移机房生成原IP与新IP的绑定到对应的配置文件中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
bash-4.1$ cat network.py
#!/usr/bin/env python
#conding:utf8

#
import sys
import os
import commands
import ConfigParser

class Netconf(ConfigParser.ConfigParser):
    def __init__(self,default=None):
       ConfigParser.ConfigParser.__init__(self,defaults=None)
    def optionxform(self,optionstr):
       return optionstr

def Def_Network(source_ip,network_card):
    str_card=network_card[:10]+'\\'+network_card #定义DEVICE:eth0:x
    network_path='/home/sysadmin/proxy_config/'
    network_template='ifcfg-ethx'
    network_full_path=network_path+network_template
    conf=Netconf()
    conf.read(network_full_path)
    conf.set("network_card","IPADDR",source_ip)
    conf.set("network_card","DEVICE",network_card)
    conf.write(open(network_path+network_card,'w'))
    cmd ="sed -i 's@ @@g' "+network_path+str_card
    (s,r)=commands.getstatusoutput(cmd)
    if s ==0:
      print "Success"
      #['ipv6init', 'userctl', 'dns2', 'dns1', 'ipaddr', 'onboot', 'netmask', 'device', 'type', 'gateway']

def main():
source_file='/home/sysadmin/proxy_config/unix_proxy.txt'
if os.path.exists(source_file):
   with open(source_file) as fn:
      data = fn.readlines()
      #print type(data)
      for ethxin data:
         ethx=ethx.split('\t')
         C_IP=ethx.strip()
         N_IP=ethx.strip()
         W_Card=ethx.strip()
         Def_Network(C_IP,W_Card)   
else:   
    print " %s file not found" %source_file


if __name__=='__main__':
    main()




注:遇到的问题,可能在其它服务的配置文件里不算什么问题,但是,在网卡设置中这是一个大的问题

[*]

[*]参数的大小写;ConfigParser模块中将所有读取的Key都变成了小写,而网卡的配置文件必须是大写;请查看ConfigParser模块的文件【我的是centos6.3x86_64系统,环境python2.6.6,路径为

/usr/lib64/python2.6/ConfigParser.py】
   如果要解决这个问题有两种方式:
   第一:要修改ConfigParser.py文件中的将return optinostr.lower()改成return optionstr即可。

1
2
354   def optionxform(self, optionstr):
355         return optionstr.lower()




   第二:通过类继承,重写optionxfrom方法。
   至于使用那种方法,请根据实际情况而定。

[*]

[*]生成的配置文件中,key = value等号两端有空格,配置文件不认,只好用sed命令替换了

5、原文件unix_proxy

1
2
1.2.3.4192.168.2.4   ifcfg-eth0:0
5.6.7.8192.168.2.8   ifcfg-eth0:1




6、模板文件ifcfg-ethx

1
2
3
4
5
6
7
8
9
10

DEVICE=eth0
ONBOOT=yes
TYPE=Ethernet
IPADDR=1.2.3.4
NETMASK=255.255.254.0
DNS2=1.2.4.8
GATEWAY=1.2.3.1
IPV6INIT=no
USERCTL=no




运行脚本即可绑定网卡的配置文件

三、总结
1、要多写,多看【标准库】这里有中文版本的,有能力的小伙伴可以去试着翻译一下 http://python.usyiyi.cn/python_278/tutorial/index.html
2、承认自己的不足,才能进步
3、多总结

页: [1]
查看完整版本: python 常用的模块 optparse与ConfigParser