2018-09-12课后作业(day18)

# 作业:
# 1. 客户端和服务器聊天,可以一直聊天,直到一方发送’拜拜’。然后就可以和下一个人一直聊
# 服务器
import socket
def creat_sever():
    while True:
        sever = socket.socket()
        sever.bind(('10.7.153.102',8081))
        sever.listen(520)
        while True:
            connect,addr = sever.accept()
            while True:
                message = input('服务器:')
                if message != '拜拜':
                    connect.send(message.encode())

                elif message == '拜拜':
                    connect.send(message.encode())
                    break

                recv_data = connect.recv(1024)
                print(recv_data.decode())
            connect.close()

if __name__ == '__main__':
    creat_sever()

#客户端
import socket
def client_side():
    while True:
        client = socket.socket()
        client.connect(('10.7.153.100',8090))
        while True:
            data = client.recv(512)
            print(data.decode(encoding='utf-8'))
            message = input('客户端:')
            if message != '拜拜':
                client.send(message.encode())
            elif message == '拜拜':
                client.send(message.encode())
                break
        client.close()
if __name__ == '__main__':
    client_side()




# 2. 下载网络图片(https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2808438283,4249462766&fm=26&gp=0.jpg)到本地
import requests
def downloads(pic):
    download = requests.get(pic)
    with open('./files/魏世强.jpg','wb') as f:
        f.write(download.content)
if __name__ == '__main__':
    downloads('https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2808438283,4249462766&fm=26&gp=0.jpg')
download.jpg

你可能感兴趣的:(2018-09-12课后作业(day18))