qq chat room classroom summary day27

subprocess module

  1. It can help you to execute operating system commands through a terminal code.
  2. And the terminal returns the execution result of the command.
import subprocess
cmd = input('cmd>>:')

obj = subprocess.Popen(
    # cmd命令
    cmd,
    # Shell = True
    shell = True,
    # 返回正确结果参数
    stdout = subprocess.PIPE,
    # 返回错误结果参数
    stderr = subprocess.PIPE

)
result = obj.stdout.read() + obj.stderr.read()
print(result.decode('gbk'))

Stick package problem

Data sent from the server for the first time, clients can not accurately disposable been received.

Next data transmitted on the primary data glued together.

  1. We can not predict the size of the data length of the other party needs to receive.
  2. A plurality of successive data transmit small amounts of data, and the time interval is short transmission disposable packaging.

TCP protocol features:

tcp is a streaming protocol will send several successive small amount of data, and the short time interval of data transmitted disposable packaging.

Stick package to solve the problem

  • struct module

We must first define the header, sending a header, and then send real data.

  • That is the description of want to send a file, and want to send the file

The client sends to the server dictionary

send_dic = {
    file_name:文件名
    file_size:文件的真实长度 100000
}
#  通过json模块序列化成bytes数据
    json_data = json.dumps(send_dic)
    bytes_data = json_data.encode('utf-8') # bytes
    
    # 先获取字典的报头
    headers = struct.pack('i',len(byte_data))
    
    # 服务端接收到字典,并接收文件的真实数据
    
    

UDP

UDP is a transport protocol.

  1. No need to establish a two-way pipeline.
  2. Not stick package.
  3. The client sends data to the server without waiting for the server to return successfully received.
  4. Easy to lose data, data insecurity.

TCP: like on the phone.

UDP: like texting.

SocketServer

python内置模块,可以简化socket套接字服务端的代码。

- 简化TCP与UDP服务端代码
- 必须要创建一个类

Guess you like

Origin www.cnblogs.com/shin09/p/11707453.html