Python Network programming

Source: Internet
Author: User

Knowledge Content:

1. Network Fundamentals

2.socket Grammar and related

3.SocketServer for multiple concurrency

Reference:

Http://www.cnblogs.com/Eva-J/articles/8244551.html

Http://www.cnblogs.com/alex3714/articles/5227251.html

First, the Network Foundation

1.C/S Architecture and B/S architecture

(1) C/S Architecture: Client-server architecture

C/S is the Client/server architecture, which is the architecture model used in most of the current network programming. It can make full use of the advantages of hardware environment on both sides, and allocate the task reasonably to client side and server side, and reduce the communication overhead of the system. Client/server architecture generally has a background computer as a server, the user's personal computer software program as a client

Server: Service-side client: Clients

Typical applications: QQ, LOL

(2) b/S Architecture: Browser-server architecture

b/S is Browser/server architecture, is popular after the rise of the Web network architecture model, b/s architecture using the browser as a customer point application software, so b/s architecture can be regarded as a special case of C/s, is an improvement of the C/S architecture mode

Serve: Server Browser: Browser

Typical applications: All websites

(3) Advantages of b/S architecture

A. Easy to deploy, maintain and upgrade: b/S application is deployed on the background server, generally do not need to update the client software (ie browser)

B. Cross-platform, low client requirements: The client computer hardware environment varies widely, the traditional C/s architecture needs to develop different client software for different operating systems, and each operating system supports a Web browser, web-based development of B/ The S architecture system only needs to develop a set of client programs and the client program is deployed on the Web server, which is downloaded to the client when accessed by the browser (note: Client programs here generally refer to front-end code EG:HTML\CSS\JAVASCRIPT, etc.)

C. Very high security requirements: b/S architecture system is generally built on the WAN, that is, many people can access, facing the unknown users, so the development of B/s system should pay attention to the system's anti-attack, data encryption, backup, non-forgery and other capabilities

(4) The relationship between B/s architecture and C/s architecture

b/S Architecture in fact, C/s architecture, but the client has become browser,browser is actually a kind of clients

2.IP address, port, domain name and URL

(1) IP Address

Network address is a network layer concept, is the internet of Things host in the network has a logical address, computer networks using IP address to represent the network address. Now the IP address can be divided into IPv4 and IPv6 two kinds

IPv4: The address of a 32-bit binary number, generally expressed as 4 decimal digits, for example: 172.16.32.3, 10.38.96.243, etc.

A IPV4 address is divided into two parts: the network address and the host address. Depending on the network address and the different bits of the host address, the IP address can be divided into 3 classes:

A (8-bit network address and 24-bit host address) B (16-bit network address and 16-bit host address) C (24-bit network address and 8-bit host address), for some reason, Class A and class B addresses are almost completely depleted

IPv6: Due to the limit of the number of IPV4, the introduction of Ipv6,ipv6 is represented by a 1128-bit binary number, in the expression using 8 16 binary digits, such as: d23:4334:0:0:23:ade:9853:23

IPv4 and IPv6 comparison : Single from the order of magnitude, IPV6 has the address capacity is probably IPv4 8*10^28 times, reached 2^128. IPv6 not only solves the problem of the number of network address resources, but also makes it convenient to assign IP addresses to the Internet in addition to the computer devices. Of course, the current widely used is still IPv4, but there is no doubt that IPV6 will be the mainstream of future IP addresses.

Why IP address is required: To facilitate the search for other hosts, the IP address can be converted to a MAC address via the ARP protocol, and the MAC address is the unique identity of a host.

About the ARP protocol: Https://baike.baidu.com/item/ARP/609343?fr=aladdin&fromid=1742212&fromtitle=ARP%E5%8D%8F%E8%AE %ae

reserved IP address: the reserved IP address is not used in the Internet and is used primarily as a LAN address within the enterprise organization

The reserved addresses are mainly in the following four categories:

    • Class A: 10.0.0.0-10.255.255.255 (length equivalent to 1 Class A IP address)
    • Class A: 100.64.0.0-100.127.255.255
    • Class B: 172.16.0.0-172.31.255.255 (length equivalent to 16 consecutive Class B IP addresses)
    • Class C: 192.168.0.0-192.168.255.255 (length equivalent to 256 consecutive Class C IP addresses)

These addresses are not assigned by the Internet and are not routed through

Loopback address 127.0.0.1

(2) port

In the Web, the port is a software-level concept, is a virtual port, the IP address is the network layer addressing method, and the port is the transport layer of addressing mode. A port is a positive integer expressed as a 16-bit binary number, with a number ranging from 0 to 65535

Note: Only one program can occupy one port at a time, and it is not possible for two programs to occupy the same port, and in general we use the port after 8000

Common default port numbers:

IP Address and port:

    • Ip--> Determining a Host
    • Port-to-determine unique programs
    • ip+ Port---Identify only one program on a single machine

(3) Domain name

IP address is composed of pure numbers, it is difficult to remember, and can not express the function, geographical location and other information, so the standardization of the organization defines the domain name of this host address representation

Domain Name: Domain name is the application layer concept, is a string of points separated by the name of a computer or computer group name, domain name can be said to be a mask of IP address

