VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • python基础教程之day23-python之日志 re模块

1.logging

复制代码
 1 import logging
 2 
 3 #-----------------------------------logging.basicConfig
 4 logging.basicConfig(
 5     level=logging.DEBUG,
 6     filename="logger.log",
 7     filemode="w",
 8     format="%(asctime)s %(filename)s[%(lineno)d]  %(message)s"
 9 
10 
11 )
12 #
13 # logging.debug('hello')
14 # logging.info('hello')
15 # logging.warning('warning message')
16 # logging.error('error message')
17 # logging.critical('critical message')
18 
19 #-----------------------------------logger
20 def logger():
21     logger=logging.getLogger()
22 
23 
24     fh=logging.FileHandler("test_log")
25     #ch=logging.StreamHandler()
26 
27     fm=logging.Formatter("%(asctime)s  %(message)s")
28 
29     fh.setFormatter(fm)
30     #ch.setFormatter(fm)
31 
32     logger.addHandler(fh)
33     #logger.addHandler(ch)
34     logger.setLevel("DEBUG")
35 
36     return logger
37 # #----------------------
38 logger=logger()
39 
40 #
41 # logger.debug("debug")
42 # logger.info("info")
43 # logger.warning("warning")
44 # logger.error("error")
45 # logger.critical("critical")
46 #--------------------------------------------------
47 import logging
48 #
49 logger=logging.getLogger()
50 
51 
52 logger1 = logging.getLogger('mylogger')
53 logger1.setLevel(logging.DEBUG)
54 
55 logger2 = logging.getLogger('mylogger')
56 logger2.setLevel(logging.WARNING)
57 
58 
59 fh=logging.FileHandler("test_log-new")
60 ch=logging.StreamHandler()
61 
62 logger.addHandler(ch)
63 logger.addHandler(fh)
64 
65 logger1.addHandler(fh)
66 logger1.addHandler(ch)
67 
68 logger2.addHandler(fh)
69 logger2.addHandler(ch)
70 
71 
72 # logger.debug('logger debug message')
73 # logger.info('logger info message')
74 # logger.warning('logger warning message')
75 # logger.error('logger error message')
76 # logger.critical('logger critical message')
77 
78 # logger1.debug('logger1 debug message')
79 # logger1.info('logger1 info message')
80 # logger1.warning('logger1 warning message')
81 # logger1.error('logger1 error message')
82 # logger1.critical('logger1 critical message')
83 
84 logger2.debug('logger2 debug message')
85 logger2.info('logger2 info message')
86 logger2.warning('logger2 warning message')
87 logger2.error('logger2 error message')
88 logger2.critical('logger2 critical message')
复制代码

2.configparser

复制代码
 1 import configparser
 2 
 3 # config = configparser.ConfigParser()     #config={}
 4 # #
 5 # #
 6 # #
 7 # #
 8 # config["DEFAULT"] = {'ServerAliveInterval': '45',
 9 #                      'Compression': 'yes',
10 #                      'CompressionLevel': '9'}
11 # #
12 # #
13 # #
14 # config['bitbucket.org'] = {}
15 # config['bitbucket.org']['User'] = 'hg'
16 # #
17 # config['topsecret.server.com'] = {}
18 # topsecret = config['topsecret.server.com']
19 # topsecret['Host Port'] = '50022'  # mutates the parser
20 # topsecret['ForwardX11'] = 'no'  # same here
21 # #
22 # #
23 # #
24 # with open('example.ini', 'w') as f:
25 #     config.write(f)
26 
27 
28 #------------------------------------------------------增删改查、
29 import configparser
30 #
31 config = configparser.ConfigParser()
32 
33 #---------------------------------------------查
34 # print(config.sections())   #[]
35 
36 config.read('example.ini')
37 # #
38 # print(config.sections())   #['bitbucket.org', 'topsecret.server.com']
39 
40 # print('bitbucket.org' in config)# False
41 #
42 # print(config['bitbucket.org']['User']) # hg
43 #
44 # print(config['DEFAULT']['Compression']) #yes
45 #
46 # print(config['topsecret.server.com']['ForwardX11'])  #no
47 
48 
49 # for key in config['bitbucket.org']:
50 #     print(key)
51 
52 #
53 #
54 # print(config.options('bitbucket.org'))#['user', 'serveraliveinterval', 'compression', 'compressionlevel', 'forwardx11']
55 # print(config.items('bitbucket.org'))  #[('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'yes'), ('user', 'hg')]
56 #
57 # print(config.get('bitbucket.org','compression'))#yes
58 #
59 #
60 # #---------------------------------------------删,改,增(config.write(open('i.cfg', "w")))
61 #
62 #
63 config.add_section('yuan')
64 config.set('yuan','k1','11111')
65 #
66 config.remove_section('topsecret.server.com')
67 config.remove_option('bitbucket.org','user')
68 # #
69 #
70 config.write(open('i.cfg', "w"))
复制代码

3.md5加密

复制代码
 1 # import hashlib
 2 
 3 # obj=hashlib.md5()
 4 #
 5 # obj.update("admin".encode("utf8"))
 6 # print(obj.hexdigest())    #21232f297a57a5a743894a0e4a801fc3
 7 
 8 # obj.update("adminroot".encode("utf8"))
 9 # print(obj.hexdigest())#   4b3626865dc6d5cfe1c60b855e68634a
10                       #   4b3626865dc6d5cfe1c60b855e68634a
11 
12  
复制代码


相关教程