io.StringIO()The use environment of

You want to send log output over the network in a logger that writes to a file.

1. Read the file and write its contents to the network

2. Write the log to a StringIO object (an in-memory string buffer) and send it to its network target without touching the file system

Python encryption modulehashlib andbase64

hashlib

Encrypt a piece of text

importAlgorithm objects such as md5 also provide properties such as digest_size and block_size, indicating the size of the encrypted text hashlib.new("md5"."Hello world!").digest()
Copy the code

Append mode

importMm = hashlib.md5() # create an MD5 object."Hello") # Encrypt text with update method mm. Update (" world!"Update (mm. Update ("Hello world!"Print mm. Hexdigest () print mm. Hexdigest () print mmCopy the code

base64

Methods collection

Encode (),decode() is specially used to encode and decode files, can also be StringIO data for encoding and decoding; Convert STR characters to byte encodeString (), decodeString () is used to encode and decode strings; B64encode () and b64decode() are used to encode and decode strings converted by byte, and have a function to replace symbolic characters; Urlsafe_b64encode () and urlsafe_b64decode() this is used specifically for url base64 codec.Copy the code
import base64

obj = 'hello world! 'Byte obj_byte_encode ='hello world! 'Byte obj_byte_encode_enBase64 = base64.b64encode(obj_byte_encode) # Obj_str_enBase64 = obj_byte_encode_enbase64. decode() print(obj_str_enBase64)# Decode (utf8 by default) Decode obj_byte_encode = base64.b64decode(obj_byte_encode_enBase64) # decode the output byte object into STR object obj = Obj_byte_encode.decode () print(obj)#Copy the code