Python Full Stack learning note (i) network Protocol of Network Foundation

Source: Internet
Author: User
Tags new set subdomain

Read Catalogue

    • I. Operating System Fundamentals
    • Two. Principles of network communication
    • 2.1 The essence of the Internet is a series of network protocols
    • 2.2 OSI layer Seven protocol
    • 2.3 TCP/IP five-layer model explained
    • 2.3.1 Physical Layer
    • 2.3.2 Data Link Layer
    • 2.3.3 Network Layer
    • 2.3.4 Transport Layer
    • 2.3.5 Application Layer
    • 2.3.6 socket
    • Three. Network communication implementation
    • Four. Network communication process
I. Operating System Fundamentals

Operating system: (Operating System, abbreviated OS) is a computer program that manages and controls computer hardware and software resources, is the most basic system software that runs directly on "bare metal", and any other software must be run with the support of the operating system.

Note: computer (hardware)->os-> application software

Two. Network communication Principle 2.1 The essence of the Internet is a series of network protocols

A hard-to-use operating system, and then install the software you can work normally, but you can only use it yourself

Like this, everyone has a machine of their own, but they are isolated from each other.

How can we play together

But what is the Internet?

In fact, the communication between the two computers and two people call the principle of communication is the same (China has a lot of regions, different regions have different dialects, for the whole Chinese can understand, everyone unified speaking Putonghua)

Mandarin is the standard of communication between people in China, and if it is a communication between people in two countries?

The problem is that you cannot ask a person/computer to master the language/standards of the world, so there is a unified communication standard in the world: English

Conclusion: English is the unified standard of communication for all people in the world, if the computer is regarded as distributed around the world, then the internet connecting two computers is actually

A uniform set of standards called Internet protocols, the essence of the Internet is a series of protocols, collectively known as ' Internet Protocol ' (Internet Protocol Suite).

Features of the Internet Protocol: A standard that defines how computers connect to the Internet, and computers that connect to the Internet.

2.2 OSI layer Seven protocol

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 floor

2.3 TCP/IP five-layer model explained

We will apply layer, presentation layer, session layer and as the application layer, from the point of view of TCP/IP layer five protocol to explain the origin and function of each layer, to understand the main protocol of each layer

Understand the whole Internet communication principle.

First, the user perceives only the top layer of application layer, top-down each layer is dependent on the next layer, so we start from the bottom layer, better understand

Each layer runs a specific protocol, the closer to the user, the lower the closer to the hardware

2.3.1 Physical Layer

Physical layer Origin: As mentioned above, the isolated computer to play together, you must be connected to the Internet, the implication is that the computer must complete the networking

Physical layer function: mainly based on the electrical characteristics of the transmission of high and low voltage (electrical signal), higher voltage corresponding to the number 1, low voltage corresponding to the number 0

2.3.2 Data Link Layer

Data Link Layer origin: pure electrical signals 0 and 1 do not make any sense, you must specify how many bits of electrical signals, each group what meaning

The function of the data Link layer: Defines how the electrical signals are grouped

Ethernet protocol:

In the early days each company had its own grouping, and later formed a unified standard, the Ethernet protocol Ethernet

Ethernet Regulations

    • A set of electrical signals constitutes a packet called ' frame '
    • Each data frame is divided into: header head and Data two sections
Head Data

Head contains: (fixed 18 bytes)

    • Sender/Source Address, 6 bytes
    • Recipient/Destination Address, 6 bytes
    • Data type, 6 bytes

Data contains: (minimum 46 bytes, maximum 1500 bytes)

    • Details of the packet

Head length +data length = Shortest 64 bytes, up to 1518 bytes, over maximum limit on shard send

MAC Address:

The source and destination addresses included in the head are: Ethernet specifies that devices that access the Internet must have a network card, and the address of the sending and receiving ends refers to the address of the network card, which is the MAC address

MAC Address: Each NIC is fired from the factory, the world's only MAC address, the length of 48-bit 2 binary, usually represented by 12-bit 16 binary number (the first six bits is the manufacturer number, the last six bits is the pipeline number)

Broadcasting:

With a MAC address, two hosts within the same network can communicate (one host obtains the MAC address of another host via the ARP protocol)

Ethernet uses the most primitive way, the broadcast way carries on the communication, namely the computer communication basic roar

2.3.3 Network Layer

Network layer origin: With Ethernet, MAC address, broadcast send way, the world's computers can communicate with each other, the problem is that the world-wide internet is

