Day18 - 作业

写一个服务器以及客户端能选择发送图片以及自动回复消息:
服务器端:

from socket import *
import os
server = socket()
server.bind(('10.7.181.89', 6688))
server.listen(100000)
while True:
    connect, address = server.accept()
    while True:
        data = connect.recv(1024000)
        if data.decode(encoding='utf-8') == '2':
            connect.send(str(os.listdir('files')).encode())
            data2 = connect.recv(1024000)
            name = data2.decode(encoding='utf-8')
            print(name)
            with open('files/'+name, 'rb') as f:
                connect.send(f.read())
                break
        else:
            print(data.decode(encoding='utf-8'))
            connect.send(data)
connect.close()

客户端:

from socket import *

client = socket()
client.connect(('10.7.181.89', 6688))
while True:
    with open('test1.txt', 'r', encoding='utf-8') as f:
        print(f.read())
    num = input(':')
    if num == '1':
        send_message = input('客户端:')
        client.send(send_message.encode())
        re_data = client.recv(1024000)
        print(re_data.decode(encoding='utf-8'))
    if num == '2':
        send_message1 = '2'
        client.send(send_message1.encode())
        re_data = client.recv(1024000)
        print(re_data.decode(encoding='utf-8'))
        choices = input('请输入需要下载图片的完整名字:')
        client.send(choices.encode())
        # while True:
        re_data = client.recv(1024000)
        with open(choices, 'ab') as f:
            f.write(re_data)
    else:
        print('请重新选择:')
client.close()

你可能感兴趣的:(Day18 - 作业)