栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 后端开发 > Python

基于python/socket客户端服务端图片传输脚本

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基于python/socket客户端服务端图片传输脚本

基于python/socket客户端服务端图片传输脚本

废话不多说上才艺

客户端配置

noinspection PyUnresolvedReferences

import io

noinspection PyUnresolvedReferences

import 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 PyUnresolvedReferences

import io

noinspection PyUnresolvedReferences

import base64 as tt

noinspection PyUnresolvedReferences

from 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命令自己百度。

转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/1038182.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号