Python Day30: Network Programming / OSI model

# # Network Programming 

`` `python 
network programming is to write Web-based applications of 
CS structure: 
    the client application client (access data terminal) 
    server applications: server (providing shared data terminal) 
learning network programming, the purpose is to write on C / S application configuration of 
two network communication conditions:
     1, the physical transmission media: twisted pair / optical fiber / coaxial / radio wave
     2 , the communication protocol 
communication protocol: the sender and recipient to comply with a common set of rules in order to be able to correctly parse the data 

of the OSI model: the computer industry's common language 
physical layer --- --- data link layer network layer transport layer ---- ---- ---- presentation layer session layer - - the application layer 
session layer, presentation layer, application layer, belong to the application layer. 

Physical Layer: various physical media 

`` ` 

# # of the OSI model data link layer 

` `` python 
data link layer: What are the contents of the electrical signal represents 0 and 1, and the grouping should contain a set of electrical signals. Ethernet link layer protocol works! 
    A set of electrical signals constitute a data packet, frame! 
    Each frame is divided into a header and data DATA head0 two part 
    head comprising a (18 bytes): 
    transmission source address Mac: 6 bytes 
    receiving a source address Mac: 6 bytes 
    of data types (tab + Ethernet type) 6-byte 
    data comprising: 46 is --1,500 bytes 
head length + the shortest length = 64 data bytes, up to 1518 bytes, exceeds the maximum limit transmitting to fragmentation 

ethernet predetermined internet access devices must have the card, the sender and receiver address is address card means, i.e. mac address 
mac address: a world are fired only at the factory on the mac each NIC address, length of 48 binary, generally indicated by 12 hexadecimal numbers (the first six are the vendor ID, the line number is six) 
with mac address, two hosts on the same network can communicate 
ethernet the most primitive way, way broadcast communication, basic communication by a roar computer that is 

`` 

# # network layer 

`` `python 
Ethernet communication problems: 
    the Internet worldwide is one of a small local area network isolated from each other composition, if all computers using Ethernet broadcast to find other computer, a machine to send packets over the world will receive, which is not only low efficiency Title, which would be a disaster, (broadcast storm is so generated) 

network layer functions: the introduction of a new set of addresses used to distinguish between different broadcast domain / subnet, this address is the network address, network address long in the end ? What, what is to distinguish between subnets 
# IP protocol 
IP protocol is working in the network layer protocol, full name: Internet protocol address, translated into Internet protocol addresses
#### 3.1 IP Address (key)
 
- ip address is called a protocol definition ip address, v4 widely used version i.e. ipv4, which specifies the network address is represented by 32-bit binary
 - range 0.0.0.0-255.255.255.255 
- a ip address is usually written as four decimal numbers, for example: 192.168.10.1 
- network number: identify the subnet
 - host number: identifies the host 

classification IP addresses: 
a category reserved for government agencies 1.0.0.0 --- 126.0.0.0 
medium-sized company category B assigned to 128.0.0.0 --- 191.255.0.0 
people assigned to any class C needed 192.168.0.1 - 192.168.255.254 
class D multicast for 
class E for experiments 
# What It is the subnet mask 
subnet mask is a 32-bit address, for shielding a part of the IP address in network identifier and host identifier distinguish and indicate that the IP address is on the LAN, or on a remote network. 
It is a part of all network, host-part 0. For example, IP addresses 172.16.10.1, if it is known before the network part 24, the main part 8, the subnet mask is 11111111.11111111.11111111.00000000 written as a decimal is 255.255.255 .0.
# Why do we need a subnet mask 
simple ip address only identifies the type ip address, ip which does not recognize a subnet of 
example: 192.168.10.1 and 192.168.10 .2 not determine them in the same subnet, because do not know which bits represent the network number and host number which represents the 
know "subnet Mask", we will be able to judge whether any two IP addresses in the same subnet. The two methods are the IP address and subnet mask were the AND operation (two digits are 1, the operation result is 1, and 0 otherwise), and compare the results are the same, and if so, to show that they are in the same sub network, otherwise it is not. 
# Through, forward subnet mask digits, to expand the size of the subnet. Subnet mask in routing settings! ! ! 
In summary, the role of IP protocol There are two, one is assigned an IP address for each computer, and the other is to determine which address in the same subnet. 

