Python network communication (i)

Source: Internet
Author: User

ISO (International Organization for Standardization)---"Network Architecture standard OSI model

OSI seven-layer model
    • Application layer: Provision of User services, specific content specified by the program
    • Presentation layer: Provides compression decompression and encryption of data, etc.
    • Session Layer: Establish a program-level connection to decide what kind of transport service to use
    • Transport layer: Provide appropriate communication services, flow control
    • Network layer: Route selection, network interconnection, packet transmission
    • Link Layer: Provides link switching, specific message sending
    • Physical layer: Physical hardware, interface

The OSI model benefits: Separate functions, reduce coupling in network transmission, each part to complete its own functions, development clearer.

High cohesion: Make the module function single, not mixed.
Low coupling: Minimizes inter-linkages and impact between modules

Four-layer, five-layer model four-layer model
    • Application Layer: Application Layer presentation layer Session layer
    • Transport Layer: Transport Layer
    • Network layer: Network layer
    • Physical link layer: Link Layer physical layer
Five-tier model (TCP/IP model)
    • Application Layer: Application Layer presentation layer Session layer
    • Transport Layer: Transport Layer
    • Network layer: Network layer
    • Link Layer: Link layer
    • Physical Layer: Physical layer
What is an agreement

Network protocol: Network communication, the parties must abide by the provisions, including the establishment of what connection, how to identify, message structure, etc.
Application layer: TFTP HTTP SMTP DNS
Transport Layer: TCP UDP
Network layer: IP

Network concept

Host: Name of the computer localhost

#获取主机名In [3]: socket.gethostname()Out[3]: 'tedu'
通过主机名解析IPIn [4]: socket.gethostbyname('tedu')Out[4]: '127.0.1.1'In [5]: socket.gethostbyname('localhost')Out[5]: '127.0.0.1'
Ip

ipv4:192.168.1.2
Points are decimal: Three points are divided into four parts, 0--255

Ipv6:ip address More

127.0.0.1: Local Test IP
0.0.0.0: LAN Available IP
192.168.1.0: Represents a network segment
192.168.1.1: Gateway
192.168.1.255: Broadcast Address
2---254

Ifconfig Ping

#通过主机名获取地址In [6]: socket.gethostbyaddr('www.baidu.com')Out[6]: ('127.0.0.1',  [], ['119.75.216.20'])          主机名      别名      IP将IP地址转换为二进制In [7]: socket.inet_aton('192.168.1.2')Out[7]: b'\xc0\xa8\x01\x02'将二进制转换为点分十进制In [8]: socket.inet_ntoa(b'\xc0\xa8\x01\x02')Out[8]: '192.168.1.2'

Inet_pton inet_ntop function As above, just the first parameter with
Socket.af_ient represents IPV4 socket.af_inet6--->ipv6

Port number

The port number is the part of the address that distinguishes the application-level program from one system
1--65535:1--255 well-known port---1023 system port

10000 6666 7777) 8888 9999

获取一个应用的端口In [11]: socket.getservbyname('mysql')Out[11]: 3306

Subnet mask: and IP mate filter segment
DNS: Domain name resolution

byte order
    • Big endian: High byte presence low address
    • Small-endian: low-byte presence low address

Network byte order: Unify the byte-order sending method

Transport Layer Service TCP protocol

Connection-oriented, is a reliable data transmission

Three-time handshake
    1. The client initiates a connection request to the server (asks if it can connect)
    2. The server receives a request to confirm (allow connection) to return the message
    3. Client receives permission to establish a connection
Wave four times
    1. The active party sends a message informing the passive party to disconnect
    2. The passive side returns the message, informs the request, prepares to disconnect
    3. The passive party sends the message to the active party to be ready to disconnect
    4. The active party sends the message to determine the disconnection

Reliable transmission: No disorder, no error, no loss, no duplication

Application: Suitable for the transmission of large content, good network conditions, need to provide accurate transmission of the situation
such as: Chat information, file transfer, mail

UDP protocol

A non-connected service is an unreliable data transmission that is carried out autonomously by the sending side, regardless of the receiving end

    • Application: Poor network, low transmission accuracy requirements, broadcast multicast
    • For example: Video conferencing, broadcast data
Socket programming

Sockets: A means of network communication socket

    • Streaming Sockets (SOCK_STREAM): Transport layer Communication based on TCP protocol
    • Datagram Sockets (SOCK_DGRAM): Transport layer communication based on UDP protocol
    • Original socket (SOCK_RAW): Access socket for underlying protocol
TCP Service Side

From socket Import *

1. Creating sockets
socket (socket_family = AF_INET,        socket_type = SOCK_STREAM,        proto = 0)功能 : 创建套接字参数 : socket_family:选择地址族种类 AF_INET (UNIX)        socket_type : 套接字类型                    SOCK_STREAM 流式                    SOCK_DGRAM 数据报        proto :子协议类型默认为0返回值 : 返回创建的套接字
2. Binding IP and port number
sockfd.bind()功能 : 绑定IP和端口参数 : 是一个元组 ,第一项是字符串形式的IP                     第二项是端口号                (‘192.168.1.2’,8888)
3. Let the socket have a listening function

S

ockfd.listen(n)功能 : 使套接字变为监听套接字,同时创建监听队列参数 : n 监听队列大小
4. Wait for the client to connect
sockfd.accept()功能 : 阻塞等待客户端连接返回值 : 第一个:返回一个新的套接字用来和客户端通信                    第二个:返回连接的客户端的地址
5. Sending and receiving messages
recv(buffer)功能 : 接收消息参数 : 一次接收消息的大小  字节返回值: 接收到的内容send(data)功能: 发送消息参数: 发送的内容  (byte)返回值 : 发送了多少个字节
    • When there is no receiving end, the send operation causes the broken pipe
6. Close the socket

Close ()
Function: Close socket

TCP Client Process
    • 1. Create a streaming socket
    • 2. Initiating a connection request
connect()功能:发起连接参数 : 一个元组  第一个元素 : 服务器的IP                  第二个元素 : 服务器的port
    • 3. Send and receive messages
    • 4. Close the socket

Python network communication (i)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.