preface
Websocket is a network transport protocol. Full duplex communication can be performed over a single TCP connection. Based on this, Websocket makes the communication between client and server easier and more efficient.
What is the websocket
Websockets are standalone protocols created on top of TCP. The agreement was born in 2008 and became an international standard in 2011. One of its key features is full duplex, that is, once a connection is established, the server or client can actively push messages to each other.
Before the emergence of Websocket, if a website needs to implement push technology, it adopts the way of polling, that is, the browser sends requests to the server at regular intervals. The disadvantage of this mode is that the browser needs to continuously send requests to the server, which consumes a lot of bandwidth resources. The newer Comet technology, while capable of two-way communication, still requires repeated requests, and the long HTTP connections commonly used in Comet can also consume server resources.
Based on the above situation, HTML5 defines the Websocket protocol, which can better save server and bandwidth resources. And realize efficient real-time communication. Currently, all browsers support it.
Websocket communication principle and mechanism
Websocket is a new protocol, but it cannot exist without HTTP. When a client constructs a WebSocket instance and connects to the server, it sends an HTTP packet request first. Tells the server to switch the communication protocol to WebSocket.
If the server supports the WebSocket protocol, it switches the communication protocol to WebSocket and returns a response message. At this point, the return status code is 101, indicating that the protocol conversion request is approved, and data can be transferred.
Websocket implements the handshake protocol over HTTP because it is compatible and the default ports are 80 and 443. The handshake phase is not easily blocked by the firewall.
The characteristics of the websocket
- The protocol header contains less information when the server and client exchange data
- The protocol adopts full-duplex. Compared with the MODE in which the HTTP request client initiates a request and the server responds only, the delay is significantly lower
- Good compatibility with HTTP, the default ports are 80 and 443. The handshake uses HTTP and is not easily masked by the firewall
- Supports text and binary data transfer
- Users can implement user-defined sub-protocols by themselves
- Maintains a long connection between the server and client through the heartbeat mechanism
Build a small example of real-time log tracing
The server starts a service that listens for log scripts and limits the number of paths allowed (to prevent hackers from scanning the entire server to exploit vulnerabilities). The server parses the client request and returns the log message content to the client. The server periodically sends heartbeat checks to the client. If it does not receive any response from the client, the server disconnects from the client
The server core program code logic is as follows
with open(file_path) as f: Select * from logfile (NUM_LINES); Join (deque(f, NUM_LINES)) content = conv.convert(content, Full =False) await websocket.send(content) Last_heartbeat = time.time() while True: end ()) content = f.read() if content: content = conv.convert(content, full=False) await websocket.send(content) else: Sleep (1) # await asyncio.sleep(1) # await asyncio.sleep(1) # await asyncio.sleep(1) # await asyncio.sleep(1) # await asyncio.sleep(1) # If time.time() - last_heartbeat > HEARTBEAT_INTERVAL: try: await websocket.send('ping') pong = await asyncio.wait_for(websocket.recv(), 5) logger.info(f"pong:{pong}") if pong ! = 'pong': raise Exception() except Exception: raise Exception('Ping error') else: last_heartbeat = time.time() else: await websocket.close()Copy the code
The client is very simple. It listens to the log file of the server and outputs the log or directly displays the log on the front-end page in real time. Accordingly, if long-term monitoring is required, the server also needs to respond to heartbeat feedback when sending heartbeat detection signals
The client core code logic is as follows
async def consumer_handler(websocket: WebSocketClientProtocol) -> None: async for message in websocket: log_message(message) if message == "ping": await websocket.send("pong") async def cousume(hostname: str, port: int, log_file: str, tail:bool=True) -> None: websocket_resource_url = f"ws://{hostname}:{port}{log_file}" if tail: websocket_resource_url = f"{websocket_resource_url}? tail=1" async with websockets.connect(websocket_resource_url) as websocket: await consumer_handler(websocket) def log_message(message: str) -> None: logger.info(f"Message: {message}")Copy the code
Here a log production file is simulated
The code logic is as follows
Import OS from loguru import Logger Class LoggerExtend(object): # Folder = '.. /logs' def __init__(self, filename, folder=None): self.folder = folder or self.folder if not os.path.exists(self.folder): os.mkdir(self.folder) self.file = self.folder + '/' + filename logger.add(self.file, rotation="100 MB") @property def get_logger(self): return logger if __name__ == '__main__': logger = LoggerExtend(os.path.basename(__file__).replace(".py", ".log")).get_logger import time while True: Logger. The info (" hello aaa ")Copy the code
Finally, start the log production program, server program, and client program
After the log production file is started, the running effect is as follows
The server startup program is running, and no run log is generated
Start the client program at this time, and the result is as follows
In this case, the server generates run logs, as shown in the following figure
For the complete code, go to GitHub
Github.com/hacksman/le…
Log production program path:
common/logger_extend.py
Server program path:
websoctet_lab/log_server.py
Client program path:
websoctet_lab/cousumer_log_view.py
The resources
[1] How To Create a WebSocket in Python
[2] How To Create a WebSocket in Python
[3] WebSocket tutorial
[4] WebSocket – Summary of mainstream Implementations of Python _LIN blog -CSDN blog
[5] GoEasy more simple Websocket | | push expert Web news
[6] Python WebSocket Client implementation
[7] Theory and Practice: From zero understanding of WebSocket communication principle, protocol format, security – Web IM development/special technical area – INSTANT Messaging developer community!
[8] WebSocket Details (I) : A preliminary understanding of WebSocket technology – Web IM development/special technical area – instant messaging developer community!
[9] Log Tailer with WebSocket and Python