废话不多说上才艺
客户端配置
noinspection PyUnresolvedReferencesimport io
noinspection PyUnresolvedReferencesimport base64 as tt
def picture2base(path):
with open(path, ‘rb’) as img:
# 使用base64进行编码
b64encode = tt.b64encode(img.read())
s = b64encode.decode()
base64 = ‘data:image/jpeg;base64,%s’ % s
# 返回base64编码字符串
return base64
import socket #导入socket 模块
c = socket.socket() #创建socket对象
host = ‘127.0.0.1’ #设置本地主机
port = 48888 #设置端口号
c.connect((host,port))
print(“已连接服务端”)
imgPath = “C:Users86182Desktopp1.jpg”
sdata = picture2base(imgPath)
print(f"开始发送图片 {imgPath}")
c.send(sdata.encode())
c.close()
print(“发送完成”)
服务端配置
noinspection PyUnresolvedReferencesimport io
noinspection PyUnresolvedReferencesimport base64 as tt
noinspection PyUnresolvedReferencesfrom PIL import Image as to
def base2picture(resbase64):
res=resbase64.split(‘,’)[1]
img_b64decode = tt.b64decode(res)
ibg = io.BytesIO(img_b64decode)
# noinspection PyUnusedLocal
itg = to.open(ibg)
itg.save(“C:Users86182Desktopp2.jpg”)
import socket #导入socket模块
s = socket.socket() #创建socket对象
host = ‘127.0.0.1’ #设置本地主机
port = 48888 #设置端口
s.bind((host,port)) #绑定端口
s.listen(6) #开始监听,等待客户连接
print(“等待客户端连接…”)
while True:
conn,addr = s.accept() #建立客户连接
print(“客户端已经连接”)
print(“开始接收”)
base64_data = “”
while True:
rdate = conn.recv(2048)
base64_data += str(rdate, ‘utf-8’)
if not rdate:
break;
base2picture(base64_data)
print(base64_data)
###备注一下,启动顺序是先启动服务端,后启动客户端,图片路径根据自己的图片路径进行配置,出现主机什么问题的时候,可以尝试换个端口,或者可以查看端口是否被占用,把端口杀掉就可以了。Windows命令自己百度。