Python full stack development--network programming one

Source: Internet
Author: User

I. Architecture of software development

Applications that involve communication between two programs can be broadly divided into two types:

The first is the application class: QQ, network disk This category is required 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: C/S architecture and B/S architecture

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.

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.

Ii. 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. Then each networked machine has its own address on the network,

What does it say about the address? 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).
View Code
" Port " is the translation of the English port, it can be regarded as the export of communication between equipment and the outside world."
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, which hides the complex TCP/IP protocol family behind the socket interface.

For the user, a simple set of interfaces is all, allowing the socket to organize the data to conform to the specified protocol.

The socket is actually 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. 
View Code4. 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 only used on a platform, or have been discarded, or are rarely used, or are not implemented at all, in all address families, Af_inet is the most widely used one, Python supports a variety of address families, but since we only care about network programming, most of the time I only use af_inet.

5.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.

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 ()#Create a communication tool, such as buying a phone when you callSk.setsockopt (socket. Sol_socket,socket. so_reuseaddr,1)#to avoid a service restart, report the address already in useSk.bind (('192.168.13.35', 8080))#bind address to Socket, Sk.bind ((' IP ', ' Port ') IP address and portSk.listen ()#Monitoring LinksCONN,ADDR = Sk.accept ()#Accept Client Links whileTrue:ret= Conn.recv (1024x768). Decode ('Utf-8')#receive client information, accept 1024 bytes each time, decode with Utf-8    ifRET = ='Bye':          Break    Print(ret) Info= Input ('>>>') conn.send (bytes (info,encoding='Utf-8'))#send information to the client, must be of type bytes, encoded with Utf-8conn.close () sk.close ( )
Server

Client Side

ImportSocketsk=socket.socket () Sk.connect ('192.168.13.96', 8080)) whileTrue:info= Input ('>>>') sk.send (bytes (info,encoding='Utf-8')) RET= Sk.recv (1024x768). Decode ('Utf-8')    Print(ret)ifinfo = ='Bye': sk.send (b'Bye')         Breaksk.close ()
Client

Python full stack development--network programming one

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.