Python simulates TCP and UDP to send packets

Introduction

In the project test, it is necessary to send data packets with specified content to the target location, and the number of packets to be sent is extremely large. The real environment cannot meet the test requirements, but the test can be supported by using Python as the basic language, combined with Socket and Scrapy, and Socket can support A large number of data packets are sent from the local machine to the target machine, and scapy can simulate the source IP to send data packets to the target machine, but the sending rate and size per second are not as good as Socket, and the two modules have their own strengths and complement each other.

Socket and Scapy

When it comes to network services, Socket and Socket programming are inseparable. In addition, there is the crawler framework Scrapy. This article mainly summarizes the transmission of data packets in the above two ways, both of which can be transmitted in the form of TCP and UDP, but TCP transmission uses byte streams, and UDP is data packets, so there are some differences in encoding when using the two transmission methods.

Socket

Socket is also called "socket". The socket format: socket(family, type[,protocal]) is used to describe the IP address and port, and is the handle of a communication chain. Applications usually send requests to the network or respond to network requests through "sockets". Examples include China Mobile and the like.
Python provides two basic socket modules to save, 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 the network server (when TCP sends data , the TCP connection has been established, so there is no need to specify the address. UDP is connectionless, and each time you send it, you must specify who it is sent to).
To communicate using Internet Protocol, use AF_INET: TCP or UDP. This is by far the most common choice,. Use PF_PACKET if you want to send and receive messages at the most basic level below the Internet protocol layer, for example, because you are implementing the protocol yourself and your process must run as root (or use a special function) to use PF_PACKET.
When we choose the connection method, we can also choose TCP SOCK_STREAM, which is more reliable than UDP, but the corresponding overhead will be large.
insert image description here

Scapy

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.
Scrapy's official website address is: link

Domo case

Socket example

Modules that need to be imported: import socket
insert image description here

Scapy example

Modules that need to be imported: from scapy.layers.inet import IP, UDP and 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.

Guess you like

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