服务端:

# -*- coding:utf-8 -*-

from socket import *
import sys

class Tcp_Server:

    def __init__(self, ip, port, buff, listenNum):
        self.addr = (ip, port)
        self.buff = buff
        self.listenNum = listenNum

    def get_socket(self):
        try:
            new_socket = socket(AF_INET, SOCK_STREAM)
            return  new_socket
        except error:
            print("socket 创建失败")
            sys.exit(1)

    def get_bind(self, new_socket, addr):
        try:
            new_socket.bind(addr)
        except error:
            print("socket 绑定失败")
            sys.exit(1)

    def get_listen(self, new_socket, listenNum):
        try:
            new_socket.listen(listenNum)
        except error:
            print("socket 监听失败")
            sys.exit(1)

    def client_cdtion(self, new_tcp_socket, buff):
        try:
            data = new_tcp_socket.recv(buff).decode()
            return  data
        except error:
            pass

    def send_msg(self, new_tcp_socket, data):
        try:
            if not data:
                pass
            else:
                new_tcp_socket.send(data.upper().encode())

        except error:
            pass

    def new_tcp_server(self):

        new_socket = self.get_socket()
        self.get_bind(new_socket, self.addr)
        self.get_listen(new_socket, self.listenNum)
        print("监听开始...")

        while True:
            new_tcp_socket, new_addr = new_socket.accept()
            print("connection from:", new_addr)

            data = self.client_cdtion(new_tcp_socket, self.buff)
            self.send_msg(new_tcp_socket, data)

        new_tcp_socket.close()
        new_socket.close()

server = Tcp_Server("192.168.31.215", 12345, 2048, 5)
server.new_tcp_server()

'''
TCP 服务端伪代码

新建 socket 连接
绑定端口
开始监听

循环接收请求
循环接收数据,接收数据后执行操作,将数据发送出去

'''

客户端:
# -*- coding:utf-8 -*-

import socket
import sys

class Tcp_Client:
    def __init__(self, ip, port, buff):

        self.buff = buff
        self.addr = (ip, port)

    def set_new_socket(self):
        try:
            new_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            return  new_socket
        except socket.error:
            print("创建 socket 连接失败")
            sys.exit(1)

    def set_connect(self, new_client):
        try:
            new_client.connect(self.addr)
        except socket.error:
            print("服务器连接地址出错")
            sys.exit(1)
    def send_msg(self, new_client, data):
        try:
            new_client.send(data.encode())
        except socket.error:
            print("tcp 连接未建立")
            sys.exit(1)
    def recv_msg(self, new_client, buff):
        try:
            data = new_client.recv(buff).decode()
            return data
        except socket.error:
            print("服务器已断开")
            sys.exit(1)

    def tcp_new_client(self):
        new_client = self.set_new_socket()
        self.set_connect(new_client)

        while True:
            data = input(">")
            if not data:
                print("请输入:")
            self.send_msg(new_client, data)

            data = self.recv_msg(new_client, self.buff)
            if not data:
                print("没有接收到数据")
            print(data)
        new_client.close()

test = Tcp_Client('192.168.31.215',12345, 2048)
test.tcp_new_client()