网络编程_作业

from socket import *
from os import walk
server = socket()
server.bind(('10.7.181.84', 1999))
server.listen(420)
while True:
    connect, address = server.accept()
    send_message = '请选择:a、文字信息 b、图片信息'
    connect.send(send_message.encode())
    while True:
        re_data = connect.recv(1024)
        if re_data.decode(encoding='utf-8') == 'a':
            connect.send(input('召唤师段位:').encode())
        elif re_data.decode(encoding='utf-8') == 'b':
            while True:
                for root, dirs, files in walk('file'):
                    connect.send(('请选择图片名称:' + str(files)).encode())
                    re_data = connect.recv(1024)
                    for x in files:
                        if x == re_data.decode(encoding='utf-8'):
                            with open('file/' + x, 'rb') as f:
                                connect.send(f.read())
                            break
                    break
                re_data = connect.recv(1024)
                if re_data.decode() == 'n':
                    break
        else:
            connect.send('无此请求!'.encode())


from socket import *
client = socket()
client.connect(('10.7.181.84', 1999))
re_data = client.recv(1024)
print(re_data.decode(encoding='utf-8'))
while True:
    send_message = input('选择请求:')
    client.send(send_message.encode())
    if send_message == 'a':
        re_data = client.recv(1024)
        print(re_data.decode(encoding='utf-8'))
        continue
    if send_message == 'b':
        while True:
            re_data = client.recv(1024)
            print(re_data.decode(encoding='utf-8'))
            x = input('图片名:')
            client.send(x.encode())
            while True:
                re_data = client.recv(1024)
                with open('files/' + x, 'ab') as f:
                    f.write(re_data)
                if (len(re_data)) < 1024:
                    break
            choice = input('y/n:')
            client.send(choice.encode())
            if choice == 'n':
                break
    else:
        re_data = client.recv(1024)
        print(re_data.decode())

你可能感兴趣的:(网络编程_作业)