2019-08-14day18作业

1,客户端
a.请求文字信息
b.请求图片信息
请选择:
[ 内容 ]
请输入你想要的图片的名字:bear1.png
True ->接收图片
False ->打印无图片

1-->发送文字 服务器直接给客户端发送文字信息
2-->发送图片 服务器将files下面所有的文件的文件名发送给客户端:
发送当前图片是否有的消息
发送图片文件
服务器

from socket import *
from threading import *
from os import *
from json import *


class DealClientThread(Thread):
    def __init__(self, connection: socket, address):
        super().__init__()
        self.connection = connection
        self.address = address


    def run(self) -> None:
        while True:
            re_data = self.connection.recv(1024)   #接收数据
            message = re_data.decode('utf-8')   # 以指定的编码格式解码bytes 默认为utf-8
            if message == 'text':
                self.connection.send('我是文字信息'.encode())
            elif message == 'image':
                all_files = os.listdir('./images')  #os.listdir用于返回指定文件夹的名字的列表
                self.connection.send(dumps(all_files).encode())   #发送所有图片名字
                image_name = self.connection.recv(1024)    #接收图片信息
                image_path = './images'+image_name.decode(encoding='utf-8') #对文件进行解码默认utf-8
                if os.path.exites(image_path):
                    print('文件存在')
                    self.connection.send('exists'.encode())   #发送存在并编码的文件
                    with open(image_path, 'rb') as f:       #以只读的方式打开文件,独处的内容是二进制数据
                        self.connection.send(f.read())
                else:
                    print('文件不存在')
                    self.connection.send('notExists'.encode())
            else:
                self.connection.close()
                break


def creatServer():
    server = socket()
    server.bind(('10.7.181.86', 8899))
    server.listen(521)

    while True:
        print('开始监听。。')
        connection, address = server.accept()
        t = DealClientThread(connection, address)
        t.start()


if __name__ == '__main__':
    creatServer()

客户端

from socket import *
from json import *


client = socket()
client.bind(('10.7.181.86', 8899))

while True:
    print('1.文字信息\n2.图片信息\n3.文字信息')
    value = input('请选择(1-3):')
    if value == '1':
        client.send('text'.encode())
        message_data = client.recv(1024)
        print(message_data.decode(encoding='utf-8'))
    elif value == '2':
        client.send('image'.encode())
        image_massage = client.recv(1024)
        all_files = loads(image_massage.decode(encoding='utf-8'))
        for index in range(all_files):
            print('%d: %s' % (index, all_files[index]))
        num = int(input('输入需要的图片号码:0-%d' % (len(all_files))))  #选择需要输入的图片号码
        client.send(all_files[num].encode())   #客户端发送图片的号码
        is_exists = (client.recv(1024).decode(encoding='utf-8'))
        if is_exists == 'exists':
            image_data = bytes()      #创建空二进制
            while True:
                small_image_data = client.recv(1024)
                image_data += small_image_data
                if len(small_image_data) < 1024:
                    break
            with open('./client'+all_files[num], 'wb') as f:
                f.write(image_data)
                print('图片下载完成')
        else:
            print('图片不存在')
    else:
        client.send('exit'.encode())
        client.close()
        break
···
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容