1, hashlib encryption module
1. Encryption: convert plaintext data into ciphertext data through a series of algorithms for data security. Encrypted ciphertext data cannot be decrypted into plaintext data. Generally, the user password storage is in the form of ciphertext. Only the user knows what the plaintext is, and the programmer cannot know it. If the stored data is leaked, there is no way to crack it.
2. Encryption algorithm: md series, sha series, base series, hmac series
3. Basic usage: import hashlib common md5
4. As long as the plaintext is the same, the encrypted ciphertext is also the same, which has nothing to do with the transmission method.
5. The longer the ciphertext, the more complex the internal corresponding algorithm is and the more difficult it is to be cracked. The more complex the algorithm is, the more resources it needs to consume, and the more data it needs to occupy to send based on the network. What algorithm to use depends on the project. Generally speaking, md5 is OK.
6. Salt processing: add some interference items during the encryption of plaintext data.
7. Dynamic salting: add some changed interference items during the encryption of plaintext data. For example, the current time, a part of the user name, a random string, and so on.
8. Check file consistency: if the file is very large, all encryption is quite time-consuming and resource consuming. At this time, slice reading can be done. Bit stream technology, power-off continuous transmission technology
import hashlib # Call module # 1. Confirm the encryption type first, md5 common md5 = hashlib.md5() # 2. Transmit plaintext data to the encryption algorithm (update can only accept data of bytes type) md5.update('data'.encode('utf8')) # If it is a number or letter, it can be written as md5.update(b 'character') # Obtain encrypted ciphertext data res = md5.hexdigest() print(res)
2, logging module
1. There are five levels of logging, which are becoming more and more important from top to bottom. The default level of logging is above 30: import logging
logging.debug() # 10
logging.info() # 20
logging.warning() # 30
logging.error() # 40
logging.critical() # 50
2. Simple use: import logging (templates can be directly applied)
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('The log module is easy to learn. Don't scare yourself')
3. How to control the output location of logs and how to achieve different formats in different locations. At this time, you need to use the four log objects.
1.logger object: responsible for generating logs
2.filter object: it is responsible for filtering logs (generally not used, directly ignored)
3.handler object: responsible for the location of log generation
4.Formatter object: responsible for the format of the log
import logging # Import module # 1. Log object logger = logging.getLogger('Transfer record') # 2.handler object: responsible for the location of log generation hd1 = logging.FileHandler('a1.log', encoding='utf') # Generate to file a1 hd2 = logging.FileHandler(r'a2.log', encoding='utf8') # Generate to file a2 hd3 = logging.StreamHandler() # Generate to terminal # 3. Responsible format fm1 = logging.Formatter( fmt='%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S %p', ) fm2 = logging.Formatter( fmt='%(asctime)s - %(name)s %(message)s', datefmt='%Y-%m-%d', ) # 4. Bind the log to the handler object, that is, confirm the location where the log object is generated logger.addHandler(hd1) logger.addHandler(hd2) logger.addHandler(hd3) # 5. Bind format formatter object hd1.setFormatter(fm1) hd2.setFormatter(fm2) hd3.setFormatter(fm1) # 6. Set log level logger.setLevel(10) # 8. Log logger.debug('What did you write')
5. Configuration dictionary
import logging import logging.config standard_format = '[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]' \ '[%(levelname)s][%(message)s]' # Where name is the name specified by getlogger simple_format = '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s' logfile_path = 'a3.log' # Here, a path is fixed directly, and the subsequent path should be obtained with code # log configuration dictionary LOGGING_DIC = { 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'standard': { 'format': standard_format }, 'simple': { 'format': simple_format }, }, 'filters': {}, # Filter LOG 'handlers': { #Log printed to terminal 'console': { 'level': 'DEBUG', 'class': 'logging.StreamHandler', # Print to screen 'formatter': 'simple' }, #Print logs to files and collect logs of info and above 'default': { 'level': 'DEBUG', 'class': 'logging.handlers.RotatingFileHandler', # Save to file 'formatter': 'standard', 'filename': logfile_path, # log file 'maxBytes': 1024*1024*5, # Log size 5M 'backupCount': 5, 'encoding': 'utf-8', # The coding of log files, no longer have to worry about Chinese log garbled }, }, 'loggers': { #The logger configuration empty string obtained by logging.getlogger (_name_) can be used as a key to be compatible with all logs '': { 'handlers': ['default', 'console'], # Here, both handler s defined above are added, that is, log data is written to the file and printed to the screen 'level': 'DEBUG', 'propagate': True, # Pass up (higher level logger) }, # When the key does not exist (the key is set to an empty string), the k:v configuration will be used by default }, } # Use configuration dictionary logging.config.dictConfig(LOGGING_DIC) # Automatically load the configuration in the dictionary, using the module logging.config logger1 = logging.getLogger('Object name') logger1.debug('come on.')
3, Third party module
1. The third-party module is not built in python. It can only be imported and used after downloading based on the network.
2. There are two ways to download third-party modules:
Method 1: the command line finds the pip folder in the python interpreter folder with the help of pip tool, and adds the path to the environment variable of the operating system. The default download source path is the official website
If the pip3 install module name # does not know the version number, the latest version will be downloaded by default
pip3 install module name = = version number # specified version download
pip3 install -i warehouse address # temporarily switches the source path of the download module
To permanently modify the path of the download module, you need to modify the python interpreter source file
Method 2: pycharm shortcut
settings--project--python interpreter -- double click under the package or the + sign on the right -- search for the module name. If you need to switch the download source path, click manageg management to add it.
3. Switch the download source:
The default download channel of pip command is the foreign python official website (sometimes very slow)
We can switch the download source (warehouse)
(1) Alibaba cloud http://mirrors.aliyun.com/pypi/simple/
(2) Watercress http://pypi.douban.com/simple/
(3) Tsinghua University https://pypi.tuna.tsinghua.edu.cn/simple/
(4) University of science and technology of China http://pypi.mirrors.ustc.edu.cn/simple/
(5) Huazhong University of science and technology http://pypi.hustunique.com/
4. Download error reporting: possible error reporting and solutions for downloading third-party modules
1. The error message contains the keyword timeout
Cause: network instability
Action: try again or switch to a more stable network
2. pip command not found
Add environment variable
3. There are no keywords. Different modules report different errors
Cause: the module requires a specific computer environment
Measures: copy the error information and open the browser Baidu search
pip downloads an error message from a module