Python network programming-socket, python Network Programming
1. TCP/IP
To understand socket, you must first familiarize yourself with the TCP/IP Protocol family, namely, the Transport Control Protocol/internetprotocol, defines the criteria for how a host connects to the Internet and how data is transmitted between them. Literally, TCP/IP is the combination of TCP and IP protocols, but in fact, the TCP/IP protocol refers to the entire TCP/IP protocol family of the Internet. Different from the seven layers of the ISO model, the TCP/IP protocol reference model classifies all TCP/IP series protocols into four abstraction layers:
Application Layer: TFTP, HTTP, SNMP, FTP, SMTP, DNS, Telnet, etc.
Transport Layer: TCP, UDP
Network Layer: IP, ICMP, OSPF, VPN, IGMP
Data link layer: SLIP, CSLIP, PPP, MTU
Each abstraction layer is built on the services provided by the lower layer and provides services for the higher layer. It looks like this
In TCP/IP, two Internet hosts are connected through two routers and corresponding layers. Applications on various hosts read from each other through some data channels.
2. socket Concept
Two programs on the network exchange data through a two-way communication connection. one end of the connection is called a socket. A socket is also called a "socket". It is used to describe the IP address and port. It is a communication chain handle. applications usually send requests to or respond to network requests through the "socket. Socket originated from Unix, and one of the basic philosophies of Unix/Linux is "Everything is a file". It is operated in open, read/write, and Close Modes for files, socket is an implementation of this mode on the network, that is, socket is a special file. The two programs use socket to interact with data through the "network". It is only responsible for two things: establish a connection and transmit data. At the same time, the socket follows the principle when sending and receiving data: when sending data, there will be receiving, sending and receiving must be equal!
Differences between file read/write and socket data sending and receiving
-The file module opens, reads, and closes a specified file.
-The socket module is used to open (CONNECT), read/write (send and receive), and close (disconnect) the socket between the server and the client)