Common domain names: www.baidu.com www.sina.com.cn, etc.

Note:

The symbols in the domain name are made up of English letters and numbers, each punctuation number is no more than 63 characters, and the uppercase and lowercase letters are not distinguished, and no other punctuation marks can be used except hyphens (-).

(4) Url

URL: Global Uniform Resource Locator is actually a link, behind the link is a resource, resources can be a picture, a song, a page 、、、

Standard form of URL: [Protocol]://[host]:[port]/[path]? [Parameters] Note: The General protocol is: HTTP, HTTPS, FTP and other ports are generally omitted

eg:https://www.nowcoder.com/1476481

Http://www.cnblogs.com/wyb666/p/8494861.html

3.OSI seven-layer model

The OSI seven-layer model is a logical division of Internet protocols from one division to another:

4.TCP/UDP protocol

(1) Tcp

TCP is a connection-oriented, reliable, byte-stream-based communication protocol for Transport layer

TCP Features: Ordering, authenticity, reliability, controllability, TCP uses a connection-oriented way to send and receive data, before sending and receiving data to establish a connection, release the connection after the transmission, the establishment of a connection using 3 times handshake to ensure the reliability of the transmission, controllability

After a successful handshake, a full-duplex communication will be established between the two applications, and this full-duplex communication will occupy the communication line between the two computers until it is closed by a party or closed by both parties.

Full duplex: Both parties can send messages to each other and receive messages

(2) Udp

UDP is a non-connected Transport layer protocol that provides an object-oriented, simple unreliable messaging service, although UDP is unreliable, but because of the overhead of UDP, UDP is generally suitable for applications where throughput is large (lightweight control) and can withstand information loss (unreliable transmission). And in the case of good network conditions, UDP packet loss rate in the actual situation is very small, so there are many classic protocols using UDP transmission, such as SNMP, NFS, DNS, etc.

5.HTTP protocol

(1) HTTP Introduction

HTTP is the Hypertext Transfer Protocol, is the most important Web application layer standard, B/s architecture of the application system with HTTP in the client to the server to transfer data. HTTP can transmit data in any format, such as text, images, or even video, which can be transmitted over HTTP.

(2) HTTP process

The HTTP port number defaults to 80, and when the server discovers that a client has established a connection and submits an HTTP request, it performs the appropriate action based on the requested content and returns the result to the client

Typically, the client enters a URL in the browser to initiate a network access by following these steps:

    • Enter a URL (URL), such as Access http://mysite:8080/app/index.html
    • The browser uses the domain Name System to query the mysite corresponding real IP, such as 212.34.98.20
    • Send a TCP connection request and establish a connection to the server 212.34.98.20 8080 port (HTTP port)
    • The content of the HTTP request includes the address of the access (/app/index.html), access Method (get), product name of the browser itself, and other information
    • The server returns the data in the corresponding address (/app/index.html) as response to the client, and if the request is not a file, the server needs to execute the corresponding code, dynamically generated and returned to the client
    • The browser closes the TCP connection to the server after accepting the result
    • The browser interprets the results (HTML, CSS, JavaScript) that are accepted and renders them on the display at last

6.socket

(1) Socket base

The two programs on the network realize the exchange of data through a two-way communication connection, one end of this connection is called a socket.

Establish a network communication connection with at least a pair of port numbers (sockets). Socket is the nature of the programming Interface (API), TCP/IP encapsulation, TCP/IP also to provide the interface for programmers to do network development, this is the socket programming interface; HTTP is a sedan that provides a specific form of encapsulation or display data; the socket is the engine, Provides the ability to communicate on the network

(2) Socket connection process

Server listener--server socket does not locate the specific client socket, but is in the state of waiting for the link, real-time monitoring network status

Client request, client socket to make a connection request, to connect to the target is the server socket, for this reason, the client socket must first describe its connection to the server socket, indicating the address and port number of the server socket, Then I want the server socket to make a connection request.

Connection confirmation-When the server socket is heard or received a connection request from the client socket, it responds to the client socket request, establishes a new thread, sends a description of the server socket to the client, once the client confirms, the connection is good. Service-side socket base is in listening state

(3) Socket two modes

There are two ways to operate a socket: connection-oriented and non-connected. A connection-oriented socket operation is like a phone that must establish a connection and a call, and all things arrive in the same order as they do, without a connected socket thinking of a mail delivery, with little assurance that the order is likely to be different.

The specific use of what mode to look at the scene, if reliability is more important, with a better connection-oriented.

A non-connected operation uses datagram Protocol (UDP), a socket in this mode does not need to connect to a socket, but simply reveals the datagram, the operation without connection is efficient, but not secure.

Connection-oriented operations use the TCP protocol. Low efficiency, but high security

(4) Socket Layer

(5) Understanding 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, thesocket is actually a façade mode, it is the complex TCP/IP protocol family hidden behind the socket interface, for the user, a simple set of interfaces is all, let the socket to organize data to meet the specified protocol

Second, socket syntax and related

Third, Socketserver realize multiple concurrency 

Python Network programming

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.