day18-作业

  1. 客户端和服务器聊天,可以一直聊天,直到一方发送’拜拜’。然后就可以和下一个人一直聊
# 服务器
import socket
server = socket.socket()
server.bind(('10.7.187.68', 8000))
server.listen(100)
print('开始监听')
while True:
    conversation, addr = server.accept()
    while True:
        print('接收到请求:', addr)
        re_data = conversation.recv(1024)
        str1 = re_data.decode('utf-8')
        print('客户端:', str1)
        if str1 != '拜拜':
            message = input('请输入发送消息:')
            conversation.send(bytes(message.encode(encoding='utf-8')))
            if message == '拜拜':
                break
        else:
            break
    conversation.close()


# 客户端
import socket
client = socket.socket()
client.connect(('10.7.187.68', 8000))
while True:
    message = input('请输入需要发送的消息:')
    client.send(message.encode(encoding='utf-8'))
    if message != '拜拜':
        re_data = client.recv(1024)
        print('服务器:', re_data.decode('utf-8'))
        if re_data.decode('utf-8') == '拜拜':
            break
    else:
        break
client.close()

2.客户端给服务器发送请求,请求成功后,客户端给服务发送‘1’,服务器就给客户端发送一张图片;客户端给服务发送‘2’,服务器就给客户端返回一个json文件中的内容;客户端给服务发送‘3’, 服务器返回’拜拜’,然后断开连接

# 服务器
import socket
server = socket.socket()
server.bind(('10.7.187.68', 8000))
server.listen(100)
print('开始监听')
while True:
    conversation, addr = server.accept()
    while True:
        print('接收到请求:', addr)
        re_data = conversation.recv(1024)
        str1 = re_data.decode('utf-8')
        print('客户端:', str1)
        if str1 != '拜拜':
            if str1 == '1':
                with open('王也.jpg', 'rb') as f:
                    content = f.read()
                conversation.send(content)
            elif str1 == '2':
                import json
                with open('json1.json', encoding='utf-8') as f1:
                    content1 = json.load(f1)
                conversation.send(bytes(content1.encode(encoding='utf-8')))
            elif str1 == '3':
                message = '拜拜'
                conversation.send(bytes(message.encode(encoding='utf-8')))
                break
            else:
                message = input('请输入:')
                conversation.send(message.encode(encoding='utf-8'))
                if message == '拜拜':
                    break
        else:
            break
    conversation.close()

# 客户端
import socket
client = socket.socket()
client.connect(('10.7.187.68', 8000))
while True:
    message = input('请输入需要发送的消息:')
    client.send(message.encode(encoding='utf-8'))
    if message != '拜拜':
        re_data = client.recv(102400000)
        if message == '1':
            with open('接收图片.jpg', 'wb') as f:
                f.write(re_data)
        elif message == '2':
            import json
            str1 = re_data.decode('utf-8')
            js1 = json.dumps(str1, ensure_ascii=False)
            with open('接收信息.json', 'w', encoding='utf-8') as f1:
                f1.write(js1)
        elif re_data.decode('utf-8') == '拜拜':
            print('服务器:', re_data.decode('utf-8'))
            break
        else:
            str1 = re_data.decode('utf-8')
            print('服务器:', str1)
    else:
        break
client.close()

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