A small local area network that is isolated from each other, then if all the communication is using the Ethernet broadcast mode, then a machine sends the packet will be received all over the world,

It's not just inefficient, it's a disaster.

Conclusion: It is necessary to find a way to distinguish which computers belong to the same broadcast domain, which are not, if they are sent by broadcast, if not,

In the way of routing (distributing packets to different broadcast domains/subnets), MAC addresses are indistinguishable and are only relevant to vendors

Network layer function: Introduce a new set of address to distinguish different broadcast domain/subnet, this set of address is the network address

IP protocol:

    • The protocol that specifies the network address is called the IP protocol, the address it defines is called an IP address, and the widely used version of V4 is IPv4, which specifies that the network address is represented by 32-bit 2 binary
    • Range 0.0.0.0-255.255.255.255
    • An IP address is usually written as a four-segment decimal number, for example: 172.16.10.1

The IP address is divided into two parts

    • Network section: Identifying subnets
    • Host part: Identity host

Note: The simple IP address segment only identifies the type of IP address, from the network part or the host part can not identify the subnet in which an IP is located

Example: 172.16.10.1 and 172.16.10.2 are not sure that they are in the same subnet

Subnet mask

The so-called "subnet mask" is a parameter that represents the characteristics of a sub-network. It is formally equivalent to an IP address, is also a 32-bit binary number, its network portion is all 1, the host part is all 0. For example, IP address 172.16.10.1, if the network portion is known as the first 24 bits, the host part is the last 8 bits, then the subnet mask is 11111111.11111111.11111111.00000000, written in decimal is 255.255.255.0.

Knowing the "subnet mask", we can determine whether any two IP addresses are in the same sub-network. The method is to use the two IP address and the subnet mask for each and operation (two digits are 1, the result of the operation is 1, otherwise 0), and then compare the results are the same, if so, it indicates that they are in the same sub-network, otherwise it is not.

For example, the subnet masks for known IP addresses 172.16.10.1 and 172.16.10.2 are 255.255.255.0, are they on the same subnet? Both and the subnet mask, respectively, and operation,

172.16.10.1:10101100.00010000.00001010.000000001

255255.255.255.0:11111111.11111111.11111111.00000000

and operation network Address result: 10101100.00010000.00001010.000000001->172.16.10.0

172.16.10.2:10101100.00010000.00001010.000000010

255255.255.255.0:11111111.11111111.11111111.00000000

and operation network Address result: 10101100.00010000.00001010.000000001->172.16.10.0

The results are 172.16.10.0, so they are on the same subnet.

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 in the same subnet.

IP packets

IP packets are also divided into head and data sections, without having to define a separate field for the IP packet, directly into the data portion of the Ethernet packet

Head: 20 to 60 bytes in length

Data: The maximum is 65,515 bytes.

In the "Data" section of the Ethernet packet, the maximum is only 1500 bytes. Therefore, if the IP packet exceeds 1500 bytes, it needs to be split into several Ethernet packets, which are sent by sub-development.

Ethernet Header IP header IP data

ARP protocol

ARP protocol origin: Computer communication is basically roar, that is, the way of broadcasting, all the upper layer of the package to the end of the packet to be encapsulated on the Ethernet header, and then sent through the Ethernet protocol, when talking about the Ethernet protocol, we know that

Communication is based on the implementation of the MAC broadcast, the computer in the contract, the acquisition of its own Mac is easy, how to obtain the target host Mac, you need to pass the ARP protocol

ARP protocol feature: Send packets in a broadcast way, get the MAC address of the destination host

How the protocol works: Each host IP is known

Example: Host 172.16.10.10/24 access 172.16.10.11/24

One: First differentiate your subnet by IP address and subnet mask

Scene Packet Address
Same subnet Target host Mac, destination host IP
Different subnets Gateway Mac, Destination host IP

Two: Analysis 172.16.10.10/24 and 172.16.10.11/24 are in the same network (if not the same network, then the target IP in the following table is 172.16.10.1, the Mac that gets the gateway via ARP)

SOURCE Mac Target Mac Source IP Destination IP Data section
Send-side host Send-side Mac FF:FF:FF:FF:FF:FF 172.16.10.10/24 172.16.10.11/24 Data

Three: This package will be broadcast in the sending side of the network in the transmission, all hosts received after unpacking, found that the target IP for their own, on the response, back to their Mac

2.3.4 Transport Layer

