One, socketserver
The bottom layer of the network protocol is the socket, which is based on the socket module and encapsulated with another layer, which is the socketserver. The socketserver is to realize the concurrency of the tcp protocol on the server side.
1. Basic writing
server side writing:
# Server #import socket module import socketserver #Create a class that inherits the BaseRequestHandler class class MyServer(socketserver.BaseRequestHandler): def handle(self): print("successful call handle method") #ThreadingTCPServer(IP port number, custom class) server = socketserver.ThreadingTCPServer(("127.0.0.1",9003),MyServer) server.serve_forever()
Client:
import socket sk=socket.socket() sk.connect(("127.0.0.1",9003)) #Logic for sending and receiving data sk.close()
operation result:
Repeated execution of the client side, continuous automatic execution of the handle method
2. Implement circular concurrency through socketserver
Server:
# Server import socketserver class MyServer(socketserver.BaseRequestHandler): def handle(self): conn=self.request while True: # Receive data res=conn.recv(1024) res1=res.decode() print(res1) conn.send(res1.upper().encode()) server=socketserver.ThreadingTCPServer(("127.0.0.1",9004),MyServer) server.serve_forever()
Client:
#client import socket sk=socket.socket() sk.connect(("127.0.0.1",9004)) while True: strvar="you can you up" sk.send(strvar.encode()) res=sk.recv(1024) print(res.decode()) sk.close()
operation result:
Server:
Client:
2. haslib
The hashlib module is a collection of a bunch of encryption algorithms. There are more than one encryption methods for hash algorithms. md5 encryption and hmac encryption are commonly used.
https://www.cmd5.com/ md5 decryption website, complex passwords cannot be decrypted
Application scenario: use when verification function is required
1. Basic usage
#import hashlib module import hashlib #Encrypt with md5 and generate objects hm=hashlib.md5() #Convert the string to be encrypted into a byte stream and pass it into hashlib for processing hm.update("123456".encode("utf-8")) #Get 32-bit hexadecimal string res=hm.hexdigest() #print encrypted string print(res)
operation result:
By decrypting the website, we view the characters corresponding to the encrypted string
2. Add salt
Add a keyword to encrypt the original string to make the password more complex and not easy to be cracked
import hashlib hm=hashlib.md5("Xman".encode("utf-8")) hm.update("123456".encode("utf-8")) res=hm.hexdigest() print(res)
operation result:
View the characters corresponding to the encrypted string through the decryption website
Purchase prompt appears due to high password complexity
3. Dynamic salt addition
Salt the password with a random number, this time we set a more complex password
import hashlib import random res1=random.randrange(100000,1000000) hm=hashlib.md5(str(res1).encode("utf-8")) hm.update("8217wlsd".encode("utf-8")) res2=hm.hexdigest() print(res2)
operation result:
View the characters corresponding to the encrypted string through the decryption website
The web page is unbreakable due to more complex passwords
4. hmac encryption
Dynamic salting
# import hmac module import hmac import os #Randomly generate a byte stream of specified bits key=os.urandom(32) msg=b"123456" #hmac( salt, password) hn=hmac.new(key,msg) res=hn.hexdigest() print(res)
operation result:
View the characters corresponding to the encrypted string through the decryption website
The webpage cannot be reversed