Tag: Enter Pycharm cal flow parameter receive return compression optimization
Python network programming has a lot of difficulties, but also easy to forget, recently I will be issued a system, complete pythonnet knowledge of the blog, while reviewing while sharing, interested can pay attention to me.
Talk not much, let's get started.
Network programming
Purpose: Transfer of data
ISO (International Organization for Standardization)
OSI seven-layer model-----> Standardized process for network communication
Application layer: Provision of User services, specific content specified by the program
Presentation layer: Provides encryption and compression optimizations for data
Session Layer: Make sure to set up the application link and select the Transport service
Transport layer: Provides data transfer services for flow control
Network layer: Route selection, Network interconnection
Link Layer: Provides link switching, sending of specific messages
Physical layer: Physical hardware, interfaces, network card provisions
Can be divided into four-layer models
Four-layer model
Application Layer
Transport Layer
Network layer
Physical link Layer
Divided into five-tier model (TCP/IP model)
Application layer (Application Layer presentation layer session layer)
Transport Layer
Network layer
Link Layer
Physical Layer
The advantages of the OSI model: separating functions, reducing coupling in network transmission, and making the development process clearer
Each part of its duties.
High aggregation: Each module functions as single as possible, does not have multiple function doping
Low coupling: Minimizing the correlation between each module
Network protocol: A requirement that all parties to the agreement must comply with in network communication. such as what connection to establish, how the message structure is parsed, etc.
Application layer: TFTP HTTP DNS SMTP
Transport Layer: TCP UDP
Network layer: IP
Physical Layer: IEEE
Network-related concepts
Network host: Determine a host on the network
Local use: "localhost" 127.0.0.1
Network address: "0.0.0.0" "172.60.50.54" (Native network IP)
View native IP address: ifconfig (Linux)
ipconfig (Windows)
Ipython3 into interactive mode
Obtaining a matching IP by computer name
Socket.gethostbyname ("localhost")
"127.0.0.1"
Get the computer name of the machine
Socket.gethostname ()
"XXXX"
IP Address
Determine the address of a host on the network
IPV4: Dotted decimal
ipv6:128 bit
Network Connectivity Test
Ping 172.60.50.182
Special IP
127.0.0.1 Local Test IP
0.0.0.0 automatic use of locally available NIC IP
192.168.1.0 represents the current network segment
192.168.1.1 is usually the gateway address
192.168.1.255 Broadcast Address
IP address converted to hexadecimal expression
Socket.inet_aton (' 192.168.1.2 ')
B ' \xc0\xa8\x01\x02 '
Domain: The name of the network server address on the network
Port number: Port number is part of the address, in a system each network application listens to different ports, to obtain the corresponding port to pass the information
Value range: 1----65535
1-255 some common ports (well-known program occupancy)
256--1023 System Port
1024--65535 personal port >10000
Transport Layer Services
Connection-oriented transport service---"TCP protocol
Transmission characteristics:
Reliable data transfer
Reliability: Data in transit, no out of sequence error-free no loss no duplication
Connection and disconnection required before and after data transfer
Three-time handshake: the process of establishing a connection in a connection-oriented transport service
1. The client sends a connection request to the server
2. The server accepts a request for confirmation and returns a confirmation message.
3. Client receives server reply final confirmation connection
Four waves: The process of disconnecting from a connection-oriented transport service station
1. The active party sends a message informing the passive party to disconnect
2. Passive response message, indicating that the request has been received, ready to disconnect
3. The passive party sends the message again, indicating that it is ready for processing and can disconnect
4. Unsolicited send acknowledgement message, disconnect
Application: Suitable for transmission of large content or files, network is good, need to ensure the transmission reliability of the situation
e.g. upload and download of chat information files, mail processing
Web Access
UDP protocol for non-connected transport service---"
Transmission characteristics:
No guarantee of transmission reliability
No connection and disconnection process
The first of the data is more free
Application: Network conditions may result in packet loss, low transmission reliability requirements
e.g.: Network video, group chat, broadcast, etc.
Socket Socket Programming
Purpose: The socket programming interface provided by programming language makes it easier to complete TCP and UDP-based programming
Sockets: A programmatic means of accomplishing these goals
Socket category
Streaming Sockets (SOCK_STREAM): Transport-Layer TCP-based protocol communication
Transport-Flow sockets for connection-reliable transport of TCP
Datagram Sockets (SOCK_DGRAM): Transport layer transmission based on UDP protocol
Transmit datagram sockets for non-connected unreliable transport UDP
Underlying sockets (Sock_ram): Accessing the underlying Protocol sockets
Service side of TCP
Import socket
1. Creating sockets
Socket.socket (sock_family = Af_inet,
Sock_type = Sock_stream,
Proto = 0)
Features: Creating sockets
Parameter: sock_family address family type af_inet IPv4 network communication
Sock_type Socket Type Sock_stream streaming
Sock_dgram Data Report
Proto typically 0 selected sub-protocol type
Return value: Returns the Socket object
2. Binding Address
Sockfd.bind (addr)
Function: Bind address
Parameters: Addr---"Tuple (ip,port) (' 0.0.0.0 ', 1234)
3. Setting up the listener socket
Sockfd.listen (N)
Function: Set socket to listen socket, create listening queue
Parameters: Listening Queue size
* One listener socket can connect multiple clients
4. Waiting for client connections to be accepted
CONNFD,ADDR = Sockfd.accept ()
Features: Blocking waits for processing client connections
Return value: CONNFD new socket for communicating with clients
Addr the address of the connected client (Ip,port)
* Blocking function: When the program runs to the blocking function position, if a condition is not reached then the program is suspended, knowing that the condition is reached to end blocking
5. Sending and receiving messages
data = CONNFD.RECV (buffersize)
Function: Accept Message
Parameter: The size byte of an accepted message at a time
Return value: Returns the accepted content
n = connfd.send (data)
Function: Send Message
Parameters: What to send (bytes format)
Return value: Returns the number of bytes actually sent
6. Close the socket
Sockfd.close ()
* Telnet IP Port
Client
1. Create socket (same as service end socket type)
2. Initiating a connection
Connect (addr)
Function: Initiate a connection to the server
Parameters: Service-side address tuple
3. Sending and receiving messages
4. Close the socket
Here's the code, which is the client code
This is the service-side code:
OK, this section ends, small partners can use Pycharm (Community edition free) run try, we see next time!
pythonnet-Network Programming (1)