2018-10-24作业

作业:
第一题:
写一个客户端和服务器的套接字:
客户端连接服务器后展示界面:
===========================
1. 需要图片
2. 需要文字
3.通知结束
==========================
请选择:
如果客户端选1,服务器给客户端发送一张图片,客户端保存图片到本地
如果客户端选2,服务器输入一段文字发送给客户端, 客户端将文字保存在一个message.txt文件中
如果客户端选3,通知服务器关闭连接,并且客户端结束

#Author ddz
import socket
server= socket.socket()
server.bind(("10.7.156.143",8888))
server.listen(66)
def photo():
with open('./q1.png','br')as f:
content= f.read()
conversation.send(content)
conversation.close()
while 1:
print("111111111")
conversation, addr= server.accept()
data= https://www.it610.com/article/conversation.recv(1024)
data= https://www.it610.com/article/int(data)
if data=https://www.it610.com/article/= 1:
conversation.send('1'.encode('utf-8'))
photo()
else:
conversation.send('2'.encode('utf-8'))
str1= input("服务端:")
conversation.send(str1.encode('utf-8'))
客户端:
import socket
client= socket.socket()
client.connect(("10.7.156.143",8888))
def web():
print("===========================\n\
1. 需要图片\n\
2. 需要文字\n\
3.通知结束\n\
==========================")
while 1:
web()
i= input("请选择(1-3):")
if i== 3:
break
if not i in [1,2,3]:
print('请输入真确指令:')
continue
client.send(i.encode('utf-8'))
j= int(client.recv(1024))
if j== 1:
message_re= client.recv(1024)
data= https://www.it610.com/article/bytes()
while message_re:
data+= message_re
message_re= client.recv(1024)
with open('./new.jpg','bw')as f:
f.write(data)
elif j== 2:
message_re= client.recv(1024)
print(message_re.decode('utf-8'))


【2018-10-24作业】第二题:
请求接口:
https://www.apiopen.top/satinApi?type=1&page=1
获取网络数据。
将内容中所有的name和text对应的值取出,并且保存到一个json文件中,保存的格式:
[{“name”:”张三”, “text”:”哈哈,让我们一起自由的飞翔”}, {“name”:”喒你家玻璃”, “text”:”截图暂停,截到的将会是对你爱情的预言三词!”}]


#Author ddz
import requests,json
str= 'https://www.apiopen.top/satinApi?type=1&page=1'
r1= requests.get(str)
data= https://www.it610.com/article/r1.json()
list1= []
for din data['data']:
dict1= {}
dict1['name']= d['name']
dict1['text']= d['text']
list1.append(dict1)
with open('./ddd.json','w',encoding='utf-8')as f:
json.dump(list1, f)

    推荐阅读