socket - homework

4352 단어
고객센터 발송 기능 요청, 서버는 접수된 도착 요청을 통해 고객센터에 필요한 문자나 사진 서버를 발송합니다
"""__ 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()


좋은 웹페이지 즐겨찾기