Day 23 hashlib, configparser, logging module

Common Module II 

hashlib module

Algorithms Introduction

Python's hashlib provides a common digest algorithm, such as MD5, SHA1, and so on.

What is the digest algorithm it? Digest algorithm, also known as hash algorithm, hash algorithm. It is through a function to convert data of arbitrary length to a fixed length data string (typically represented by hexadecimal string.)

Digest by the digest algorithm is the function f () of an arbitrary length data DATA fixed length calculated summary digest, in order to discover whether the original data had been tampered with.

Digest algorithm is able to indicate whether the data has been tampered with, because digest function is a one-way function, calculate f (data) is easy, but it is very difficult to digest data by reverse thrust. Also, do a bit of modifications to the original data, will lead to the calculated digest completely different.

We have common Digest algorithm MD5, for example, to calculate the MD5 value of a string:

import hashlib
 
md5 = hashlib.md5 ()
md5.update('how to use md5 in python hashlib?')
print md5.hexdigest()

The results are as follows:
d26a53750bc40b38b65a520292f69306

If a large amount of data, the block can be called multiple times Update (), the final calculation result is the same:

md5 = hashlib.md5 ()
md5.update('how to use md5 in ')
md5.update('python hashlib?')
print md5.hexdigest()

MD5 digest algorithm is the most common, fast, the result is to generate a fixed 128 bit bytes, usually a 32-bit hexadecimal string representation. Another common digest algorithm is SHA1, MD5 calls and calls SHA1 completely analogous:

import hashlib
 
= hashlib.sha1 (SHA1)
sha1.update('how to use sha1 in ')
sha1.update('python hashlib?')
print sha1.hexdigest()

SHA1 is a 160 bit result of bytes, usually a 40-bit hexadecimal string representation. More secure than SHA1 and SHA256 algorithms is SHA512, but the slower the more secure algorithm, and a longer summary length.

Abstract algorithm

User name and password to log in to any site will allow users to store user login. How to store user name and password it? It is stored into a database table:

name    | password
--------+----------
michael | 123456
bob     | abc999
alice   | alice2008

If the user password saved in plain text, if the database is compromised, all the user's password fall into the hands of hackers. In addition, site operation and maintenance personnel can access the database, which is able to obtain all the user's password. The correct way to save the password is not stored in plain text user's password, but rather a summary store user passwords, such as MD5:

username | password
---------+---------------------------------
michael  | e10adc3949ba59abbe56e057f20f883e
bob      | 878ef96e86145580c38c87f0410ad153
alice    | 99b1c2188db85afee403b1536010c2c9

Consider such a case, many users prefer 123456,888888, password these simple passwords, thus, hackers can calculate in advance the values ​​of these common password MD5, to obtain a reverse thrust table:

'E10adc3949ba59abbe56e057f20f883e': '123456'
'21218cca77804d2ba1922c33e0151105': '888888'
'5f4dcc3b5aa765d61d8327deb882cf99': 'password'

This eliminates the need to crack, only need to compare the MD5 database, a hacker obtained using a common password for user account.

For the user, of course, do not use too simple password. However, we can strengthen it simple password protection on programming?

Since common password MD5 value can easily be calculated, so make sure to store the user's password has not been calculated MD5 those commonly used passwords, this approach is achieved by the original password plus a complex string, commonly known as "plus salt":

hashlib.md5("salt".encode("utf8"))

After Salt password MD5 process, it is not as long as the hacker knows Salt, even if the user input password is simple, it is difficult to reverse thrust by the clear text password MD5.

However, if two users use the same password for a simple example 123456, in the database, the two same MD5 value is stored, indicating that the two user's password is the same. Is there a way to let users use the same password to store different MD5 it?

If we assume that the user can not modify the login name, you can put the user login name as part of the Salt to calculate MD5, in order to achieve the same password is also different storage MD5.

