2018-09-12 网络基础作业

  1. 客户端和服务器聊天,可以一直聊天,直到一方发送’拜拜’。然后就可以和下一个人一直聊
import socket
def creat_server():
    """写一个服务器"""
    #创建套接字对象
    server = socket.socket()
    #绑定IP地址和端口
    server.bind(("10.7.153.123",8088))
    #开始监听(监听客户端的请求)
    """listen(最大个数)"""
    server.listen(512)
    while True:
        connect,addr = server.accept()
        while True:
            message = input("服务器:")
            connect.send = (message.encode)
            if message == "拜拜":
                break
            recv_dada = connect.recv(1024)
            print(str(recv_data,"utf8"))
            if recv_data == "拜拜":
                break

        connect.close()
creat_server()
import socket
def creat_client():
    client = socket.socket()

    client.connect(("10.7.153.123",8080))
    # number = 0
    while True:
        data = client.recv(1024)
        receive_data = data.decode(encoding="utf-8")
        print(receive_data)
        if receive_data == "拜拜":
            break
        massage = input("客户端:")
        client.send(massage.encode())
        if massage == "拜拜":
            break
    client.close()
# creat_client()
  1. 下载网络图片([图片上传失败...(image-1df561-1536835967749)]

https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2808438283,4249462766&fm=26&gp=0.jpg)到本地

import requests

url = "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2808438283,4249462766&fm=26&gp=0.jpg"
response = requests.get(url)
with open("./files/load.jpg","wb")as f:
    f.write(response.content)
load.png

你可能感兴趣的:(2018-09-12 网络基础作业)