-
Notifications
You must be signed in to change notification settings - Fork 0
/
transmitter.py
72 lines (58 loc) · 1.34 KB
/
transmitter.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
67
68
69
70
71
72
import socket
import Tkinter as tk
import tkFileDialog
KB = 1024
MB = 1024 * KB
filebuffer = 4 * KB
# host = '78.132.190.8'
host = 'vort3.ddns.net'
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()
sock = socket.socket()
sock.connect_ex((host, 25565))
print "connected to", host
root = tk.Tk()
root.withdraw()
filename = tkFileDialog.askopenfilename()
size = getsize(filename)
push_and_pull(size)
print 'size confirmed', size
with open(filename, 'rb', filebuffer) as obj:
c = 0
part = obj.read(filebuffer)
s = len(part)
while part != "":
c += 1
sock.send(part)
percents = (s*100)/(size)
if c % 100 == 0:
print "Current:", s, "\t\tExpected:",
print size, '\t', percents, '%'
part = obj.read(filebuffer)
s += len(part)
print "sent"
pull_and_push(size)
print "file size confirmed"
sock.close()