2019-08-14 day18 作业

2019-08-14 day18 作业_第1张图片
作业要求 .png

功能能够完成但是没有做错误输入处理

服务器

import os
from socket import *

# 1创建服务器套接字
server = socket()
server.bind(('10.7.181.105', 9909))
server.listen(512)
# 开始监听
connect, address = server.accept()
print('客服端ip:', address)
if connect:
    send_message = '选项1.请求文字信息 \n选项2.请求图片信息'
    connect.send(send_message.encode())
# 2运行服务器
name_list = []
while True:
    print('开始监听')
    re_data = connect.recv(1024).decode(encoding='utf-8')
    print(re_data)
    send_message2 = ''
    if re_data == '1':
        send_message2 = '==========================\n' \
                        '请求图片信息你会获得一个所有图片名字的列表\n' \
                        '通过输入图片的名字来下载图片,输入3结束会话\n' \
                        '选项2.请求图片信息\n' \
                        '选项3.结束会话'
        connect.send(send_message2.encode())
    elif re_data == '2':
        name_list = os.listdir('server/')
        send_message2 = str(name_list) + '\n' \
                                         '请输入你要下载的图片名字'
        connect.send(send_message2.encode())
    elif re_data == '3':
        break
    else:
        if not name_list:
            send_message2 = '选择错误!!!!!!!!!\n' \
                            '请重新选择:\n' \
                            + send_message
            connect.send(send_message2.encode())
        elif re_data:
            path = 'server/' + re_data
            print(path)
        with open(path, 'rb') as f:
            connect.send(f.read())

connect.close()

客服端

"""
from copy import deepcopy
from socket import *

# 1创建客服端套接字
client = socket()
server_str = '10.7.181.105'
# input('请输入要连接的服务器ip:')
client.connect((server_str, 9909))
re_data = client.recv(1024)
print(re_data.decode(encoding='utf-8'))
choose_input = input('请输入你的选择:')
choose_input2 = ''
i = 0
while True:

    if choose_input in ['1', '2']:
        client.send(choose_input.encode())
        re_data = client.recv(1024)
        print(re_data.decode(encoding='utf-8'))
        choose_input = input('请输入你的选择:')
    if choose_input == '3':
        client.close()
        break
    elif choose_input not in ['1', '2']:
        print(choose_input, i)
        if choose_input != choose_input2:
            print('1')
            choose_input2 = deepcopy(choose_input)
            client.send(choose_input.encode())
        re_data = client.recv(1024)
        print(len(re_data))
        with open('client/'+choose_input, 'ab') as f:
            f.write(re_data)
            i += 1

        if len(re_data) < 1024:
            choose_input = '1'



你可能感兴趣的:(2019-08-14 day18 作业)