socket - homework

客服端发送功能请求,服务器通过接收的到请求,发送个客服端需要的文字或图片
服务器

"""__ author__= 小孩子 """

from socket import *
from os import listdir
from threading import *


class ServerThread(Thread):
    def __init__(self, connect, address):
        super().__init__()
        self.connect = connect
        self.address = address

    def run(self) ->None:
        self.func()

    def func(self):
        # 接受消息
        client_data = self.connect.recv(1024).decode()
        print('Client,{}:'.format(self.address[1]), client_data)
        if client_data == 'img':
            self.img_server()
        elif client_data == 'text':
            self.text_server()
        else:
            self.connect.close()
            return

    def img_server(self):
        list_img = str(listdir('../image'))
        while True:
            self.connect.send(list_img.encode())
            # 接受客户端发送的文件名
            client_file = self.connect.recv(1024)
            if client_file == 'exit':
                connect.close()
                break
            if client_file.decode() in list_img:
                with open('../image/' + client_file.decode(), 'rb') as file:
                    image = file.read()
                self.connect.send(client_file)
                # 发送图片
                self.connect.send(image)
            else:
                self.connect.send('False'.encode())
            self.func()

    def text_server(self):
        while True:
            self.connect.send(input('Server:').encode())
            client_data = self.connect.recv(1024).decode()
            print('Client,{}:'.format(self.address[1]), client_data)
            if client_data == 'exit':
                self.connect.close()


def servers():
    server = socket()
    # 绑定ip地址和端口
    server.bind(('10.7.181.121', 8070))
    # 设置最大连接数量
    server.listen(87)
    while True:
        print('监听中.....')
        connect, address = server.accept()
        t1 = ServerThread(connect, address)
        print(current_thread())
        t1.start()


if __name__ == '__main__':
    servers()

客户端

"""__ author__= 小孩子 """

from socket import *


class Clients:
    def __init__(self):
        self.client = socket()
        # 发送请求
        self.client.connect(('10.7.181.121', 8070))

    def text(self, func: str):
        # 告诉服务器,你需要什么
        self.client.send(func.encode())
        while True:
            # 接收消息
            server_date = self.client.recv(1024)
            print('Server:', server_date.decode())
            client_data = input('Client:')
            # 发送消息
            self.client.send(client_data.encode())
            if client_data == 'exit':
                self.client.close()
                break

    def image(self, func: str):
        # 告诉服务器,你需要什么
        self.client.send(func.encode())
        while True:
            # 接收图片列表
            server_date = self.client.recv(1024)
            print('Server:', server_date.decode())
            client_data = input('Client请输入图片名字:')
            # 发送图片名字给服务器
            self.client.send(client_data.encode())
            # 接收文件名
            file_name = self.client.recv(1024).decode()
            if file_name != 'False':
                while True:
                    # 接收消息
                    server_date = self.client.recv(1024)
                    with open(file_name, 'ab') as file:
                        file.write(server_date)
                    if len(server_date) < 1024:
                        print('下载文件成功!')
                        break
            else:
                print('没有这个文件')
            self.inter()

    def inter(self):
        with open('../files/menu.txt', 'r', encoding='utf-8') as file:
            inters = file.read()
        print(inters)
        function = input('请选择功能:')
        while True:
            if function == 'text':
                self.text(function)
            elif function == 'img':
                self.image(function)
            else:
                self.client.close()
                break


def main():
    clients = Clients()
    clients.inter()


if __name__ == '__main__':
    main()


你可能感兴趣的:(socket - homework)