Python Path-Network programming

Source: Internet
Author: User

I. Wedge

You want to pass a data between two programs, you need to use network communication.

Two. Architecture of software development:

The first kind: Application class: QQ,, network disk, Youku This category belongs to the desktop application that needs to be installed.

The second kind: Web class: Baidu, Know, blog Park, and so on using the browser access can be directly used by the application.

The nature 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, (also the physical level).

2.B/S Architecture

b/S is: Browser and server, Chinese meaning: browser-side and the servers-side architecture, this architecture is divided from the user level.

Browser browser, in fact, is a kind of client clients, but this client does not need to install what application, just need to request the server-side related resources via HTTP (Web resources), the client browser browser can be used to make additional deletions and checks.

Three. Network Foundation.

1. OSI seven layer model.

2.socket concept

The 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, the socket is actually a façade mode, it hides the complex TCP/IP protocol family behind the socket interface, for the user, a simple set of interfaces is all, Let the socket organize the data to conform to the specified protocol.

In fact, standing on your point of view, the socket is a module. We do this by invoking the method implemented in the module
Establish connectivity and communication between two processes. Someone also said 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.

3. TCP protocol and UDP protocol

TCP Reliable. Connection-oriented protocol, low transmission efficiency full duplex communication (send cache, receive cache), byte stream oriented.

Applications that use TCP: Web browsers, e-mail, and file transfer programs.

UDP unreliable, non-connected services, high transmission efficiency (send money delay small), one-to-one, one-to-many, many-to-many, message-oriented, to do their best to serve, no congestion control, the use of UDP applications: Domain Name System (DNS); Video streaming, IP voice.

Four. Socket (socket) First use.

1. Socket based on TCP protocol

TCP is connection-based, and you must start the server before you start the client to connect to 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)
View Code

Client Side

Import= Socket.socket ()    #  creates a client socket sk.connect ('127.0.0.1 ', 8000)    #  try to connect to server sk.send (b'hello'  ) # dialog (send/Receive) Print  #  Close Customer sockets
View Code

Problem: May be encountered when restarting the server

Workaround:

#Add a socket configuration to reuse IP and ports.ImportSocket fromSocketImportSol_socket,so_reuseaddrsk=Socket.socket () sk.setsockopt (SQL_SOCKET,SO_REUSEADDR,1)#That 's it, plus before bind.Sk.bind ('127.0.0.1', 8000)#bind an address to a socketSk.listen ()#Monitoring LinksCONN,ADDR = Sk.accept ()#Receive 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)

Python Path-Network programming

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.