★ Definition
Because the programs on different machines have to communicate, the network
◇C/S Architecture
Server service Side
Client clients
◇B/S Architecture
Broswer Browse Side
Server Client
such as: various small programs and public numbers
★ Communication ◇ Network cable
A bridge connecting each machine
◇ Network card
Each computer has a globally unique MAC address (physical address)
◇ARP protocol
Find the corresponding MAC address by IP address
◇ Switch
Problems with communication between multiple machines
◇ Gateway
A machine in a local area network wants to access a machine outside the LAN, requiring a gateway
◇ Network Segment
IP address and subnet mask bitwise AND operation
◇IP protocol
- 127.0.0.1 Local loopback Address
- Assigning an IP address to each computer
- Determine which addresses are in the same sub-network
◇ Communication Protocol TCP protocol
Full duplex, both sides can send and receive information
The process of establishing communication
1, establish connection: three-time handshake
2, data transfer: Long link
3, disconnect: Four waves
UDP protocol
is a simple transport protocol that only sends out data, but does not guarantee that they will reach their destination
◇ Port
On the computer, each network communication program, will open a port
Port range: 0-65535
In general we use: Port after 8000
The difference between IP and port
IP: Identify a single machine
Port: Determine the only one program
★ Five-layer Model ◇ Application layer
Agreement:
- HTTP protocol
- HTTPS protocol
- FTP Transport Protocol
- SMTP protocol
◇ Transport Layer
Select a communication protocol
◇ Network Layer
Add IP information to the previous layer of information
IP protocol
◇ Data Link Layer
Add a MAC address to the previous layer of information
ARP protocol
◇ Physical Layer
LAN network card
Transmit bit stream 0101010
★socket Module
As long as IP and port are identified, you can use the socket to communicate with it
◇ Type
- Based on file type: Af_unix
- Based on network type: AF_INEF
◇socket instancesTCP-based (sticky packets)
1 ImportSocket2 3SK = Socket.socket ()#instance Socket Object4Sk.bind (('127.0.0.1', 8080))#binding IP and Ports5Sk.listen ()#Monitor6 7conn, addr = Sk.accept ()#Establish a connection8Conn.send (b'Hello')#Send Message9res = CONN.RECV (1024x768). Decode ('Utf-8')#Receiving MessagesTen Print(RES) OneConn.close ()#Close Connection A -Sk.close ()#close the Socket objectserver.py
1 ImportSocket2 3SK = Socket.socket ()#creating a Socket object4Sk.connect (('127.0.0.1', 8080))#Create a connection5 6res = SK.RECV (1024x768). Decode ('Utf-8')#Receive Information7 Print(RES)8Sk.send (b'OK')#Send Message9Sk.close ()#Close Connectionclient.py
UDP-based (packet drops)
1 ImportSocket2 3SK = Socket.socket (type=socket. SOCK_DGRAM)#create a socket object based on the UDP protocol4Sk.bind (('127.0.0.1', 8080))#bind IP and Port5 6RET, addr = Sk.recvfrom (1024)#receive a message and receive a addr7 Print(Ret.decode ('Utf-8'))8Sk.sendto (b'Hello', addr)#sending a message needs to pass addr9 TenSk.close ()#close the Socket objectserver.py
1 ImportSocket2 3SK = Socket.socket (type=socket. SOCK_DGRAM)#create a socket object based on the UDP protocol4Addr = ('127.0.0.1', 8080)5 6Sk.sendto (b'Hi', addr)7RET,ADDR = Sk.recvfrom (1024)#receive a message and receive a addr8 Print(Ret.decode ('Utf-8'))#sending a message needs to pass addr9 TenSk.close ()#close the Socket objectclient.py
◇ Sticky Pack (only in TCP protocol)Cause: Connection-oriented stream transfer, data is borderless
1, buffering mechanism: Do not know the length of the data sent by the client, recv receive the data, will be cached in memory, waiting for the next recv
2, optimization algorithm: Continuous small packets will be combined to send, will be received by a recv
Solutions
Before sending large data, tell the receiver how much data I want to transmit, and receive the appropriate data size from the receiving end.
Through the struct module
Fixed-Length bytes
Pack: Is the bytes type that is about to convert a data into a fixed length
Unpack:unpack after the data is a Ganso
1 ImportSocket2 Importstruct3 4SK =Socket.socket ()5Sk.bind (('127.0.0.1', 8090))6 Sk.listen ()7 8conn, addr =sk.accept ()9 whileTrue:Tencmd = input ('>>>') One ifcmd = ='Q': A Break -Conn.send (Cmd.encode ('GBK'))#Send an order -num = CONN.RECV (4) thenum = Struct.unpack ('I', num) [0]#unpack after the data is a meta-ancestor -RET = CONN.RECV (num). Decode ('GBK')#Receive Information - Print(ret) - + - conn.close () +Sk.close ()server.py
1 ImportSocket2 Importsubprocess3 Importstruct4 5SK =Socket.socket ()6Sk.connect (('127.0.0.1', 8090))7 8 whileTrue:9cmd = SK.RECV (1024x768). Decode ('GBK')#receive a commandTen ifcmd = ='Q': One Break Ares = subprocess. Popen (cmd, shell=true, stdout=subprocess. PIPE, Stderr=subprocess. PIPE)#Execute the command locally -Stdout_ret = Res.stdout.read ()#GBK bytes -Stderr_ret = Res.stderr.read ()#GBK bytes thenum = Len (stdout_ret) +Len (Stderr_ret) -Sk.send (Struct.pack ('I', num))#Pass the packet size (the packet itself is 4 bytes) -Sk.send (Stdout_ret)#Send command results to the server -Sk.send (Stderr_ret)#Send command results to the server + -Sk.close ()client.py
Python-Network programming