-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtogether.py
More file actions
64 lines (48 loc) · 1.35 KB
/
Copy pathtogether.py
File metadata and controls
64 lines (48 loc) · 1.35 KB
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
from downloader import Downloader
from threading import Timer
import socket
import json
PORT = 1111
CHECKTIME = 5
BUFF = 1000000000
# GLOBAL TODO: make this call in functions!
sock = {}
newDownload = {}
def requestDownload():
global newDownload
global sock
# Send data
message = json.dumps({
'request': 'imReady'
})
sock.sendall(message)
data = sock.recv(BUFF)
print data
data = json.loads(data)
if data['type'] == 'noNeed':
Timer( CHECKTIME, checkStatus ).start()
elif data['type'] == 'download':
newDownload = Downloader( data['url'], data['startRange'], data['endRange'], data['partNum'] )
newDownload.start()
Timer( CHECKTIME, checkStatus ).start()
else:
print 'error!'
def checkStatus():
global newDownload
status = newDownload.status()
print json.dumps(status)
if status['inDownload'] == True:
Timer( CHECKTIME, checkStatus ).start()
else:
requestDownload()
def main():
global sock
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect the socket to the port where the server is listening
server_address = ('localhost', PORT)
print 'connecting to %s port %s' % server_address
sock.connect(server_address)
requestDownload()
if __name__ == "__main__":
main()