python web Development Network Programming TCP/IP UDP protocol

Keywords: Web Development socket udp TCP/IP

learning from python web development from introduction to mastery

1. TCP/IP protocol

Everyone can talk only with the same protocol

TCP/IP protocol:

  • Application layer: FTP file transfer, Telnet remote login, DNS domain name system, SMTP e-mail transmission... (providing services for users)
  • Transport layer: TCP transmission control, UDP user datagram (end-to-end communication to ensure sequential data transmission and integrity)
  • Network layer: IP internet protocol, IGMP Internet group management, ICMP Internet control message (host to host communication)
  • Link layer: (monitor the exchange of data between the host and the network)

1.1 IP protocol

  • The data is divided into small packages and sent through IP packets. There is no guarantee of arrival and sequence

1.2 TCP protocol

  • Based on the IP protocol, three handshakes are used to establish a reliable connection to ensure the sequential arrival of data
  • Lost, automatic retransmission
  • TCP message includes data, source IP, destination IP, source port and destination port

2. UDP protocol

  • For connectionless protocols, there is no need to establish a connection, just know each other's IP and port
  • It is not guaranteed to arrive, but it is faster than TCP

3. Socket

  • Both programs need to use Socket to communicate with the network
  • Used to describe IP addresses and ports
  • The service opens a Socket and binds it to a port. Different ports correspond to different services

Sockets in python:

s = socket.socket(AddressFamily, Type)
  • AddressFamily, fill in socket.AF_INET (for Internet interprocess communication), fill in socket.AF_UNIX (for interprocess communication on the same machine)
  • Type socket type, socket.SOCK_STREAM stream socket (mainly used for TCP), socket.SOCK_DGRAM packet socket (mainly used for UDP)

Common functions: https://www.runoob.com/python/python-socket.html

functiondescribe
Server side socket
s.bind()Bind the address (host,port) to the socket in AF_ Under INET, the address is represented in the form of tuple (host,port).
s.listen()Start TCP listening. backlog specifies the maximum number of connections that the operating system can suspend before rejecting connections. This value is at least 1, and most applications can set it to 5.
s.accept()Passively accept TCP client connections (blocking) and wait for the connection to arrive
Client socket
s.connect()Actively initialize TCP server connection,. Generally, the format of address is tuple (hostname,port). If there is a connection error, the socket.error error error is returned.
s.connect_ex()The extended version of the connect() function. When an error occurs, it returns an error code instead of throwing an exception
Public purpose socket function
s.recv()Receive TCP data. The data is returned as a string. bufsize specifies the maximum amount of data to be received. flag provides additional information about the message, which can usually be ignored.
s.send()Send TCP data and send the data in string to the connected socket. The return value is the number of bytes to send, which may be less than the byte size of string.
s.sendall()Send TCP data completely. Sends the data in the string to the connected socket, but attempts to send all the data before returning. None is returned successfully, and an exception is thrown if it fails.
s.recvfrom()Receive UDP data, similar to recv(), but the return value is (data,address). Where data is the string containing the received data, and address is the socket address where the data is sent.
s.sendto()Send UDP data and send the data to the socket. Address is a tuple in the form of (ipaddr, port) and specifies the remote address. The return value is the number of bytes sent.
s.close()Close socket
s.getpeername()Returns the remote address of the connection socket. The return value is usually a tuple (ipaddr,port).
s.getsockname()Returns the socket's own address. Usually a tuple (ipaddr,port)
s.setsockopt(level,optname,value)Sets the value of the given socket option.
s.getsockopt(level,optname[.buflen])Returns the value of the socket option.
s.settimeout(timeout)Set the timeout of socket operation. Timeout is a floating-point number in seconds. A value of None indicates that there is no timeout. In general, timeout periods should be set when a socket is first created because they may be used for connection operations (such as connect())
s.gettimeout()Returns the value of the current timeout period in seconds. If the timeout period is not set, it returns None.
s.fileno()Returns the file descriptor of the socket.
s.setblocking(flag)If the flag is 0, set the socket to non blocking mode, otherwise set the socket to blocking mode (default). In the non blocking mode, if the call recv() does not find any data, or the call send() cannot send data immediately, the socket.error exception will be caused.
s.makefile()Create a file associated with the socket

4. TCP programming

The client initiates the connection actively
The passive response is connected to the server:

4.1 create TCP server

  1. Create socket
  2. Bind bind bind IP and port
  3. listen socket can be connected passively
  4. accept waits for the client to connect
  5. recv / send receive send data

Example: use the socket module to send a request to the local server (127.0.0.1) through the client browser; The server receives the request and sends hello world to the browser

import socket

host = "127.0.0.1"  # IP
port = 8080  # port
web = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
web.bind((host, port))  # Binding port
web.listen(5)  # Listening, up to 5 connections
print("The server started successfully, Waiting for client connection...")
while True:
    conn, addr = web.accept()  # Establish client connection
    print("Client connection succeeded, address:", addr)
    data = conn.recv(1024)  # Get the data sent by the client
    print("Received data sent by client:", data.decode())
    conn.sendall(b'HTTP/1.1 200 OK\r\n\r\nHello World, Michael!')  # Send data to client
    conn.close()  # Close connection
    print("Client connection closed")

The server started successfully, Waiting for client connection...
Client connection succeeded, address: ('127.0.0.1', 7631)
Received data sent by client: GET / HTTP/1.1
Host: 127.0.0.1:8080
Connection: keep-alive
sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="96", "Microsoft Edge";v="96"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36 Edg/96.0.1054.34
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6


Client connection closed
 Client connection succeeded, address: ('127.0.0.1 ', 7632)
Received data sent by the client: GET /favicon.ico HTTP/1.1
Host: 127.0.0.1:8080
Connection: keep-alive
sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="96", "Microsoft Edge";v="96"
sec-ch-ua-mobile: ?0
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36 Edg/96.0.1054.34
sec-ch-ua-platform: "Windows"
Accept: image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: no-cors
Sec-Fetch-Dest: image
Referer: http://127.0.0.1:8080/
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6


Client connection closed

4.2 create TCP client

The client is simpler

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = "127.0.0.1"
port = 8080
s.connect((host, port))
while True:
    send_data = input("Please enter the data to send:")
    if send_data == "exit":
        break
    s.send(send_data.encode("utf-8"))
    recvData = s.recv(1024).decode("utf-8")  # Maximum receive 1024 bytes
    print("Received data:", recvData)
s.close()

4.3 simple chat tools

Server

import socket

host = socket.gethostname()
port = 12345
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
s.listen(1)
sock, addr = s.accept()
print('Establish connection:', addr)
info = sock.recv(1024).decode()
while info != "byebye":
    if info:
        print("Receive the message:", info)
    send_data = input("Please enter the information to send:")
    sock.send(send_data.encode())
    if send_data == "byebye":
        break
    info = sock.recv(1024).decode()
sock.close()
s.close()

client

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
port = 12345
s.connect((host, port))
print("Connected to server")
info = ''
while info != 'byebye':
    send_data = input("Please enter the data to send:")
    s.send(send_data.encode())
    if send_data == 'byebye':
        break
    info = s.recv(1024).decode()
    print("Data received from server:", info)
s.close()

Posted by 1bigbear on Tue, 30 Nov 2021 04:43:23 -0800