Python---network programming

Source: Internet
Author: User
Tags ack

I. Architecture of software development

1:C/S Architecture

Client and server side, the client here generally refers to the client application EXE, the program needs to be installed before running on the user's computer, the user's computer operating system environment dependent on the larger.

2:B/S Architecture

Browser with the server browser side.

Browser browser, in fact, is also a kind of client clients, but this client does not need to install any application, just on the browser through the HTTP request server-side related resources.

Second, the Network Foundation

IP address: Refers to the Internet Protocol address.

IP address is a unified address format provided by IP Association, which allocates a logical address for each network on the Internet and each host to block differences in physical addresses.

The IP address is a 32-bit binary number that is typically divided into 4 ' 8-bit binary numbers '.

IP addresses are usually in the form of dotted decimal notation (a,b,c,d), where a,b,c,d are all decimal integers between 0~255.

The port can be considered as an outlet for communication between the device and the outside world.

So the IP address can be accurate to a specific computer, and the port is accurate to the specific program.

With a subnet mask, we can tell if any two IP addresses are in the same subnet. The method is to use the two IP address and the subnet mask to perform an and operation (two digits are 1, the result 1, the other 0), and then compare the results are the same, if so, it means that they are in the same sub-network, otherwise it is not.

To summarize, the IP protocol has two main functions: one is to assign an IP address to each computer, and the other is to determine which addresses are on the same subnet.

TCP protocol

TCP---Transmission Control Protocol, providing a connection-oriented, reliable byte-stream service. Before customers can exchange data with the server, a TCP connection must be established between the two parties before the data is transferred. TCP provides time-out retransmission, discard duplicate data, verify data, flow control and other functions to ensure that data can be transferred from one end to the other.

Reliable, connection-oriented protocol (eg: call), low transmission efficiency full duplex communication (send cache & receive cache), byte stream oriented. Applications that use TCP: Web browsers, e-mail, file transfer programs.

TCP is the Transport layer protocol in the Internet, using three-time handshake protocols to establish a connection. When the active party sends a SYN connection request, wait for the other person to answer syn+ack[1], and eventually perform an ACK acknowledgment on the other's syn. This method of establishing a connection prevents the wrong connection from being generated. [1] The process of the TCP three handshake is as follows: The client sends a SYN (SEQ=x) message to the server side and enters the Syn_send state. The server receives a SYN message, responds to a SYN (SEQ=y) ACK (ack=x+1) message, and enters the SYN_RECV state. The client receives the server-side SYN message, responds to an ACK (ACK=y+1) message, and enters the established state. Three times the handshake is complete, the TCP client and server side successfully establish the connection and can begin transmitting data. 
three-time handshake for TCP
To establish a connection requires three handshake, while terminating a connection to go through four handshake, which is partially closed by TCP (half-close) caused by. (1an application process first calls close, which is said to perform an active close. TCP on that side then sends a FIN section indicating that the data has been sent. (2) receives this fin's peer to perform a "passive shutdown" (passive close), which is confirmed by TCP. Note: Fin's receive also acts as a file terminator (end-of-file) is passed to the receive-side application process and placed after any other data that has been queued for the application process, because the receipt of fin means that the receiving application process has no additional data to receive on the corresponding connection. (3After a period of time, the application process that receives this file terminator will call close to close its socket. This causes its TCP to also send a fin. (4) Acknowledge this fin by receiving the original sender TCP of the final fin (that is, the end of the active shutdown). [1since each direction requires a fin and an ACK, it usually requires 4 sub-sections. Note: (1) "Usually" means that, in some cases, the fin of step 1 is sent along with the data, and the sections sent by step 2 and step 3 are from the end of the passive shutdown, and may be merged into a sub-section. [2] (2) between step 2 and step 3, it is possible to flow data from one end of the passive shutdown to the execution of the active shutdown, which is known as "semi-close" (half-close). (3when a UNIX process terminates either voluntarily (calling exit or returning from the main function) or involuntarily (receiving a signal to terminate the process), all open descriptors are closed, which also causes a fin to be emitted on any TCP connection that is still open. Either the client or the server can perform an active shutdown on either side. Typically, the customer performs an active shutdown, but some protocols, such as the HTTP/1.0 is actively shut down by the server execution. [2]
four waves of TCP

UDP protocol

UDP---User Datagram Protocol, is a simple transport layer protocol for datagram. UDP does not provide reliability, it simply sends the application to the IP layer's datagram, but does not guarantee that it will reach its destination. Because UDP does not have to establish a connection between the client and the server before transmitting the datagram, and there is no mechanism such as time-out retransmission, the transmission speed is very fast.

Unreliable, non-connected services, high transmission efficiency (before sending are chosen adequately), a pair of one or one-to-many, many-to-one, many-to-many, message-oriented, to do their best to serve, no congestion control. Applications that use UDP: Domain Name System (DNS), video streaming, voice over IP (VoIP).

Internet protocols are divided into OSI layer seven or TCP/IP layer five or TCP/IP four, depending on functionality

Run common physical devices on each tier

Transport Layer--four-layer switch, four-tier router

Network Layer--routers, layer three switches

Data Link layer--Network bridge, Ethernet switch, network card

Physical Layer--repeater, hub, twisted pair

Run common protocols on each tier

Application Layer--...

Transport Layer-->TCP and UDP protocol

Network Layer-->IP Protocol

Data Link Layer-->arp protocol

Physical Layer--...

Third, socket

A socket is an intermediate software abstraction layer that the application layer communicates with the TCP/IP protocol family, which is a set of interfaces. In design mode, the socket is actually a façade mode, it is the complex TCP/IP protocol family hidden behind the socket interface, for the user, a set of simple interface is all, let the socket to organize data to meet the specified protocol.

Socket family based on file type

Socket family name: Af_unix

Unix all files, file-based sockets are called by the underlying file system to fetch data, two sockets process running on the same machine, you can access the same file system to complete the communication indirectly

Socket family based on network type

Socket family name: af_inet

(There are also af_inet6 used for IPv6 and some other address families, but they are either used only on a platform, or have been discarded, or are rarely used, or are not implemented at all, and Af_inet is the most widely used one in all address families, Python supports a variety of address families, but since we only care about network programming, most of the time I use af_inet only)

Socket based on TCP protocol

TCP is link-based, you must start the server, and then start the client to link the server

ImportSocketsk=socket.socket () Sk.bind ('127.0.0.1', 8898))#bind an address to a socketSk.listen ()#Monitoring LinksCONN,ADDR = Sk.accept ()#Accept Client LinksRET = CONN.RECV (1024)#Receiving client InformationPrint(ret)#Print Client InformationConn.send (b'Hi')#send a message to the clientConn.close ()#close the client socketSk.close ()#to turn off server sockets (optional)
Service Side
Import= Socket.socket ()           #  creates a client socket sk.connect ('127.0.0.1 ', 8898)    #  try to connect to server sk.send (b'hello! '  = sk.recv (1024x768)         #  Dialog (send/Receive)print(ret) sk.close ()             #  Close Customer sockets
Client

 

Python---network programming

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.