# The IP packet (Learn) 
ip packet is also divided into head and data portions are not required to define a single ip packet fields, directly into the data part of the Ethernet packet 
head: 20 to 60-byte length 
data: maximum It is 65,515 bytes 
and the Ethernet packets "data" section, up to only 1500 bytes. Therefore, if the IP packet exceeds 1500 bytes, it will need to be split into several Ethernet data frame transmitted separately. 

ARP protocol (understand)
The origin of ARP protocol:? IP is usually dynamic allocation is a logical address, and data transmission must rely on MAC address, how can you get the MAC address it by IP protocol which requires the ARP 
arp protocol functions: radio transmitted as packets, the destination hosts mac address
 . 1 .FF: the FF: the FF: the FF: the FF: the FF is a special MAC address in the switch will be seen that this address data is broadcast to all hosts within the network
 2.192 .168.1.102 the reply is received to the ARP request with its own MAC MAC source host
 3. the sender (192.168.1.101 after) receive a response, ip MAC address stored in the mapping relationship to the cache of the other side will, for the next use 
summary: after acquiring the MAC address ARP broadcast manner, the MAC address of the other gateway when not in the same subnet ARP obtained data reach the other gateway, and the gateway according to the corresponding IP host, of course, also the other gateway obtains host MAC by the ARP 
`` ` 

# # transporting layer 

` `` python 
origin of the transport layer ** : 
the physical layer link channel resumes 
the MAC data link layer, may be positioned to a host in a LAN, 
by the network layer IP address, subnet mask, can be targeted to a host in a LAN worldwide 

on a computer is only possible to run an application, such as micro-letters and qq while landing, and that the received data is in the end cross or to micro-channel qq it? 
the answer is: port number, the port is required networked applications associated with the card number 
transport layer functions: establish communication port to port
 ** ** supplement: port range 0-65535,0-1023 port is occupied by the system
 ** TCP ** and ** UDP ** work in the transport layer protocol:
 # TCP protocol: 
reliable transmission, TCP packet length limit is not, in theory, can be infinite, but in order to ensure the efficiency of the network , the length of the TCP packet usually will not exceed the length of the IP packet, to ensure that a single TCP packet re-segmentation is not necessary.
# Three-way handshake 
TCP reason reliable, because before transmitting data to confirm the three-way handshake to establish a link 
process three-way handshake is actually really confirm my hair you receive, you can receive my hair, so as to ensure data transmission the reliability 
issue is indeed to protect data transmission when the three-way handshake is reliable, then the data transfer handshake to how to ensure the success of it? 
# ** TCP protocol requires that after sending data, the other party must receive the reply message to confirm data successfully sent, if no reply is received within the time period of the information, it will automatically resend, if the number of retries too, said links may have been interrupted! ** 

# four waving 
purpose is to ensure that the fourth wave of data transmission between the two sides have been completed, the same is to ensure data integrity of 
its obvious advantages: to ensure that the data transmission is complete 
disadvantage: since every acknowledgment information to be transmitted, resulting in reduced transmission efficiency 
scenario: a plurality of scene data integrity must be guaranteed, such as text information, payment information, etc.! 

# UDP protocol 
 is not reliable transmission "header" section of only a total of eight bytes, the total length Not exceeding 65,535 bytes, exactly into an IP packet.
mode UDP and TCP protocol adopted completely different, it does not care about the other side No data is received, do not even care, the other side of the address is valid, simply send packets to the network, then to everything else!
 ** ** summary
Advantages: Since no transmission acknowledgment information, the transmission efficiency is higher than the TCP protocol 
disadvantages: an incomplete data transfer may 
scenarios: video, voice chat, data integrity is not required, but require a higher transmission speed 

` 

# # the application layer 

`` `python 
application layer Origin: users are using the application, are working at the application layer, the Internet is open, everyone can develop their own applications, what kind of data transmission format, you need a application developers to develop their own 
application layer functions: application of the provisions of the data format 





`` `

 

Guess you like

Origin www.cnblogs.com/huhongpeng/p/10960209.html