18作 網路基礎

weixin_34308389發表於2018-09-13

作業:

  1. 客戶端和伺服器聊天,可以一直聊天,直到一方傳送’拜拜’。然後就可以和下一個人一直聊

服務端:

import socket

def creat_server():
while True:
server = socket.socket()

    server.bind(("192.168.0.104", 8088))



    server.listen(512)


    while True:
        print('監聽狀態')

        connect, addr = server.accept()
        print(addr)


        while True:
            message = input('伺服器:')

            connect.send(message.encode())
            # connect.send(bytes('吃飯了嗎?', 'utf-8'))


            recv_data = connect.recv(1024)
            if message == '拜拜':
                continue
            # print(recv_data.decode())
            print(str(recv_data, 'utf-8'))
        connect.close()

客戶端:

import socket


def creat_client():

    client = socket.socket()





    client.connect(('192.168.0.104', 8088))

    while True:

        data = client.recv(1024)
        print(data.decode(encoding='utf-8'))



        message = input('客戶端:')

        client.send(message.encode())
        if message == '拜拜':
            continue
    client.close()
creat_client()
  1. 下載網路圖片(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)
all_data = response.content

with open('new.jpg','wb')as f:
    f.write(all_data)

結果圖片:


13693123-55b467a6b3ff9939.jpg
new.jpg

相關文章