程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python (18): network programming (layer 7 protocol +tcp/udp+socket programming)

編輯:Python

Python As the most popular language , A technology that each of us should learn !
This article is for beginners , I will tell you how to get started in the easiest way python! 

One 、 Basic network concepts

1. How to connect computers

In the early :

Ethernet : LAN and switch

2.ip Address and ip agreement

  • ip agreement : The protocol that defines the network address is ip agreement , It defines the address as ip Address , Widely adopted v4 The version is ipv4, It specifies that the network address is 32 position 2 Hexadecimal said
  • Range : 0.0.0.0-255.255.255.255
  • One ip The address is usually written in four decimal numbers , example :172.16.10.1

3.mac Address

  • head The source and destination addresses contained in are from :ethernet Provision for access internet All devices must have network cards , The address of sender and receiver refers to the address of network card , namely mac Address .
  • mac Address : Each network card is burned to be the only one in the world when it leaves the factory mac Address , The length is 48 position 2 Base number , Usually by 12 position 16 A decimal number means ( The first six digits are the manufacturer's number , The last six digits are the assembly line number )

4. Router

  • Router (Router), It's connecting to the local area networks on the Internet 、 Wan devices , It will automatically select and set the route according to the channel , With the best path , Send signals back and forth .  Routers are the hub of the Internet ," Traffic police ". At present, routers have been widely used in all walks of life , Various products of different grades have become the realization of various backbone network internal connection 、 The main force of interconnection between backbone networks and between backbone networks and Internet . The main difference between routing and switches is that switches happen in OSI Refer to the second level of the model ( Data link layer ), And routing happens in the third layer , Network layer . This difference determines that routing and switch need to use different control information in the process of mobile information , So the way they realize their functions is different .
  • Router (Router) Also called gateway device (Gateway) It's used to connect multiple logically separate networks , The so-called logical network represents a single network or a subnet . When data is transferred from one subnet to another , It can be done through the router's routing function . therefore , The router has the ability to determine the network address and select IP The function of the path , It can be used in a multi network environment , Build flexible connections , Different data grouping and media access methods can be used to connect various subnets , Routers only accept information from the origin or other routers , An Internet device belonging to the network layer .

5. LAN

LAN (Local Area Network,LAN) It refers to a group of computers interconnected by multiple computers in a certain area . It's usually within a few thousand meters . LAN can realize file management 、 Application software sharing 、 Printer sharing 、 Schedule within the working group 、 E-mail and fax communication services . LAN is closed , It can be made up of two computers in the office , It can also be made up of thousands of computers in a company .

6. Subnet mask

  • So-called ” Subnet mask ”, It's a parameter that represents the characteristics of a subnet . It is formally equivalent to IP Address , Also a 32 Bit binary number , Its network part is 1, The main part is 0. such as ,IP Address 172.16.10.1, If the network part is known to be pre 24 position , The main part is the rear 8 position , So the subnet mask is 11111111.11111111.11111111.00000000, It's written in decimal 255.255.255.0.
  • know ” Subnet mask ”, We can judge , Any two IP Is the address on the same subnet . The way is to put two IP Address and subnet mask respectively AND operation ( Both digits are 1, The result of operation is 1, Otherwise 0), Then compare the results to see if they are the same , If so , It means they are in the same subnet , Otherwise it's not . 

IP There are two main functions of the agreement , One is to assign... To each computer IP Address , The other is to determine which addresses are in the same subnet .

Two 、 Software development architecture

What we know about the application of communication between two programs can be roughly divided into two kinds :

  • Application class :qq、 WeChat 、 Network disk 、 Youku is a desktop application that needs to be installed
  • web class : For example, Baidu. 、 You know 、 Blog Park and other applications that can be accessed directly by browser

The essence of these applications is actually the communication between two programs . These two categories correspond to two software development architectures ~

1.C/S framework

C/S framework :Client And Server , Chinese meaning : Client and server architecture , This kind of architecture is also from the user level ( It can also be physical ) To divide .

The client here generally refers to the client application EXE, The program needs to be installed before , To run on the user's computer , It depends on the user's computer operating system environment .

2.B/S framework

B/S framework :Browser And Server, Chinese meaning : Browser and server architecture , This architecture is divided at the user level .

Browser browser , In fact, it's also a kind of Client client , Just this client doesn't need you to install any applications , Just go through HTTP Request server-side related resources ( Web resources ), client Browser The browser can add, delete, modify and check .

3、 ... and 、osi Seven layer model

The core of networking is composed of a bunch of protocols , Protocol is the standard , For example, the standard of people's communication all over the world is English , If we compare computers to people , Internet protocol is the English language of computer industry . All computers have learned the internet protocol , Then all computers can send and receive information according to the unified standard to complete the communication .

According to the different division of labor, people divide the internet protocol into different levels logically :

Common protocols at each layer :

  • application layer :HTTP,FTP,DNS,TFTP…
  • The presentation layer : Data encryption
  • The session layer :SQL,RPC
  • Transport layer :TCP,UDP
  • The network layer :IP,IPX
  • Data link layer :ATM
  • The physical layer : circuit

Four 、socket layer

Socket also called " Socket ", Applications usually go through " Socket " Send a request to the network or answer a network request , To enable communication between hosts or processes on a computer . be based on tcp.

5、 ... and 、TCP And UDP agreement

TCP(Transmission Control Protocol) reliable 、 Connection oriented protocol (eg: Make a phone call )、 Low transmission efficiency full duplex communication ( Send cache & Receive cache )、 Byte stream oriented . Use TCP Application :Web browser ; E-mail 、 File transfer program .

UDP(User Datagram Protocol) unreliable 、 Connectionless service , High transmission efficiency ( Little delay before sending ), one-on-one 、 One to many 、 For one more 、 Many to many 、 Message oriented , Do your best to serve , No congestion control . Use UDP Application : The domain name system  (DNS); Video streaming ;IP voice (VoIP)

6、 ... and 、http working process

http It's a simple request - Response protocol , It usually runs on TCP above .

  1. The user sends a url request ,DNS Will resolve the corresponding content of the address
  2. Encapsulate content http Request packet
  3. And then it's packaged as TCP package , establish TCP Connect (TCP Three handshakes of )
  4. The client sends the request , Server receives request , Send a response
  5. The client received a response , Render the page
  6. Server down tcp Connect (TCP Four waves )

1. Three handshakes

2. Four waves

7、 ... and 、socket Programming simple example

Server side

import socket
# establish socket object
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Get the local host name
host = socket.gethostname()
port = 9999
# Binding port number
serversocket.bind((host, port))
# Set the maximum number of connections , Queue up after passing
serversocket.listen(5)
while True:
# Establish client connection
clientsocket, addr = serversocket.accept()
print(" Connection address :{}".format(addr))
msg = ' Chicken, you are so beautiful '
clientsocket.send(msg.encode('utf-8'))
clientsocket.close()

  client

import socket
# establish socket object
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Get the local host name
host = socket.gethostname()
# Set the port number
port = 9999
# Connection service , Specify the host and port
s.connect((host, port))
# Receive less than 1024 Bytes of data
msg = s.recv(1024)
s.close()
print(msg.decode('utf-8'))

  function

 

 

 


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved