2019-08-14

"""__author__ == rong"""
# Server
from socket import *
import os


server = socket()
server.bind(('10.7.181.119', 1031))
server.listen(512)
connect, address = server.accept()
while True:
    send_message = '1.请求文字信息\n2.请求图片信息'
    connect.send(send_message.encode(encoding='utf-8'))
    recv_message = connect.recv(1024).decode(encoding='utf-8')
    print('选择为:', recv_message)
    if recv_message == '1':
        send_message = '这是请求的文字信息'
        connect.send(send_message.encode(encoding='utf-8'))
        continue
    else:
        path = 'files'
        path_list = os.listdir(path)
        send_message = '选一个你要的图片,图片名:' + str(path_list)
    connect.send(send_message.encode())
    recv_message1 = connect.recv(1024).decode(encoding='utf-8')
    print('选择为:', recv_message1)
    if recv_message1 not in path_list:
        send_message = '没有这个图'
        connect.send(send_message.encode(encoding='utf-8'))
    else:
        send_message = '找到'
        connect.send(send_message.encode(encoding='utf-8'))
        with open('./files/{}'.format(recv_message1), 'rb') as p:
            connect.send(p.read())
# connect.close()
"""__author__ == rong"""
# Client
from socket import *

client = socket()
client.connect(('10.7.181.119', 1031))
while True:
    recv_data = client.recv(1024)
    print(recv_data.decode(encoding='utf-8'))
    while True:
        send_message = input('输入:')
        if send_message != '1' and send_message != '2':
            print('输入错误!')
            continue
        else:
            break
    client.send(send_message.encode(encoding='utf-8')) # 发送选择
    recv_data = client.recv(1024) # 接收文字或是图片
    print(recv_data.decode(encoding='utf-8'))
    if send_message == '1':
        continue
    send_message = input('输入:')
    client.send(send_message.encode(encoding='utf-8'))
    recv_data1 = client.recv(1024)
    if recv_data1.decode(encoding='utf-8') == '找到':
        while True:
            recv_data2 = client.recv(1024)
            with open('{}'.format(send_message), 'ab') as f:
                f.write(recv_data2)
            if (len(recv_data2) < 1024):
                break
    else:
        print(recv_data1.decode(encoding='utf-8'))
# client.close()

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