2019-08-14-网络编程作业

from socket import *
import os
# 服务器
server = socket()  # 创建套接字对象
server.bind(('10.7.181.83', 8989))   # 绑定IP和端口
server.listen(512)     # # 3) 等待连接并且设置同一时间能够接收的请求数量
while True:
    connect, address = server.accept()   # 连接
    while True:
        recv_data = str(connect.recv(1024), encoding='utf-8')  # 接受0或1
        if recv_data == '1':
            send_message = input('文字信息:')
            connect.send(send_message.encode())
        if recv_data == '0':
            send_message = os.listdir('./全职猎人人物')
            connect.send(str(send_message).encode())
            recv_data1 = connect.recv(1024).decode()
            if recv_data1 in send_message:
                path = '全职猎人人物/' + recv_data1
                with open(path, 'rb') as f:
                    connect.send(f.read())
            else:
                connect.send('无此图片!'.encode())
from socket import *
client = socket()   # 创建套接字对象
client.connect(('10.7.181.83', 8989))  #连接服务器
while True:
    print('0.图片信息\n1.文字信息')
    send_message = input('请选择 0 or 1 :')
    client.send(send_message.encode())
    re_data = client.recv(1024)
    if send_message == '1':
        print(re_data.decode(encoding='utf-8'))
    elif send_message == '0':
        print(re_data.decode())
        select = input('选择图片名称:')
        client.send(select.encode())
        while True:
            re_data1 = client.recv(1024)
            with open('files/new.jpg', 'ab') as f:
                f.write(re_data1)
                print(len(re_data1))
            if len(re_data1) < 1024:
                print('传输成功')
                break

你可能感兴趣的:(2019-08-14-网络编程作业)