-
Notifications
You must be signed in to change notification settings - Fork 0
/
receiver.py
66 lines (54 loc) · 1.28 KB
/
receiver.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import socket
KB = 1024
MB = 1024 * KB
filebuffer = 4 * KB
def push_and_pull(msg):
msg = str(msg)
reply = None
while reply != msg*4:
sock.send(msg)
reply = sock.recv(1*KB)
if not reply:
continue
break
def pull_and_push(e=None):
input = None
while input == None:
input = sock.recv(1*KB)
if not input:
continue
if e != None:
sock.send(str(e)*4)
return
sock.send(input*4)
return input
def getsize(path):
with file(path) as f:
f.seek(0, 2)
return f.tell()
listener = socket.socket()
listener.bind(('', 25565))
listener.listen(5)
sock, addr = listener.accept()
print "accepted", addr
expected = int(pull_and_push())
print "Expected size is", expected
filename = "D:\\test.jpg"
with open(filename, 'wb+') as f:
s = 0
c = 0
trash = None
while s != expected:
c += 1
obj = sock.recv(filebuffer)
if not obj:
continue
f.write(obj)
s += len(obj)
percents = (s*100)/(expected)
if c % 100 == 0:
print "Current:", s, "\t\tExpected:",
print expected, '\t', percents, '%'
push_and_pull(getsize(filename))
print "File recieved and file size checked"
sock.close()