2018-09-12-day18网络基础作业

2018-09-12  本文已影响0人  oxd001

作业:

  1. 客户端和服务器聊天,可以一直聊天,直到一方发送’拜拜’。然后就可以和下一个人一直聊
    客户端
import socket
def create_client():
    client = socket.socket()
    client.connect(('10.7.153.191',8080))
    message1 = input('客户端:')
    while message1!='拜拜':
        client.send(message1.encode())
        message2 = client.recv(1024)
        message2 = message2.decode()
        print('服务端:'+message2)
        if message2 == '拜拜':
            break
        else:
            message1 = input('客户端:')
    else:
        client.send(message1.encode())
        client.close()
            
if __name__ == '__main__':
    create_client()

服务端

import socket
def create_socket():
    server = socket.socket()
    server.bind(('10.7.153.191',8080))
    server.listen(5)
    connect,addr = server.accept()
    while True:   
        message2 = connect.recv(1024)
        print('客户端:'+message2.decode())
        if message2.decode() =='拜拜':
            connect.close()
            connect,addr = server.accept()
            continue
        message1 = input('服务端:')
        if message1 == '拜拜':
            connect.close()
            connect,addr = server.accept()
            continue
        connect.send(message1.encode())
        
if __name__ == '__main__':
    create_socket()
  1. 下载网络图片(
    https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2808438283,4249462766&fm=26&gp=0.jpg)到本地
import requests
if __name__ == '__main__':
    url = 'https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2808438283,4249462766&fm=26&gp=0.jpg'
    response = requests.get(url)
    re = response.content
    with open('lufei.jpg','wb') as f:
        f.write(re)
上一篇下一篇

猜你喜欢

热点阅读