Python 19th days-catch a cold, python catch a cold
ConfigParser module,Hashlib module and hmac Module:
Create a configuration file:
1 import configparser 2 3 config = configparser. configParser () # create an object variable in the configuration file 4 # global configuration 5 config ["DEFAULT"] = {'serveraliveinterval ': '45', 6' compression ': 'Yes', 7' CompressionLevel ': '9'} 8 # create a domain name 9 config ['uge3. cn '] = {} 10 uge3 = config ['uge3. cn '] 11 uge3 ['user'] = 'yjj '12 13 config ['topsecret .server.com'] = {} 14 topsecret = config ['topsecret .server.com '] 15 topsecret [' host Port '] = '000000' # mutates the parser16 topsecret ['forwardx11'] = 'no' # same here17 18 config ['default'] ['forwardx11'] = 'Yes '19 with open ('example. ini ', 'w') as configfile: 20 config. write (configfile) # write the configuration file to the Open Document
View:
Import configparserconfig = configparser. configParser () # create an object variable config in the configuration file. read ('example. ini ') # Read the file print (config. sections () # output related content node_name = config. sections () [1] print (config [node_name]) for I, v in config [node_name]. items (): # print (I, v) print (config. options ('uge3. cn ') # print the selected domain name information and holographic information print (config. items ('topsecret .server.com ') # print the selected domain name information \ value and holographic information, Value
Modify, add, and delete:
1 import configparser 2 config = configparser. configParser () # create an object variable in the configuration file 3 4 config. read ('example. ini ') # Read File 5 node_name = config. sections () [1] 6 print (config [node_name]) 7 config. remove_option (node_name, 'forwardx11') # Delete the specified entry 8 config. set (node_name, 'host port', '123') 9 config. write (open ('example _ 2. ini ', 'w') # rewrite the file 10 sec = config. has_section ('wupeiqi ') # search for content 11 print (sec) 12 sec = config. add_section ('wupeiqi ') # add content 13 config. has_section ('wupeiqi2 ') # search for content 14 config. add_section ('wupeiqi2 ') # add content 15 config. write (open ('I. cfg ', "w") # rewrite the file
Hashlib module:
Encryption type: MD5, SHA1, shaloud, SHA256, SHA384, SHA512
1 import hashlib 2 m = hashlib. md5 () # Use the MD5 method 3 m. update (B 'yan ') # algorithm 4 print (m. hexdigest () # output 5 m in hexadecimal format. update (B 'jingjing') 6 print (m. hexdigest () #41e76e38a109317422894a86ed970288 7 m2 = hashlib. md5 () # use MD5 Method 8 m2.update (B 'yanjingjing ') # algorithm 9 print (m. hexdigest () #41e76e38a109317422894a86ed97028810 # same string, md5 will always be the same
Hmac module:
1 h = hmac. new (B '000000', B 'bcd') # It creates key and content internally and then encrypts 2 print (h. hexdigest ())