python computer network

Socket is the basis of network communication, which is equivalent to establishing a pipeline between the sending end and the receiving end to realize the mutual transmission of data and commands

Fundamentals of computer network

Network architecture


Network protocol

A set of rules, standards, or conventions established for the exchange of data in a computer network
Three elements of network protocol: syntax, semantics and timing
Semantic: what to do
Explain the meaning of each part of the control information, specify what control information needs to be sent, and what actions need to be completed and what corresponding actions should be made
Grammar: how to do it
Structure and format of user data and control information
Timing: specify the sequence of events
Timing is a detailed description of the sequence of events

application layer

The application layer protocol directly interacts with the end user and defines how the application processes running on different terminal systems transmit messages to each other
DNS: domain name service, which is used to realize the conversion between domain name and ip address
FTP: file transfer protocol, which can transfer files between different platforms through the network
HTTP: Hypertext Transfer Protocol
SMTP: Simple Mail Transfer Protocol
TELNET: Remote Login Protocol

Transport layer

The transport layer mainly runs TCP and UDP protocols
TCP: connection oriented, reliable transmission protocol with quality assurance, but with large overhead. It is often used for file transmission, e-mail, etc
UDP: a connectionless protocol that transmits as much as possible with low overhead. It is commonly used in applications such as video on demand
UDP is only suitable for different occasions
In the transport layer, the port number is used to identify and distinguish specific application layer processes. Whenever an application layer network process is created, the system will automatically assign a port number to associate with it, which is an important basis for end-to-end communication on the network

IP address

IP runs in the network layer of the network architecture and is an important basis for network interconnection. IP address is used to identify the host on the network. On the public network or within the same LAN, each host must use a different IP address, Due to the wide application of network address translation and proxy server technology, the host IP addresses of different intranets can be the same and work normally without affecting each other
Socket: IP address and port number are used together to identify specific application processes on specific hosts on the network
MAC address: network card address or physical address, which is used to identify the physical addresses of different network cards

UDP programming

UDP is a connectionless protocol. In UDP programming, you do not need to establish a connection first, but directly send information to the receiver
There are three socket module methods commonly used by UDP
Socket: create a socket object
sednto: send the content specified by string to the address specified by address. Address is a tuple containing the host IP address of the receiver and the port number of the application process. The format is IP address and port number
recvfrom: receive data

"""
The sending end sends a string, and the receiving end listens on the local port 5000 and displays the received content. If the string is received, the listening ends
"""
import socket
# Use IPv4 protocol and udp protocol to transmit data_ INET:ipv4 AF_ INET6:ipv6 SOCK_ DGRAM:udp
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
# Bind port and port number. An empty string indicates any available ip address of the machine
s.bind(('',5000))
while True:
    data,addr = s.recvfrom(1024)
    # Display received content
    print(data.decode(),addr[1],addr[0])
s.close()

"""
Receiving end:
"""
import sys
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.sendto(sys.argv[1].encode(),('ip','port'))
s.close()

TCP programming

TCP is generally used where reliable data transmission is required

"""
use TCP Protocol communication needs to first establish a connection between the client and the server, and close the connection after the communication is completed to release resources
TCP The agreement can provide more than UDP The protocol has better service quality and the communication reliability has been essentially improved
connect: Connect to remote computer
sendall: send data
recv: Sister search data
bind: Binding address
listen: Listening port
accept: Request from corresponding client
"""
# Server
import socket
import sys

words = {
    'how are u':'fine',
    'how old are u':'nicai'
}
HOST=''
PORT = 50007
# SOCK_STREAM:TCP
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# Bind socket
s.bind((HOST,PORT))
# Start listening
s.listen(1)
print('port',PORT)
conn,addr = s.accept()
print('addr',addr)
while True:
    data = conn.recv(1024)
    data = data.decode()
    if not data:
        break
    print('data',data)
    conn.sendall(words.get(data,'no').encode())
conn.close()
s.close()

# client
HOST='' # Server host
PORT=50007 # Server port
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
try:
    s.connect((HOST,PORT)) # Connect server
except Exception as e:
    sys.exit()
while True:
    c = input('s')
    s.sendall(c.encode())
    data = s.recv(1024)
    data = data.decode()
    print('data',data)
    if c.lower() == 'bye':
        break
s.close()

reference material

https://blog.csdn.net/smilefxx/article/details/103148040

Keywords: Python network Network Protocol

Added by JustinM01 on Mon, 14 Feb 2022 18:57:27 +0200