day19-homework

作業(yè):

第一題:

寫(xiě)一個(gè)客戶端和服務(wù)器的套接字:
客戶端連接服務(wù)器后展示界面:

===========================

  1. 需要圖片
  2. 需要文字
  3. 通知結(jié)束
    ==========================
    請(qǐng)選擇:

如果客戶端選1,服務(wù)器給客戶端發(fā)送一張圖片,客戶端保存圖片到本地
如果客戶端選2, 服務(wù)器輸入一段文字發(fā)送給客戶端, 客戶端將文字保存在一個(gè)message.txt文件中
如果客戶端選3,通知服務(wù)器關(guān)閉連接,并且客戶端結(jié)束
server

import socket
import threading

def func(addr1,conversation1):
    while True:
        message_re = conversation1.recv(1024).decode('utf-8')
        if message_re == '1':
            with open('./Django遷移.png','rb') as f:
                img = f.read()
            conversation1.send(img)
        elif message_re == '2':
            text = input('請(qǐng)輸入:')
            conversation1.send(text.encode('utf-8'))
        elif message_re == '3':
            conversation.close()
            exit()
        else:
            pass


server = socket.socket()
server.bind(("10.7.156.108",10020))
server.listen(10)
while True:
    conversation, addr = server.accept()
    threading.Thread(target=func, args=(addr, conversation)).start()

client

import socket

client = socket.socket()
client.connect(("10.7.156.108",10020))
while True:
    with open('./1-view.txt','rb') as f:
        print(f.read().decode('utf-8'))
    option = input("請(qǐng)輸入操作碼:")
    if option == '1':
        client.send('1'.encode('utf-8'))
        message = client.recv(1024000)
        with open('./00.png','wb') as f:
            f.write(message)
        print('圖片下載完成!')
    elif option == '2':
        client.send('2'.encode('utf-8'))
        message = client.recv(1024000).decode('utf-8')
        with open('./message.txt','wb') as f:
            f.write(message.encode('utf-8'))
        print('文本保存完成!')
    elif option == '3':
        client.send('3'.encode('utf-8'))
        break
    else:
        print('操作碼錯(cuò)誤!!!')
    # client.send(input('>>>').encode('utf-8'))
    # message = client.recv(1024).decode("utf-8")
    # if message != '':
    #     print(message)

第二題:
請(qǐng)求接口:https://www.apiopen.top/satinApi?type=1&page=1 獲取網(wǎng)絡(luò)數(shù)據(jù)。
將內(nèi)容中所有的name和text對(duì)應(yīng)的值取出,并且保存到一個(gè)json文件中,保存的格式:

[{“name”:”張三”, “text”:”哈哈,讓我們一起自由的飛翔”}, {“name”:”喒你家玻璃”, “text”:”截圖暫停,截到的將會(huì)是對(duì)你愛(ài)情的預(yù)言三詞!”}]

import requests
import json

response = requests.get('https://www.apiopen.top/satinApi?type=1&page=1')
text = json.loads(response.text)
# print(text)
res = []
for i in text['data']:
    res.append({'name':i['name'],'text':i['text']})
with open('./data.json','w') as f:
    json.dump(res,f)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,981評(píng)論 19 139
  • 1.ios高性能編程 (1).內(nèi)層 最小的內(nèi)層平均值和峰值(2).耗電量 高效的算法和數(shù)據(jù)結(jié)構(gòu)(3).初始化時(shí)...
    歐辰_OSR閱讀 29,643評(píng)論 8 265
  • 點(diǎn)擊查看原文 Web SDK 開(kāi)發(fā)手冊(cè) SDK 概述 網(wǎng)易云信 SDK 為 Web 應(yīng)用提供一個(gè)完善的 IM 系統(tǒng)...
    layjoy閱讀 13,946評(píng)論 0 15
  • 1、通過(guò)CocoaPods安裝項(xiàng)目名稱項(xiàng)目信息 AFNetworking網(wǎng)絡(luò)請(qǐng)求組件 FMDB本地?cái)?shù)據(jù)庫(kù)組件 SD...
    陽(yáng)明AGI閱讀 16,018評(píng)論 3 119
  • while循環(huán)(javascript) 代碼 分析 這個(gè)和for循環(huán)的要素是一樣的。只不過(guò)寫(xiě)的順序不一樣罷了。
    鴨梨山大哎閱讀 124評(píng)論 0 1