day27 summary

Yesterday Review

1. Software development architecture
C / S
Client: Client
Server: server
advantages:
network resources and less use of the software is stable.
Cons:
Users in the use of multiple software to download multiple clients, each update software users need to update to follow.
Poor user experience!
B / S
Browser: Browser (client)
Server: server
advantages:
users need to download multiple clients to browse all the software acts as a client.
Cons:
network resource consumption, when the network is unstable, the use of the software is not stable.
2. Network Programming history
all the advanced technologies derived from the military.
Want telecommunications, hence the network programming.
Early remote communication:
1. Telephone ---> telephone line
2. big butt computer ---> cable, wired LAN
3. laptop ---> wireless LAN
remote communication:
1. The physical connection media, network cards. ..
2. The computer to communicate:

        - 人与人之间进行通信,需要语言,英语、中文
                    - 计算机之间进行通信,需要"互联网协议"。
            互联网协议(OSI七层协议)
            - 应用层
                    - 表示层
                        - 会话层
                            - 传输层
                            - 网络层
                                    - 数据链路层
                                        - 物理连接层
                                            由下往上:
                        - 物理连接层
            基于电信号发送一堆二进制010100111111101000数据
                        - 数据链路层
            把二进制数据交给 “以太网协议”处理:
                        1.规定好电信号的分组方式
                        2.必须要一个网卡
                            - mac地址
                        由12位唯一编码
                              前6位: 厂商号
                           后6位: 流水号
            以太网协议:
                      广播
                      单播
                      弊端: 广播风暴,无法跨局域网通信。
            - 交换机
                让多台电脑连接到一起
            - 互联网:
                让所有电脑都互联到一起。
  • Network layer

      连接到互联网的电脑都需要有一个ip地址。
    
      IP: 用于唯一标识计算机(局域网)具体位置。
          点分十进制:
              最小值: 0.0.0.0
              最大值: 255.255.255.255
    
      - 传输层
          TCP/UDP协议:
              TCP: 是一个流式协议。
                  基于TCP协议进行通信,必须要创建双向通道。
                      - 一个是客户端往服务端的管道
                      - 另一个是服务端往客户端的管道
                  三次握手,四次挥手:
                      三次握手建连接
                      四次挥手断连接
      - 应用层

    http (focus), ftp (understand)
    socket socket:

    recv data is fetched from memory

subprocess module

client
# import socket
# 
# client = socket.socket()
# 
# client.connect(
#     ('127.0.0.1', 9527)
# )
# 
# while True:
# 
#     cmd = input('》》》:')  # dir
# 
#     client.send(cmd.encode('utf-8'))
# 
#     if cmd == 'q':
#         break
# 
#     # 接受终端返回的结果
#     data = client.recv(10000000000).decode('gbk')
# 
#     print(data)
# 
# client.close()


# 发送数据量小,并且时间间隔短的数据

import socket
import time

client = socket.socket()

client.connect(
    ('127.0.0.1', 9527)
)



client.send(b'hello')
time.sleep(1)
client.send(b'hello')
time.sleep(1)
client.send(b'hello')

client.close()
server
# import socket
# import subprocess
#
# server = socket.socket()
#
# server.bind(
#     ('127.0.0.1', 9527)
# )
#
# server.listen(5)
#
# while True:
#     conn, addr = server.accept()
#
#     while True:
#         try:
#             # recv的数据是从内存中获取
#             cmd = conn.recv(1024).decode('utf-8')
#
#         if cmd == 'q':
#             break
#
#         if len(cmd) == 0:
#             continue
#
#         print(cmd)
#
#         # 执行cmd命令
#         obj = subprocess.Popen(
#             cmd,
#             shell=True,
#             stdout=subprocess.PIPE,
#             stderr=subprocess.PIPE,
#         )
#
#         result = obj.stdout.read() + obj.stderr.read()
#
#         conn.send(result)
#
#     except Exception as e:
#         print(e)
#         break
# conn.close()

import socket

server = socket.socket()

server.bind(
    ('127.0.0.1', 9527)
)

server.listen(5)


conn, addr = server.accept()


data1 = conn.recv(1024)
data2 = conn.recv(1024)
data3 = conn.recv(1024)
print(data1)
print(data2)
print(data3)

Stick package problem

Stick package problem
of data sent by the server for the first time, received a single client can not be accurately completed.
The next data transmitted on the primary data stick together.
1. The length of the other side can not predict the size of the data need to accept.
2. small data transmitted several times in succession, and the short time interval of data transmitted disposable packaging.
Characteristics TCP protocol:
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
must first define the header, sending a header, and then send real data.
- both want to send a file, the file description information want to send
the client sends to the server dictionary
send_dic = {
file_name: File name
file_size: the real length of the file 1000000
}
by json module data bytes into a sequence
json_data = json.dumps (send_dic)
bytes_data = json_data.encode ( 'utf-8 ') # bytes
to obtain dictionary header
headers = struct.pack ( 'i', len (bytes_data))
server receives the dictionary, and the real data file received

server
import socket
import json
import struct
server = socket.socket()
server.bind(
    ('127.0.0.1', 9527)
)
server.listen(5)

while True:
    conn, addr = server.accept()
    while True:
        try:
            # 先接收报头
            headers = conn.recv(4)

            # 解包获取真实数据长度
            data_len = struct.unpack('i', headers)[0]

            # 获取字典数据真实长度
            bytes_data = conn.recv(data_len)

            back_dic = json.loads(bytes_data.decode('utf-8'))

            print(back_dic)

        except Exception as e:
            print(e)
            break
    conn.close()
