Python references Socket and Scapy to implement stability testing

foreword

In daily testing, stability testing is an essential link, but it is difficult to simulate real customer scenarios. Moreover, after the stability testing starts, the collection of execution data is very little, so we combine the Socket in ptyhon Library and Scapy library, to achieve stability testing, random EPS, interval EPS occurrence probability, set execution time, simulated source IP, and specified destination IP are all configurable, and have the function of printing logs.

Socket and Scapy

Both are used to transmit data. What we mainly use is to transmit messages, which can be transmitted in the form of TCP and UDP. TCP transmission uses byte streams, and UDP is data packets, so two transmissions are used There are also some differences in how the code is referenced.

Socket characteristics

Python provides two basic socket modules, the first is Socket, which provides the standard BSD Sockets API, and the second is SocketServer, which provides the server center class, which can simplify the development of network servers. When TCP sends data , the TCP connection has been established, so there is no need to specify the address. UDP is connectionless-oriented, and each time you send it, you must specify who it is sent to.

Scapy features

Scapy involves techniques such as pf_packet socket programming, routing and object-oriented design, and it is written in python. In general, receiving messages needs to comply with certain protocols, so that both parties can process data through specific sticking and unpacking operations. In many cases, custom protocols are relatively simple. First, receive the message header, get the message length, and then get the message body. , but there are many protocols that are cumbersome to write, so the scapy library is used to obtain a message at a time. Generally, the header information of the message is similar, with source address, destination address, and message length.

Module composition

The timer module is used to execute a function every X seconds

insert image description here

The probability function of the random variable is used for the occurrence probability of EPS

insert image description here

Documentation for reading raw logs by the readfile moduleinsert image description here

The sending function is used to randomly extract any original log and send data packets according to EPS

insert image description here

Implementation process

set variable

Multiple EPS intervals can be set. For example, A, B, and C. The value of the EPS occurrence probability list must be three and the sum is 100. When running the script, only the variable can be modified, and the code area does not need to be changed. The file path cannot be empty.
Parse The file function will proofread the file before running the overall script
. Interpretation of the variables in the figure below: The running time of the script is 1,800,000 seconds, and the EPS is divided into two intervals 0-500 and 500-2000. The probability of random values ​​​​in the interval 0-500 is 95%, The probability of random values ​​in the 2000 interval is 5%. Read the txt file in the root directory, specify the IP of the receiver as 10.176.63.31, specify the port of the receiver as 514, initialize the total number of execution times, the number of interval execution times, and declare the socket.
insert image description here

execution method

insert image description here

log printing

The print log includes: the path of the file, the number of lines in the file, the start execution time, the number of data sent by EPS per second and the number of lines corresponding to the sent file (students who need detailed information can also print the log sample corresponding to the number of lines), the end execution time, The total amount of data sent, the probability of sending the EPS interval, and the sending time. If necessary, you can also add throwing exception output information. When the program is abnormally interrupted or manually interrupted, you can still output a complete log. You can also add the log if you need text correspondence. Write to txt file.
insert image description here

expand

Simulation source IP

The sending method pasted above is to send data packets with the local IP according to the Socket. If there is a special need, it can also simulate the source IP to send data packets.
Modules that need to be imported: from scapy.layers.inet import IP, UDP, from scapy.all import *, so that the data packets can be sent according to our preset IP
insert image description here

Data packet garbled problem (usage of python3)

Introduce the module import importlib importlib.reload(sys)
If the sent data packets are still garbled, you need to check whether there is a problem with the sent encoding format, try .encode('GBK') and .encode('UTF-8').
If you modify the encoding format and do not receive it, you need to check whether the sent data packet is too long, and the currently sent data packet is too long and cannot be sent.

other connection methods

When we choose the connection method, we can also choose TCP's SOCK_STREAM, which is more reliable than UDP, but the corresponding overhead will be larger

Guess you like

Origin blog.csdn.net/qq_36616956/article/details/128928988