tcp 文件传输
客户端提交图片,服务端接收图片,并将其保存(保存的图片名为当前时间)
客户端
1 from socket import * 2 import datetime 3 4 with open('1.JPG','rb') as rimg: 5 img=rimg.read() 6 7 8 9 10 11 12 connect_img=socket() 13 connect_img.connect(('127.0.0.1', 8848)) 14 15 connect_img.send(img) 16 17 18 connect_img.close()
服务端
from socket import * import datetime server_img=socket(AF_INET,SOCK_STREAM) server_img.bind(('0.0.0.0',8848)) server_img.listen(5) print("等待连接") connfd,addr=server_img.accept() print("CNNENT FROM:",addr) print('写入图片') with open(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')+'.jpg','wb+') as wimg: while True: data = connfd.recv(1024) if not data: break wimg.write(data) connfd.close() server_img.close()
因为一次并不能接受完客户端的内容所以循环进行接收和写入
python