新手入门
- 积分
- 8
- 金钱
- 8
- 注册时间
- 2019-12-26
- 在线时间
- 2 小时
|
1金钱
我用网络调试助手能很完整的收到字节流数据,但Python收到的缓冲打印显示夹杂很多乱七八糟的字符,我不知道应该如何将其变为jpg格式的图片,希望大家能给我一点启发,谢谢大家。
下面是我的接收代码
#coding=utf-8
import socket, time, struct, os, threading
#import picprocess #图像处理模块
host = '192.168.43.105' #localhost
port = 8080
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 定义socket类型
s.bind((host, port)) # 绑定需要监听的Ip和端口号,tuple格式
s.listen(1)
#modell = picprocess.readmodel() #图像识别参照模型
def conn_thread(connection, address):
connection.settimeout(10)
print("准备接收")
while True:
try:
buf = connection.recv(52*1024)
if buf:
filepath = './rubbish.txt' #修改为图片所要保存的路径
file = open(filepath,'wb')
file.write(buf)
file.close()
print(buf)
except socket.timeout:
print('超时')
break
connection.close()
print('通信结束')
def main():
#初始化
while True:
connection, address = s.accept()
print('Connected by ', address)
thread = threading.Thread(target=conn_thread, args=(connection, address)) # 使用threading也可以
thread.start()
# threading.start_new_thread(conn_thread, (connection, address))
s.close()
if __name__ == '__main__':
main()
|
|