python telnet

The meaning of various read functions in telnetlib

Origin
Recently, I wanted to use Python's telnetlib to write a test script. But a bunch of read functions in telnetlib are lazy and eager; I don't know the difference between them. Searching on python documentation and google also yielded no results. Fortunately, python is open source. It is relatively simple and clear to find the source code.

Basic Principles
To understand the meaning of each read function in telnetlib, we must first understand the working principle of telnetlib. Telnetlib first receives data from the network through the socket connection, stores the data in its own raw queque, and then processes (cooks) it (related to the telnet protocol). The processing results are stored in the cooked queue for application access. The whole process is as shown below.

---------, ,----------, ,-----, ,-------, ,----- ------,
  network |=====>|socket buf|=====>|raw-q|=====>|cooked-q|=====>|application|
---------` `----------` `-----` `--------` `---------- -`
    | | | | |
    |               |                 |              |               |
    |               |                 |              |---------------|
    |               |                 |               read_very_lazy |
    |               |                 |                              |
    |               |                 |------------------------------|
    |               |                          read_lazy             |
    |               |                                                |
    |               |------------------------------------------------|
    |                          read_very_eager/read_eager            |
    |                                                                |
    |------------------------------------------------- ---------------|
    | read_until/read_all/read_some/expect |

The meaning of each read function The difference between
various read functions lies in the different stages they cover.

read_very_lazy
only reads processed data from cookedq.
read_lazy
If rawq has data, process the data in rawq, and then read the processed data from cookedq.
read_eager
accepts data from the system's socket buffer (ie, non-blocking mode 1 reads data) and processes it, and then reads data from cookedq.
read_very_eager
is similar to read_eager. The difference is that read_eager returns as soon as it
successfully reads data from cookedq, while read_very_eager tries to read as much data as possible. The rest

of the read functions basically block and wait for network data until the desired data is read. 1. A note on non-blocking: The non-blocking mentioned in this article is for receiving ordinary data, which is "Do not block unless in the midst of an IAC sequence." in the python documentation.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326626984&siteId=291194637