怎么推销自己的网站,八桂职教,不花钱做推广的网站,四川遂宁做网站的公司python中使用socket服务发送接收图像的代码#xff0c;可在服务器端中插入模型推理代码进行推理返回结果。 服务器端
# -*-coding:utf-8-*-
import os.path
import socket
import structdef deal_image(sock, addr):print(connection, addr)while True:# 计算文件信息大小fil…python中使用socket服务发送接收图像的代码可在服务器端中插入模型推理代码进行推理返回结果。 服务器端
# -*-coding:utf-8-*-
import os.path
import socket
import structdef deal_image(sock, addr):print(connection, addr)while True:# 计算文件信息大小fileinfo_size struct.calcsize(128sq)# 接收文件信息bufbuf sock.recv(fileinfo_size)if buf:# 解包filename, filesize struct.unpack(128sq, buf)fn filename.decode().strip(\x00)new_filename os.path.join(./, new_ fn)recvd_size 0# 保存图像fp open(new_filename, wb)# 没看董while not recvd_size filesize:if filesize - recvd_size 1024:data sock.recv(1024)recvd_size len(data)else:data sock.recv(1024)recvd_size filesize# 写数据fp.write(data)# 关闭文件fp.close()# 关闭服务sock.close()break# 建立连接
s socket.socket()
host socket.gethostname()
port 12345
s.bind((host, port))
s.listen(5)while True:# 接收连接的地址c, addr s.accept()str hello,world, str(addr)# 发送连接消息以信息流的方式发送c.send(str.encode(encodingutf-8))print(addr)# 处理接收的图像数据deal_image(c, addr)c.close()
客户端
# -*-coding:utf-8-*-
import os.path
import socket
import struct# 初始化客户端建立通信
s socket.socket()
host socket.gethostname()
port 12345
s.connect((host, port))#接收消息
data s.recv(1024).decode(encodingutf-8)
print(data)#发送图片文件头信息
filepath r./123.jpg
fhead struct.pack(b123sq, bytes(os.path.basename(filepath).encode(encodingutf-8)), os.stat(filepath).st_size)
s.send(fhead)#发送图像
fp open(filepath, rb)
while True:data fp.read(1024)if not data:print(send over)breaks.send(data)
# 关闭连接
s.close()