The origin of the Transport layer: the network layer of IP to help us area molecular Network, Ethernet layer of the Mac to help us find the host, and then everyone is using the application, your computer may open at the same time QQ, Storm video, and many other applications,

Then we found a specific host through IP and Mac, how to identify the application on this host, the answer is the port, the port is the number of the application associated with the network card.

Transport Layer Capabilities: Establish Port-to-port communication

Supplemental: Port range 0-65535,0-1023 for system-occupied ports

TCP protocol:

Reliable transmission, TCP packets do not have a length limit, theoretically can be infinitely long, but in order to ensure the efficiency of the network, usually the length of TCP packets will not exceed the length of IP packets, to ensure that a single TCP packet no longer split.

Ethernet Header IP header TCP Header Data

UDP protocol:

Unreliable transfer,the "header" section is only 8 bytes, the total length of not more than 65,535 bytes, just put in an IP packet.

Ethernet Header IP header UDP header Data

TCP Messages

TCP three-time handshake and four-time wave

2.3.5 Application Layer

Application layer Origin: The user is using the application, all work in the application layer, the Internet is developed, everyone can develop their own applications, the data are diverse, it is necessary to specify the organization of good data

Application Layer functionality: Specifies the data format of the application.

Example: TCP protocol can be used for a variety of programs to pass data, such as email, WWW, FTP and so on. Then there must be different protocols for the format of e-mail, Web pages, FTP data, and these application protocols constitute the "Application layer".

2.3.6 socket

We know that two processes if need to communicate the most basic premise can be able to uniquely identify a process, in the local process communication we can use PID to uniquely identify a process, but the PID is only local only, the network of two process PID collision probability is very large, this time we need to separate its path, We know that the IP address can uniquely identify the host, and the TCP layer protocol and port number can uniquely identify a process for the host, so we can use the IP address + protocol + port number to uniquely identify a process in the network.

The ability to uniquely identify the processes in the network, they can use the socket to communicate, what is the socket it? We often translate the socket into sockets, the socket is an abstraction layer between the application layer and the transport layer, which abstracts the complex operations of the TCP/IP layer into a few simple interfaces to provide a layer to invoke the implemented process to communicate in the network.

Socket originated from UNIX, in Unix everything is file philosophy, socket is an "open-read/write-off" mode implementation, the server and the client maintain a "file", after establishing a connection open, you can write to their own files for the other side to read or read the contents of the other side, Closes the file at the end of the communication.

Three. Network communication implementation

To achieve network communication, each host needs to have four elements

    • The IP address of this machine
    • Subnet mask
    • IP address of the gateway
    • IP address of DNS

There are two ways of getting these four elements

1. Static access

That is, manual configuration

2. Dynamic acquisition

Get through DHCP

Ethernet Header IP header UDP header DHCP Packets

(1) The first "Ethernet header", set the MAC address of the sender (native) and the MAC address of the receiver (DHCP server). The former is the MAC address of the local network card, the latter do not know, fill in a broadcast address: FF-FF-FF-FF-FF-FF.

(2) Next "IP Header", set the IP address of the sender and the IP address of the receiver. At this time, for both, this machine is not known. The IP address of the issuing party is then set to 0.0.0.0, the IP address of the receiver is set to 255.255.255.255.

(3) The last "UDP header", set the port of the issuing party and the port of the receiver. This section is provided by the DHCP protocol, which is port 68 and the receiver is port 67.

Once this packet is constructed, it can be sent out. Ethernet is broadcast sent, and each computer on the same sub-network receives this packet. Because the receiver's MAC address is ff-ff-ff-ff-ff-ff, do not see who is sent to, so each received this package of the computer, you must also analyze the IP address of the package to determine whether it is sent to their own. When the sender IP address is 0.0.0.0 and the receiver is 255.255.255.255, the DHCP server knows "This package is sent to me" and the other computer can discard the package.

Next, the DHCP server reads out the contents of the packet, assigns the IP address, and sends back a "DHCP response" packet. The structure of this response packet is similar, the MAC address of the Ethernet header is the network card address of both sides, The IP address of the IP header is the IP address of the DHCP server (the issuing party) and the 255.255.255.255 (receiver), the UDP header port is 67 (sender) and 68 (receiver), the IP address assigned to the requester side and the specific parameters of the network are included in the data section.

The newly added computer receives this response packet, so it knows its own IP address, subnet mask, gateway address, DNS server and so on parameters