Digest algorithm in many places have a wide range of applications. Pay attention to not digest algorithm encryption algorithm, it can not be used for encryption (plain text because they can not push through anti-summary), can only be used for tamper-proof, but it's one way to calculate characteristics determine the user's password can be verified without storing passwords in plain text .

 

configparser module

This is similar to the profile module is adapted to windows ini file format, may comprise one or more sections (sectionTop), each node can have a plurality of parameters (key = value).

Create a file

Look at a lot of common document format software is as follows:

[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes
  
[bitbucket.org]
User = hg
  
[topsecret.server.com]
Port = 50022
ForwardX11 = no

If you want to create such a document with python how to do it?

import configparser

config = configparser.ConfigParser()

config["DEFAULT"] = {'ServerAliveInterval': '45',
                      'Compression': 'yes',
                     'CompressionLevel': '9',
                     'ForwardX11':'yes'
                     }

config['bitbucket.org'] = {'User':'hg'}

config['topsecret.server.com'] = {'Host Port':'50022','ForwardX11':'no'}

with open('example.ini', 'w') as configfile:

   config.write(configfile)

Find Files

import configparser

config = configparser.ConfigParser()

# --------------------------- find the file contents, form-based dictionary

print(config.sections())        #  []

config.read('example.ini')

print(config.sections())        #   ['bitbucket.org', 'topsecret.server.com']

print('bytebong.com' in config) # False
print('bitbucket.org' in config) # True


print(config['bitbucket.org']["user"])  # hg

print(config['DEFAULT']['Compression']) #yes

print(config['topsecret.server.com']['ForwardX11'])  #no


print(config['bitbucket.org'])          #<Section: bitbucket.org>

for key in config [ 'bitbucket.org']: # Note that there are default default default keys
    print(key)

print (config.options ( 'bitbucket.org')) # with a for loop, find 'bitbucket.org' in all key

print (config.items ( 'bitbucket.org')) # found 'bitbucket.org' all key-value pairs

print (config.get ( 'bitbucket.org', 'compression')) # key in the method of Section yes get the corresponding value
 

CRUD operations

import configparser

config = configparser.ConfigParser()

config.read('example.ini')

config.add_section('yuan')



config.remove_section('bitbucket.org')
config.remove_option('topsecret.server.com',"forwardx11")


config.set('topsecret.server.com','k1','11111')
config.set('yuan','k2','22222')

config.write(open('new2.ini', "w"))

logging module

Functional simple configuration

import logging  
logging.debug('debug message')  
logging.info('info message')  
logging.warning('warning message')  
logging.error('error message')  
logging.critical('critical message') 

Python's logging module logs printed by default to the standard output, and only shows a greater than or equal WARNING level log, indicating that the default log level WARNING (log level Level CRITICAL> ERROR> WARNING> INFO> DEBUG), the default log format for the log level: Logger name: user output messages.

Flexible configuration log level, the log format, output location:

import logging

file_handler = logging.FileHandler(filename='x1.log', mode='a', encoding='utf-8',)
logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s -%(module)s:  %(message)s',
    datefmt='%Y-%m-%d %H:%M:%S %p',
    handlers=[file_handler,],
    level=logging.ERROR
)

logging.error ( 'Hello')

Cutting logs

import time
import logging
from logging import handlers

sh = logging.StreamHandler()
rh = handlers.RotatingFileHandler('myapp.log', maxBytes=1024,backupCount=5)
fh = handlers.TimedRotatingFileHandler(filename='x2.log', when='s', interval=5, encoding='utf-8')
logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s -%(module)s:  %(message)s',
    datefmt='%Y-%m-%d %H:%M:%S %p',
    handlers=[fh,sh,rh],
    level=logging.ERROR
)

for i in range(1,100000):
    time.sleep(1)
    logging.error('KeyboardInterrupt error %s'%str(i))
 

 

Configuration parameters:

logging.basicConfig () function can be changed by the logging module specific parameters default behavior parameters are available:

