Speaking of proxy, it will certainly be related to network protocols, including (TCP, IP, HTTP). The process in the network needs to communicate through socket. Socket can be considered as a kind of interface abstracted by the operating system, so that users can deal with the underlying network protocols more conveniently.

0x01

Let’s take a look at TCP socket programming.

The service side

import socket
import threading

# AF_INET: IPV4 based network communication SOCK_STREAM: TCP based streaming socket communication
s = socket.socket(socket.AF_INET. socket.SOCK_STREAM)
Bind the socket to the address
s.bind(('127.0.0.1'. 8888))
Listen for TCP incoming connections
s.listen(5)
def handle_tcp(sock. addr) :
    print("new connection from %s:%s" % addr)
    sock.send(b'Welcome! ')

    while True:
        data = sock.recv(1024)
        if not data:
            break
        sock.send(b'Hello, %s! ' % data)
    sock.close(a)


while True:
    sock. addr = s.accept(a)
    t = threading.Thread(target=handle_tcp. args=(sock. addr))
    t.start(a)
Copy the code

The client

Import socket s = socket.socket(socket.af_inet, socket.sock_stream) s.socket (('127.0.0.1', 8888)) print(s.recv(1024)) for data in [b'dog']: s.send(data) print(s.recv(1024)) s.close()Copy the code

The above is a very simple client and server example, the server side uses threads, mainly to be able to handle multiple requests at the same time. Otherwise the whole program will block every time a request is processed.

After the wireshark is used to capture the request, you can view that the client successfully shakes hands with the server for three times and then sends data. After the request is successfully sent, the two parties wave hands to disconnect the connection for four times.

Let’s talk about the process

1. The server initializes a socket object, binds the socket to (127.0.0.1, 8888), listens, and blocks the Accept function until it receives a connection.

2. The client also initializes a socket object and calls CONNECT to establish a connection with the server.

3. The server accept function returns a new Sock socket object, which is passed into a new thread to interact with the client.

4. The recV and Send functions of the socket are used for data exchange.

5. Finally, socket close closes the socket.

Because the data transmitted by TCP belongs to stream, there is no limit on the number of times recV and SEND can be called, and there is no limit on the number of times the data can be sent or bound. The udp module encapsulates a UDP packet for each write operation performed by the sender and sends it. The receiver also performs a read operation for each UDP packet. Each read operation must be completed.

0x02

Take a look at UDP socket programming

The service side

import socket

# AF_INET: IPV4 based network communication SOCK_DGRAM: udp based streaming socket communication
s = socket.socket(socket.AF_INET. socket.SOCK_DGRAM)
Bind the socket to the address
s.bind(('127.0.0.1'. 8888))

while True:
    data. addr = s.recvfrom(1024)
    print('Received from %s:%s. ' % addr)
    s.sendto(b'Hello, %s! ' % data. addr
Copy the code

The client

import socket

s = socket.socket(socket.AF_INET. socket.SOCK_DGRAM)

for data in [b'dog'] :
    s.sendto(data. ('127.0.0.1'. 8888))
    print(s.recv(1024))
s.close(a)
Copy the code

Udp does not need to establish a connection, but only needs to know the IP address and port number of the peer party to send data packets. But we don’t know if we can get there. So the client sends data directly to the server via sendto(), and the server calls recvfrom() to get the data.