Python3 advanced---socket programming

1. Three elements of network communication

(1) IP address

(2) Port number

(3) Transmission protocol

2. Socket communication process based on TCP protocol

Process introduction:

(1) The server creates a socket according to the address type, socket type and protocol type (you can imagine that the socket is a bank)

(2) Bind the ip and port number to the socket (you can imagine that the IP is the bank address and the port number is the house number)

(3) The server socket listens to the port number request, and is ready to receive and process client requests at any time. At this time, the server socket is not opened (it can be imagined that the door of the bank is not opened, waiting for the customer to come)

(4) The client creates a socket

(5) The client opens the socket, and then connects to the server's socket according to the server's IP and port number (it can be imagined as trying to open the door of the bank)

(6) Before the client requests the connection, the server is in a blocked state. After the server socket receives the client socket request, it starts to open the socket, and then creates a new socket (it can be imagined that the bank gate is Closed, when requesting service, the bank opens the door and creates a service window, that is, the new socket for serving customers)

(7) Then communicate between the server and the client

(8) The client disconnects and closes

(9) The server is closed (closed is the new socket, which is the service window)

3. Single-threaded communication

 (1) Client:         

# -*- encoding:utf-8 -*-
import os
import socket

"""
文件上传:客户端

"""

def post_file(sk_obj, file_path):
    """
    上传文件
    :param sk_obj: socket对象
    :param file_path: 文件路径
    :return: 
    """
    # 发送文件名qq.jpg
    file_name = os.path.split(file_path)[1]
    sk_obj.sendall(file_name.encode("utf-8"))

    # 发送文件大小
    file_size = os.stat(file_path).st_size
    sk_obj.sendall(str(file_size).encode("utf-8"))

    # 发送文件内容
    with open(file_path, "rb") as f:
        while file_size > 0:
            sk_obj.sendall(f.read(1024))
            file_size -= 1024


sk = socket.socket()
sk.connect(("127.0.0.1", 13000))
print("客户端上线了。。。。")

file_path = r"qq.jpg"
post_file(sk, file_path)
print("客户端发送图片数据给服务端")

sk.close()

(2) Server

# -*- encoding:utf-8 -*-
import socket

"""
文件上传:服务端

"""


def get_file(sk_obj):
    """
    接收文件
    :param sk_obj: socket对象
    :return: 
    """
    # 接受文件名
    file_name = sk_obj.recv(1024).decode("utf-8")
    sk_obj.sendall(b"OK")

    # 接受文件大小
    file_size = int(sk_obj.recv(1024).decode("utf-8"))
    sk_obj.sendall(b"OK")

    # 接受文件内容
    with open(file_name, "wb") as f:
        while file_size > 0:
            f.write(sk_obj.recv(1024))
            file_size -= 1024


# 创建socket对象
sk = socket.socket()

# 为socket对象绑定ip和端口号
sk.bind(("127.0.0.1", 13000))

# 监听
sk.listen()
print("服务端启动了。。。。。")

# 等待传入连接
# 连接之前,处于等待状态
# 连接之后,会返回一个新的套接字和客户端ip地址
conn, address = sk.accept()
print("客户端IP地址为:", address)

get_file(conn)
print("服务端接收图片数据")

conn.close()

sk.close()

 

Guess you like

Origin blog.csdn.net/qq_19982677/article/details/108190818