server.py(服务器端)
"""__author__zhangdong__"""
import socket
import re
from requests import request
import way
if __name__ == '__main__':
# 1.创建对象
server = socket.socket()
# 2.绑定地址
server.bind(('10.7.181.97', 9090))
# 3.监听
server.listen(50)
# 保持服务器不关闭
while True:
conversation, addr = server.accept()
print(addr)
# 让客户端和服务器一直处于连接的状态
while True:
# 发送消息
message = input('>>>')
judge = re.match(r'[.][/]\d+',message)
judge2 = re.match(r'\w',message)
if judge:
# 发送一张图片
with open(message, 'rb') as f:
data = f.read()
conversation.send(data)
conversation.close()
# elif judge2:
#
else:
conversation.send(message.encode())
# 接收消息
message_data = conversation.recv(1024)
judge = re.match(r'[.][/]\d+',message_data)
judge2 = re.match(r'\w', message_data)
if judge:
way.picture(message_data,server)
elif judge2:
way.data(message_data)
else:
way.eccept(message_data, server)
client.py(客户端)
"""__author__zhangdong__"""
import socket
import re
from requests import request
import way
if __name__ == '__main__':
# 1.创建套接字对象
client = socket.socket()
# 2.连接服务器
client.connect(('10.7.181.86', 8060))
while True:
# 发送消息
message = input('>>>')
judge = re.match(r'[.][/]\d+', message)
judge2 = re.match(r'\w', message)
if judge:
# 发送一张图片
with open(message, 'rb') as f:
data = f.read()
client.send(data)
client.close()
# elif judge2:
#
else:
client.send(message.encode())
# 接收消息
message_data = client.recv(1024)
judge = re.match(r'[.][/]\d+', message_data)
judge2 = re.match(r'\w', message_data)
if judge:
way.picture(message_data,client)
elif judge2:
way.data(message_data)
else:
way.eccept(message_data, client)
way.py
"""__author__zhangdong__"""
from requests import request
def picture(message_data,server):
"""保存图片"""
image_data = bytes() # 创建一个空的bytes用来保存整个图片数据
# data = server.recv(1024)
while message_data:
image_data += message_data
message_data = server.recv(1024)
# 保存图片到本地
with open('./image.png', 'wb') as f:
f.write(image_data)
server.close()
def data(message_data):
"""保存数据"""
# 保存数据到本地
responce = request('GET', message_data)
print(type(responce), responce)
# 以字符串的形式获取响应体(服务器返回的数据)
text = responce.text
print(type(text), text)
with open('./aaa.text', 'w') as f:
content = f.write(text)
def eccept(message_data,client):
if message_data == '拜拜':
client.close()
else:
print(message_data.decode(encoding='utf-8'))
网友评论