Python network programming: usage of socket package

Sustained replenishment

1 Network programming

  Network programming is mainly used for communication between two or more computers, and it can also be communication between different processes in the same computer. Socket sockets can be used to implement network communication. Regarding Socket sockets, you need to pay attention to the following points:

  • Socket is an abstraction layer between the application layer and the transport layer in network communication. Using sockets can simplify the complex communication process between hosts in network communication. Sockets can provide some interfaces for the application layer, and the application layer can use the interfaces provided by sockets to realize communication between hosts in the network. .
  • The socket mainly has three important parameters: the target IP, the transport protocol (TCP or UDP) used by the transport layer, and the port used by the transport layer. In the TCP/IP protocol, "IP address + TCP or UDP port number" can uniquely identify a process in network communication. So "IP address + port number" corresponds to a Socket.
  • The two processes that want to establish a connection each have a Socket to identify, then the Socket pair formed by these two Sockets uniquely identifies a connection. Therefore, Socket can be used to describe the one-to-one relationship of network connections.

Let's take a simple example to see how the Socket socket implements data exchange between processes. details as follows:

import socket
import threading
class Socket_APP(threading.Thread):
    def __init__(self,port,content=''):
        self.port=port
        self.socket=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
        self.socket.bind(("localhost",port))
        self.msg=content
        threading.Thread.__init__(self)
    
    def run(self):
        if self.port==1000:
            self.socket.sendto(bytes(self.msg,encoding='utf-8'),
                             ('localhost',2000))
            print("端口{}发送的内容为:{}".format(self.port,self.msg))
            self.socket.close()
        if self.port==2000:
            msg,addr=self.socket.recvfrom(1024)
            msg=msg.decode('utf-8')
            print("端口{}收到的内容为:{}".format(self.port,msg))
            self.socket.close()
if __name__=="__main__":
    app1=Socket_APP(1000,'hello world')
    app2=Socket_APP(2000)
    app1.start()
    app2.start()

In this case, the process app1 is used to simulate the sending process (port number is 1000), and app2 is used to simulate the receiving process (port number is 2000). The execution result of the above code is as follows:
insert image description here
tips: For other contents of the socket, you can refer to material 4, so I won’t go into details here.

References

  1. https://blog.csdn.net/m0_51683653/article/details/127679387
  2. https://www.runoob.com/python+/python-socket.html
  3. https://blog.csdn.net/qq_51654808/article/details/125221403
  4. Python-based socket programming

Guess you like

Origin blog.csdn.net/yeshang_lady/article/details/129798206