client
import socket
import struct
import json
import time

client = socket.socket()

client.connect(
    ('127.0.0.1', 9527)
)

while True:

    send_dic = {
        'file_name': 'jason真实写真集.avi',
        'file_size': 10000000  # 10G
    }

    # json序列化,并转码成bytes类型数据
    json_data = json.dumps(send_dic)
    bytes_data = json_data.encode('utf-8')

    # 先做报头
    headers = struct.pack('i', len(bytes_data))
    client.send(headers)
    client.send(bytes_data)

    time.sleep(10)

Upload large files

Client to server upload large files

server
import socket
import json
import struct
server = socket.socket()
server.bind(
    ('127.0.0.1', 9527)
)
server.listen(5)

while True:
    conn, addr = server.accept()
    try:
        # 先接收字典报头
        headers = conn.recv(4)

        # 解包获取字典真实数据长度
        data_len = struct.unpack('i', headers)[0]

        # 获取字典真实数据
        bytes_data = conn.recv(data_len)

        # 反序列得到字典
        back_dic = json.loads(bytes_data.decode('utf-8'))

        print(back_dic)

        # 拿到字典的文件名,文件大小
        file_name = back_dic.get('file_name')
        file_size = back_dic.get('file_size')

        init_data = 0
        # 1.以文件名打开文件,准备写入
        with open(file_name, 'wb') as f:

            # 一点一点接收文件,并写入
            while init_data < file_size:
                data = conn.recv(1024)
                # 2.开始写入视频文件
                f.write(data)
                init_data += len(data)

            print(f'{file_name}接收完毕!')

    except Exception as e:
        print(e)
        break

conn.close()
client
import socket
import struct
import json

client = socket.socket()

client.connect(
    ('127.0.0.1', 9527)
)


# 1.打开一个视频文件,获取视频数据大小
with open(r'D:\jason真实写真集.mp4', 'rb') as f:
    movie_bytes = f.read()
    # 关闭文件

# 2.为视频文件组织一个字典,字典内有视频的名称,视频大小
send_dic = {
    'file_name': 'jason真实写真集.mp4',
    'file_size': len(movie_bytes)  # 10G
}

# 3.先打包字典,发送headers报头,再发送真实字典数据
json_data = json.dumps(send_dic)
bytes_data = json_data.encode('utf-8')
headers = struct.pack('i', len(bytes_data))
# 发送报头
client.send(headers)
# 发送真实字典数据
client.send(bytes_data)

# 4.接着发送真实视频文件数据
init_data = 0
num = 1
with open(r'D:\jason真实写真集.mp4', 'rb') as f:
    while init_data < len(movie_bytes):
        # 最后一次获取,有多少拿多少
        send_data = f.read(1024)
        print(send_data, num)
        num += 1
        # 每次发送1024数据
        client.send(send_data)
        # 为初始发送数据 + 已发送数据的长度
        init_data += len(send_data)

UDP

UDP is a transport protocol.
1) does not need to establish a two-way pipeline.
2) do not stick pack
3) the client sends data to the server without waiting for the server to return successfully received
3) easy to lose data, data insecurity.

TCP: like on the phone.
UDP: like texting.

server
import socket

# SOCK_DGRAM: 代表UDP
server = socket.socket(type=socket.SOCK_DGRAM)

# 服务端需要绑定ip+port
server.bind(
    ('127.0.0.1', 9527)
)

# TCP
# conn, addr = server.accept()
# conn.recv()

# UDP
msg, addr = server.recvfrom(1024)
msg1, addr1 = server.recvfrom(1024)
msg2, addr2 = server.recvfrom(1024)

print(msg, msg1, msg2)
client
import socket
client = socket.socket(type=socket.SOCK_DGRAM)
server_ip_port = ('127.0.0.1', 9527)

client.sendto(b'hello', server_ip_port)
client.sendto(b'hello', server_ip_port)
client.sendto(b'hello', server_ip_port)
client.sendto(b'hello', server_ip_port)
client.sendto(b'hello', server_ip_port)

QQ chat room

server
import socket

server = socket.socket(type=socket.SOCK_DGRAM)

server.bind(
    ('127.0.0.1', 9527)
)

while True:

    # 服务端接收客户端传过来的消息
    msg, addr = server.recvfrom(1024)  # (消息,客户端地址)
    msg1, addr1 = server.recvfrom(1024)  # (消息,客户端地址)
    msg2, addr2 = server.recvfrom(1024)  # (消息,客户端地址)

    print(addr)
    print(addr1)
    print(addr2)
    print(msg.decode('utf-8'))
    print(msg1.decode('utf-8'))
    print(msg2.decode('utf-8'))

    # 服务端往客户端发送消息
    send_msg = input('服务端发送消息:').encode('utf-8')
    server.sendto(send_msg, addr)
    server.sendto(send_msg, addr1)
    server.sendto(send_msg, addr2)
client
import socket

client = socket.socket(type=socket.SOCK_DGRAM)

server_ip_port = ('127.0.0.1', 9527)

while True:
    send_msg = input('客户端1: ').encode('utf-8')

    # 发送消息必须要加上对方地址
    client.sendto(send_msg, server_ip_port)

    # 能接收任何人的消息
    msg = client.recv(1024)

    print(msg.decode('utf-8'))

Guess you like

Origin www.cnblogs.com/zhm-cyt/p/11707229.html