Four. Network communication process

1. Get the Machine

    • IP address of this machine: 192.168.1.100
    • Subnet Mask: 255.255.255.0
    • IP address of the gateway: 192.168.1.1
    • IP address of DNS: 8.8.8.8

2. Open the browser and want to access Google, in the address bar entered the URL: www.google.com.

3.dns protocol (based on UDP protocol)

13 Root DNS:

a.root-servers.net198.41.0.4 United States b.root-servers.net192.228.79.201 United States (also supported IPV6) c.root-servers.net192.33.4.12 France d.root-servers.net128.8.10.90 United States e.root-servers.net192.203.230.10 United States f.root-servers.net192.5.5.241 United States (another support IPV6) g.root-servers.net192.112.36.4 United States h.root-servers.net128.63.2.53 United States (another support IPV6) i.root-servers.net192.36.148.17 Sweden j.root-servers.net192.58.128.30 United States k.root-servers.net193.0.14.129 United Kingdom (another support IPV6) l.root-servers.net198.32.64.12 USA m.root-servers.net202.12.27.33 Japan (also supported by IPV6)

Domain Definition: http://jingyan.baidu.com/article/1974b289a649daf4b1f774cb.html

Top-level domain name: to. com,.net,.org,.cn etc. belong to the international top-level domain name, according to the current Internet domain Name System, the international top-level domain name divides into two categories: Category top-level domain name (GTLD) and geographical top-level domain name (ccTLD) two kinds. Category top-level domain name is the "COM", "NET", "ORG", "BIZ", "INFO" and so on the end of the domain name, all by foreign companies responsible for management. A geographic top-level domain name is a domain name that ends in a country or region code, such as "CN" on behalf of China and "UK" for the UK. Geographic top-level domains are generally managed by each country or region.

Level two domain name: Level Two domain name is based on the top-level domain name, the analogy of China's two-level domain has,. com.cn,.net.cn,.org.cn,.gd.cn, etc. subdomain is the subdomain of its parent domain, and the parent domain name is abc.com, Subdomains are www.abc.com or *.abc.com. In general, the level two domain name is a record of the domain name, such as alidiedie.com is a domain name, www.alidiedie.com is one of the more commonly used records, the general default is to use this, but similar *. Alidiedie.com's domain name is all called the level two of alidiedie.com.

The contents of the 4.HTTP section are similar to the following:

get/http/1.1 Host:www.google.com connection:keep-alive user-agent:mozilla/5.0 (Windows NT 6.1) ... accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 ACCEPT-ENCODING:GZIP,DEFLATE,SDCH accept-language:zh-cn,zh;q=0.8 accept-charset:gbk,utf-8;q=0.7,*;q=0.3 Cookies: ...

We assume that the length of this part is 4960 bytes and it will be embedded in the TCP packet.

5 TCP protocol

The TCP packet needs to set the port, the receiver (Google) HTTP port is 80 by default, and the sender (native) port is a randomly generated integer between 1024-65535, assuming 51775.

The header length of the TCP packet is 20 bytes, plus the packet embedded in HTTP, the total length becomes 4980 bytes.

6 IP protocol

Then, the TCP packet is then embedded in the IP packet. IP packets need to be set up on both sides of the IP address, which is known, the sender is 192.168.1.100 (native) and the receiver is 172.194.72.105 (Google).

The header length of the IP packet is 20 bytes, plus the embedded TCP packet, the total length becomes 5000 bytes.

7 Ethernet Protocol

Finally, the IP packet is embedded in the Ethernet packet. Ethernet packet needs to set the MAC address of both sides, the sender is the local network card MAC address, the receiver is the gateway 192.168.1.1 MAC address (through the ARP protocol).

The data portion of the Ethernet packet, the maximum length is 1500 bytes, and now the IP packet length is 5000 bytes. Therefore, IP packets must be split into four packets. Because each package has its own IP header (20 bytes), the length of the IP packets for the four packets is 1500, 1500, 1500, 560, respectively.

8 Server-side response

After the forwarding of multiple gateways, Google's server 172.194.72.105, received the four Ethernet packets.

According to the IP header number, Google put four packages together, take out the full TCP packet, and then read the inside of the "HTTP request", and then make "HTTP response", and then sent back with the TCP protocol.

After the native HTTP response is received, the Web page can be displayed to complete a network communication

Python Full Stack learning note (i) Network protocol for Network Foundation

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.