美文网首页
day17 作业

day17 作业

作者: HavenYoung | 来源:发表于2018-08-07 21:56 被阅读0次
    import socket
    
    ip = '192.168.3.1'
    
    
    def user_input():
        message = input('>>>>')
        if message == '拜拜':
            send_message(message)
            return False
        elif message == '发送图片':
            send_pic()
        else:
            send_message(message)
    
        return True
    
    
    def send_message(message):
        conversation, address = server.accept()
        conversation.send(message.encode())
        conversation.close()
    
    
    def send_pic():
        conversation, address = server.accept()
    
        pic = input('图片路径:')
        print(pic)
    
        with open('./蕾姆酱.jpg', 'rb') as f:
            pic = f.read()
    
        conversation.send(pic)
        conversation.close()
    
    
    def get_message():
        conversation, address = server.accept()
        data = conversation.recv(1024)
        message = data.decode(encoding='utf-8')
        if message == '发送图片':
            get_pic()
        else:
            print(message)
    
    
    def get_pic():
        conversation, address = server.accept()
        data = conversation.recv(1024)
        pic = bytes()
        while data:
            pic += data
            data = conversation.recv(1024)
    
        with open('./pic.jpg', 'wb') as f:
            f.write(pic)
    
    
    if __name__ == '__main__':
        # 1.创建对象
        server = socket.socket()
    
        # 2.绑定地址
        server.bind(('192.168.3.1', 8080))
    
        # 3.监听
        server.listen(50)
    
        while True:
            if user_input():
                pass
            else:
                break
    
            get_message()
    
    
    import socket
    
    ip = '192.168.3.1'
    
    
    def user_input():
        message = input('>>>>')
        if message == '拜拜':
            send_message(message)
            return False
        elif message == '发送图片':
            send_pic()
        else:
            send_message(message)
    
        return True
    
    
    def send_message(message):
        client.connect((ip, 8080))
        client.send(message.encode())
        client.close()
    
    
    def send_pic():
        client.connect((ip, 8080))
    
        pic = input('图片路径:')
        print(pic)
    
        with open('./蕾姆酱.jpg', 'rb') as f:
            pic = f.read()
    
        client.send(pic)
        client.close()
    
    
    def get_message():
        client.connect((ip, 8080))
        data = client.recv(1024)
        message = data.decode(encoding='utf-8')
        if message == '发送图片':
            get_pic()
        else:
            print(message)
    
    
    def get_pic():
        client.connect((ip, 8080))
        data = client.recv(1024)
        pic = bytes()
        while data:
            pic += data
            data = client.recv(1024)
    
        with open('./pic.jpg', 'wb') as f:
            f.write(pic)
    
    
    if __name__ == '__main__':
        # 1.创建套接字
        client = socket.socket()
    
        # 2.连接服务器
        # client.connect((ip, 8080))
    
        while True:
            get_message()
    
            if user_input():
                pass
            else:
                break
    
    

    相关文章

      网友评论

          本文标题:day17 作业

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