美文网首页
2018-11-28 Day18-homework

2018-11-28 Day18-homework

作者: DingUKE | 来源:发表于2018-11-28 20:53 被阅读0次

    作业:

    1. 客户端和服务器聊天,可以一直聊天,直到一方发送’拜拜’。
      然后就可以和下一个人一直聊

    (服务器)

    import socket
    server = socket.socket()
    server.bind(('10.7.187.72', 8086))
    server.listen(1024)
    while True:
        conversation, addr = server.accept()
        print('接受到请求!')
        while True:
            recv_data = conversation.recv(1024)
            print('客户端:', recv_data.decode('utf-8'))
            if recv_data.decode('utf-8') == '拜拜':
                break
            message = input('服务器:')
            conversation.send(message.encode('utf-8'))
            if message == '拜拜':
                break
    

    (客户端)

    import socket
    
    client = socket.socket()
    client.connect(('10.7.187.72', 8086))
    while True:
        message = input('客户端:')
        client.send(message.encode('utf-8'))
        if message == '拜拜':
            break
        recv_data = client.recv(1024)
        print('客户端:', recv_data.decode('utf-8'))
        if recv_data.decode('utf-8') == '拜拜':
            break
    

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

    (服务器)

    import socket
    server = socket.socket()
    server.bind(('10.7.187.72', 8100))
    server.listen(1024)
    while True:
        conversation, addr = server.accept()
        print('接收到请求!')
        while True:
            recv_data = conversation.recv(1024)
    
            if recv_data.decode('utf-8') == '1':
                with open('./okok.jpg', 'rb') as f:
                    content = f.read()
                    conversation.send(content)
                    print('客户端', recv_data.decode('utf-8'))
            elif recv_data.decode('utf-8') == '2':
                with open('./1234.json', 'r', encoding='utf-8') as f1:
                    json1 = f1.read()
                    print(json1)
                    conversation.send(json1.encode('utf-8'))
                    print('客户端','2')
            elif recv_data.decode('utf-8') == '3':
                conversation.send('拜拜'.encode('utf-8'))
                break
    

    (客户端)

    import socket
    import json
    
    def get_picture():
        data = bytes()
        while True:
            re_data = client.recv(1024)
            data += re_data
            with open('./图片.jpg', 'bw') as f:
                f.write(data)
    def json1_get():
        re_data = client.recv(1024)
        json1 = re_data.decode('utf-8')
        print(json1)
        with open('./接受.json', 'w') as f:
            json.dump(json1, f)
        print('2(1)')
    
    client = socket.socket()
    client.connect(('10.7.187.72', 8100))
    
    while True:
        message = input('客户端:')
        client.send(message.encode('utf-8'))
        data = bytes()
        # while True:
        #     print('2')
        if message == '1':
            print('接受图片!')
            get_picture()
        if message == '2':
            print('接受json文件')
            json1_get()
        if message == '3':
            re_data = client.recv(1024)
            print('服务器:', re_data.decode('utf-8'))

    相关文章

      网友评论

          本文标题:2018-11-28 Day18-homework

          本文链接:https://www.haomeiwen.com/subject/ylxtcqtx.html