filename: Creating FiledHandler, such logs are stored in the specified file with the specified file name.
filemode: File Open, this parameter is specified in the filename, the default value "a" may also be designated as "w".
format: log handler specified display format.
datefmt: specify the date and time format.
level: Set rootlogger (behind will explain specific concepts) log level
stream: StreamHandler created with the specified stream. Can specify the output to sys.stderr, sys.stdout or file (f = open ( 'test.log' , 'w')), default sys.stderr. If both lists the filename and stream two parameters, the stream parameter is ignored.

the format parameter string format may be used:
% (name) is the name of S Logger
% (levelno) s digital form log level
% (levelname) s log level text form
% (pathname) s call log output function module the full path name, it may not
% (filename) s calling module log output function filename
% (module) s calling module name log output function
% (funcName) s calling a function for a log output function of
% (lineno) d line of code calls log output function of the statement where
% (created) f the current time, represented by the floating-point standard UNIX time representation
% at (relativeCreated) d output log information, the number of milliseconds since Logger created
% (asctime) s current time string. The default format is "2003-07-0816: 49: 45,896." Milliseconds after the comma
% (thread) d thread ID. It may not have
% (threadName) s thread name. It may not be
d Process ID% (process). May not
% (message) s user output message


Copy the code
logging.basicConfig () function can be changed by the logging module specific parameters default behavior parameters are available:

filename: Creating FiledHandler, such logs are stored in the specified file with the specified file name.
filemode: File Open, this parameter is specified in the filename, the default value "a" may also be designated as "w".
format: log handler specified display format.
datefmt: specify the date and time format.
level: set the log level rootlogger (behind will explain specific concepts) of
stream: Creating StreamHandler with the specified stream. Can specify the output to sys.stderr, sys.stdout or file (f = open ( 'test.log', 'w')), default sys.stderr. If both lists the filename and stream two parameters, the stream parameter is ignored.

the format parameter string format may be used:
% (Name) s Logger name
% (Levelno) s log level digital form
% (Levelname) s log level text form
% (Pathname) s call to the full path name of the log output function module may not
% (Filename) s call log output function module filename
% (Module) s call log output function module name
Function names% (funcName) s call log output function
OK code statements% (lineno) d log output function call where
% (Created) f the current time, represented by a standard floating point representation UNIX Time
Since the number of milliseconds Logger created when the d output log information% (relativeCreated)
% (Asctime) of the current time string s. The default format is "2003-07-0816: 49: 45,896." Milliseconds after the comma
% (Thread) d thread ID. Maybe not
% (ThreadName) s thread name. Maybe not
% (Process) d process ID. Maybe not
Message% (message) s user output
Copy the code

logger object configuration

import logging

logger = logging.getLogger()
# Create a handler, a log file for writing
= logging.FileHandler FH ( 'the test.log', encoding = 'UTF-. 8') 

# then create a handler, for output to the console
CH = logging.StreamHandler ()
Formatter logging.Formatter = ( '% (the asctime) s -% (name) s - % (levelname) s -% (message) s')
fh.setLevel(logging.DEBUG)

fh.setFormatter(formatter)
ch.setFormatter(formatter)
logger.addHandler (fh) #logger对象可以添加多个fh和ch对象
logger.addHandler (ch)

logger.debug ( 'log debug message)
logger.info (' log info message ')
logger.warning (' log warning message ')
logger.error (' error message log ')
logger.critical (' Critical message log ')
 

logging library provides a number of components: Logger, Handler, Filter, Formatter. Logger object provides an interface application can be used directly, Handler send logs to the appropriate destination, Filter provides a method of filtering log information, Formatter display format specified log. Further, by: logger.setLevel (logging.Debug) set the level, of course, can also

fh.setLevel (logging.Debug) set a single-file stream level.

Guess you like

Origin www.cnblogs.com/AaronY/p/12609885.html