欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

day23-python之日志 re模块

程序员文章站 2022-04-28 14:14:10
1.logging 2.configparser 3.md5加密 ......

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