I. Wedge
Now that you've learned to write Python code, if you've written two Python files a.py and b.py and run them separately, you'll find that the two Python files work well. But what do you do if you want to pass a data between the two programs?
This problem can be solved with your knowledge now, we can create a file, write the content that a.py want to pass to the file, and then b.py to read the content from this file.
But what do you do when your a.py and b.py are on separate computers?
Similar mechanisms have computer networks, QQ and so on. We can chat with others on our computer and can upload and download content to the network disk on our own computer. These are two programs in communication.
two. Architecture of software development
We understand that applications involving two communication between programs can be broadly divided into two types:
The first is the application class: QQ,, network disk, Youku This category is a need to install desktop applications
The second is the Web class: for example, Baidu, know, blog park, etc. using browser access can be directly used by the application
The essence of these applications is actually the communication between two programs. And these two classifications correspond to two software development architectures ~
1.C/S Architecture
C/S is: Client and server, Chinese meaning: Clients and servers-side architecture, this architecture is also from the user level (can also be the physical level) to divide.
The client here generally refers to the client application EXE, the program needs to be installed before it can be run on the user's computer, the user's computer operating system environment dependent on the larger.
2.B/S Architecture
b/S is: Browser and server, Chinese meaning: browser-side vs. servers-side architecture, this architecture is divided from the user level.
Browser browser, in fact, is also a kind of client clients, but this client does not need to install what application, just in the browser through the HTTP request server-side related resources (Web resources), the client browser browser can be used for pruning and checking.
Back to top three. Network Basics
Network Fundamentals
1. How does a program find another program on the network?
First of all, the program must be started, and secondly, must have the address of this machine, we all know that our people's address is probably the state \ province \ city \ district \ Street \ Floor number such words. So each networked machine also has its own address on the network, how is its address represented?
is represented by a string of numbers, for example: 100.4.5.6
IP address refers to the Internet Protocol address (English: Internet Protocol address, and also translated as Internet Protocol addresses), is the abbreviation of IP address. IP address is a unified address format provided by the IP protocol, 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, usually divided into 4 "8-bit binary" (that is, 4 bytes). IP addresses are usually expressed as "dotted decimal" (a.b.c.d), where a,b,c,d aredecimal integers between 0 ~255. Example: Point 10-in IP address (100.4.5.6), which is actually a 32-bit binary number (01100100.00000100.00000101.00000110).
What is an IP address?
" Port " is the translation of the English port, it can be regarded as the export of communication between equipment and the outside world."
What is a port?
So the IP address is accurate to a specific computer, and the port is accurate to the specific program.
2.osi seven-layer model
Intro
Note A complete computer system is composed of hardware, operating system, application software three, with these three conditions, a computer systems can play with their own (play a single game, play a minesweeper what)
If you want to play with others, then you need to surf the internet, what is the Internet?
The core of the Internet is made up of a bunch of protocols, the Protocol is the standard, such as the world's standard of communication is English, if the computer compared to human, Internet Protocol is the computer industry English. All computers have learned the Internet Protocol, and all the computers can send and receive information according to the unified standard to complete the communication.
OSI seven-layer model
People divide the Internet protocol logically into different levels according to the Division of labor:
3.socket Concept Socket Layer
Understanding sockets
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 set of simple interface is all, let the socket to organize data to meet the specified protocol.
in fact, standing on your point of view, the socket is a module. We establish the connection and communication between two processes by invoking the methods already implemented in the module. Some people also say the socket as IP+port, because IP is used to identify the location of a host in the Internet, and port is used to identify an application on this machine. So as long as we have the IP and port established, we can find an application and use the socket module to communicate with it.
stand in my angle and look at the socket.3. History of Socket sockets
Sockets originated in the the 1970s UC Berkeley version of Unix, which is what people call BSD Unix. Therefore, sometimes people also refer to sockets as "Berkeley sockets" or "BSD sockets". Initially, sockets are designed to be used for communication between multiple applications on the same host. This is also called interprocess communication, or IPC. There are two types of sockets (or two races), which are file-based and network-based.
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)
4.TCP Protocol and UDP protocol
TCP(transmission Control Protocol) 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.
UDP(User Datagram Protocol) 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).
I know that you don't understand that, directly.
Back to Top
Four. Socket (socket) Initial use of socket based on TCP protocol
TCP is link-based, you must start the server, and then start the client to link the server
Server Side
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)
Client 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
problem: Some students may encounter when restarting the server
Workaround:
#Add a socket configuration to reuse IP and portsImportSocket fromSocketImportSol_socket,so_reuseaddrsk=Socket.socket () sk.setsockopt (SOL_SOCKET,SO_REUSEADDR,1)#That 's it, in front of BIND plusSk.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)
Socket based on UDP protocol
UDP is non-linked and can receive messages directly after starting the service without having to establish a link in advance
Simple to useServer Side
import Socketudp_sk = Socket.socket ( Type=socket. SOCK_DGRAM) # Create a server socket udp_sk.bind ( Span style= "COLOR: #800000" > " 127.0.0.1 , 9000)) # bind server socket msg,addr = Udp_ Sk.recvfrom (1024) print (msg) udp_sk.sendto (b " hi ", addr) # Dialog (receive and send) udp_sk.close () # close server sockets
Client Side
Import socketip_port= ('127.0.0.1', 9000) Udp_sk=socket.socket (type= Socket. SOCK_DGRAM) udp_sk.sendto (b'hello', ip_port) back_msg,addr=udp_ Sk.recvfrom (1024x768)print(Back_msg.decode ('utf-8'), addr)
Python path-network programming