服务器套接字
import socket
import time
# 第一题:
# 写一个客户端和服务器的套接字:
# 客户端连接服务器后展示界面:
# ===========================
# 1. 需要图片
# 2. 需要文字
# 3. 通知结束
# ==========================
# 请选择:
# 如果客户端选1,服务器给客户端发送一张图片,客户端保存图片到本地
# 如果客户端选2, 服务器输入一段文字发送给客户端, 客户端将文字保存在一个message.txt文件中
# 如果客户端选3,通知服务器关闭连接,并且客户端结束
# 定义一个窗口打印
def show_interface():
with open('./interface.txt', 'r', encoding='utf-8') as f:
item = f.read()
return item
server = socket.socket()
server.bind(('10.7.156.118', 1027))
server.listen(520)
while True:
# 接收请求
conversation, address = server.accept()
# 循环
while True:
# 发送界面
message = show_interface()
conversation.send(message.encode('utf-8'))
# 接收消息
data_rec = conversation.recv(1024)
data_decode = data_rec.decode('utf-8')
print('客户端消息(%s): %s' % (address[0][-2:], data_decode))
# 客户端输入1的时候传输图片
if data_decode == '1':
print('对方请求传输图片')
print('正在传输')
with open('./files/king.png', 'br') as f:
content = f.read()
conversation.send(content)
conversation.close()
print('传输完成')
# 客户端输入2的时候给客户端传输文字
elif data_decode == '2':
print("对方请求保存文字")
content = input("输入文字: ")
conversation.send(content.encode('utf-8'))
print('文字已发送')
# 当客户端输入3,给客户端发送断开连接,延时两秒后退出循环
elif data_decode == '3':
print("对方选择结束连接服务器,正在断开连接......")
conversation.send("disconnect".encode('utf-8'))
time.sleep(2)
break
# 延时两秒后服务器关闭
print("正在关闭服务器")
time.sleep(2)
print('服务器已关闭')
break
客户端套接字
"""__author__ = ErYang"""
import socket
import time
client = socket.socket()
client.connect(('10.7.156.118', 1027))
while True:
# 接收消息
data_rec = client.recv(1024)
data_decode = data_rec.decode('utf-8')
print(data_decode)
# 发送消息
message = input("请选择(1-3): ")
while not(1 <= int(message) <= 3):
message = input("请选择(1-3): ")
# 当发出消息为1之后接受的数据存为图片
if message == '1':
print('准备接收图片')
client.send(message.encode('utf-8'))
data_rec = client.recv(1024)
data = bytes()
while data_rec:
data += data_rec
data_rec = client.recv(1024)
with open('./rec.png', 'bw') as f:
f.write(data)
time.sleep(2)
print('图片保存完成')
continue
# 当发出数据为2后接受数据存到rec.txt文件中
elif message == '2':
print('准备接收文字')
client.send(message.encode('utf-8'))
data_rec = client.recv(1024).decode('utf-8')
with open('./rec.txt', 'w', encoding='utf-8') as f:
f.write(data_rec)
time.sleep(2)
print('写入完成')
continue
# 当发出数据为3后,延时两秒打印出接收的服务器消息,延时两秒之后断开连接
elif message == '3':
client.send(message.encode('utf-8'))
print("你选择了结束连接")
time.sleep(2)
print(client.recv(1024).decode('utf-8'))
time.sleep(2)
break
问题二
import re
import requests
import json
# 第二题:
# 请求接口:https://www.apiopen.top/satinApi?type=1&page=1 获取网络数据。
# 将内容中所有的name和text对应的值取出,并且保存到一个json文件中,保存的格式:
# [{“name”:”张三”, “text”:”哈哈,让我们一起自由的飞翔”},
# {“name”:”喒你家玻璃”, “text”:”截图暂停,截到的将会是对你爱情的预言三词!”}]
url = 'https://www.apiopen.top/satinApi?type=1&page=1'
response = requests.get(url)
content = response.text
re_str = r'"name":"[^,]+'
re_str2 = r'"text":"[^,]+'
item = re.findall(re_str, content)
item2 = re.findall(re_str2, content)
# print(item, item2, sep='\n')
list_name = []
list_text = []
list_match = []
for i in item:
name = i.split(':')
list_name.append(name[1][:-1])
for j in item2:
text = j.split(':')
list_text.append(text[1][:-1])
for index_num in range(len(list_name)):
dict_match = {"name": list_name[index_num], "text": list_text[index_num]}
list_match.append(dict_match)
with open('./match.json', 'w', encoding='utf-8') as f:
json.dump(list_match, f)
with open('./match.json', 'r', encoding='utf-8') as f:
print(json.load(f))
网友评论