Python full stack learning --day33 (network programming-socket)

1. Network programming

Description: You have now learned to write python code, if

You have written two python files a.py and b.py, run them separately, and you will find that these two python files run well respectively. But what if you want to pass a piece of data between the two programs?

This problem can be solved with your current knowledge. We can create a file, write the content that a.py wants to pass to the file, and then b.py can read the content from this file.

 

But what should you do when your a.py and b.py are on different computers?

Similar mechanisms include computer network disks, QQ and so on. We can chat with others on our computer, and upload and download content to the network disk on our own computer. These are two programs communicating.

 

2. Architecture of software development

The applications we know that involve communication between two programs can be roughly divided into two categories:

The first is the application category: QQ, WeChat, Netdisk, Youku are desktop applications that need to be installed

The second is the web class: applications such as Baidu, Zhihu, Blog Park, etc. that can be used directly by using a browser to access

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

1. C/S Architecture

C/S is: Client and Server, Chinese meaning: client and server architecture, this architecture is also divided from the user level (or the physical level).

The client here generally refers to the client application EXE. The program needs to be installed before it can run on the user's computer, which is highly dependent on the user's computer operating system environment.

2.B/S architecture

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

Browser browser, in fact, is also a client client, but this client does not require you to install any application, just request server-side related resources (web page resources) through HTTP on the browser, and the client Browser browser will Able to add, delete, modify and check.

 

3. Network foundation

First of all, the program must be started, and secondly, the address of this machine must be available. We all know that the address of our people is probably the word country\province\city\district\street\lou\house number. Then every networked machine also has its own address on the network. How is its address represented?

It is represented by a string of numbers, for example: 100.4.5.6

 

socket concept

socket layer

what is socket

Socket is an abstraction layer between the application layer and the middleware that communicates with the TCP/IP protocol suite. It is a set of interfaces. In the design mode, Socket is actually a facade mode, which hides the complex TCP/IP protocol family behind the Socket interface. For users, a set of simple interfaces is all, and let the Socket organize the data to meet the specified requirements. protocol.

In fact, from the perspective of development, socket is a module. We do this by calling the methods already implemented in the module and establishing the connection and communication between the two processes.

Some people also say socket as ip+port, because IP is used to represent the location of a host in the Internet, and port is used to identify an application on this machine.

Therefore, we can find an application as long as we establish the IP and port, and use the socket module to communicate with it.

tcp protocol and udp protocol

TCP: Reliable, connection-oriented protocol (eg: making phone calls), low transmission efficiency, full-duplex communication (send buffer & receive buffer), oriented to byte streams. Applications using TCP: WEB browsers; e-mail, file transfer programs.

UDP: Unreliable, connectionless program, high transmission efficiency. One-to-one, one-to-many, many-to-one, many-to-many, message-oriented, best-effort service, no congestion control. Applications using UDP: Domain Name System (DNS); Video Streaming; Voice over IP (voip).

4. Initial use of sockets

socket based on TCP protocol

tcp is based on the link, you must start the server first, and then start the client to link the server

server side

 

import socket
# A communication based on the tcp protocol
sk = socket.socket() # buy a phone
# sk.bind(('192.168.11.53',9999)) # Install a calling card
sk.bind(('127.0.0.1',9999)) # install a calling card
# 8000 - 10000
sk.listen() # boot

conn,addr = sk.accept() # Waiting for the call to connect the two of us, the address of the other party
print(addr)
conn.send('Hello'.encode('utf-8'))
ret = conn.recv(1024) #1024 means accept 1024 bytes
print(ret.decode('utf-8'))

conn.close() # hang up
sk.close() # close the phone

# The loopback address of the computer is 127.0.0.1

  client side

import socket

sk = socket.socket() # buy a phone
sk.connect(('127.0.0.1',9999)) # call

ret = sk.recv (1024)
print(ret.decode('utf-8'))
sk.send('You are good too'.encode('utf-8'))

sk.close() # shutdown

  

Example one:

Online customer service chat

###server side
import socket
# A communication based on the tcp protocol
sk = socket.socket()  
sk.bind(('192.168.11.59',9999))    
sk.listen() # listen
conn,addr = sk.accept()   

while True:
    ret = conn.recv(1024) #1024 means the server accepts 1024 bytes
    print(ret.decode('utf-8'))
    message = input('>>>:')
    conn.send(message.encode('utf-8'))
    if message == 'q':
        conn.close() # hang up
        sk.close() # close the phone
#####client side
import socket

sk = socket.socket() #buy a mobile phone
sk.connect(('192.168.11.59',9999)) #Call
while True:
    message = input('>>>:')
    sk.send(message.encode('utf-8'))
    ret = sk.recv (1024)
    print(ret.decode('utf-8'))
    if message == 'q':
        sk.close() #shutdown

  

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325313838&siteId=291194637