forked from cxinping/PythonFullStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileClient2.py
More file actions
30 lines (26 loc) · 1015 Bytes
/
Copy pathfileClient2.py
File metadata and controls
30 lines (26 loc) · 1015 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# -*- coding: utf-8 -*-
import socket
import os
cSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
cSocket.connect(('127.0.0.1', 5000))
# Step1: 获得接收文件的大小
serverResponse = cSocket.recv(1024).decode('utf8')
fileTotalSize = int(serverResponse)
print("fileTotalSize={0}".format(fileTotalSize))
# Step2:发送确认信息
cSocket.send("ready to recv file".encode("utf8"))
# 初始化接收大小
revivedSize = 0
fileName = 'photo3.jpg'
with open(fileName, 'wb') as file:
# Step3: 判断是否已经接收完文件,对接收文件大小和接收文件总大小进行比较
while revivedSize < fileTotalSize:
# 配置5KB 缓存
data = cSocket.recv(1024 * 50)
# 接收文件大小
revivedSize = revivedSize + len(data)
#print("revivedSize={0},data={1}".format(revivedSize,len(data)) )
file.write(data)
else:
print("fileTotalSize={0},revivedSize={1}".format(fileTotalSize, revivedSize))
print('===recv file ok===')