-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.py
128 lines (95 loc) · 2.67 KB
/
client.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import socket #for sockets
import sys #for exit
import threading
import select
import time
import multiprocessing
# create dgram udp socket
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
except socket.error:
print 'Failed to create socket'
sys.exit()
host = raw_input('Enter an IP to connect to : ')
port = 9999;
filename = ""
packetNum = -1
packets = []
tracking = [] # this variable will keep track of the time that
# has passed since each packet left. When that time
# expires it will be sent again.
def main():
global filename
filename = raw_input('Enter name of File: ')
#http://stackoverflow.com/questions/25465792/python-binary-eof
with open(filename, "rb") as infile:
while True:
data = infile.read(32)
if not data:
break
package(data)
appendSize()
sendPackets()
def appendSize():
global packets
for x in xrange(0,len(packets)):
packets[x] = packets[x] + (len(packets),)
def package(d):
global packetNum
packetNum += 1
packets.append((packetNum,filename, d))
tracking.append(True)
def sendPackets():
for packet in packets:
packet = packet + (len(packets),) # adding total number of packets to packet.
# This could not be calculated in advance
# and is guarunteed to be correct if calulated now.
trackit(packet[0])
# s.sendto(str(packet), (host, port))
def trackit(pktNum):
print "sending packet " + str(pktNum)
s.sendto(str(packets[pktNum]), (host, port))
time.sleep(0.1)
while tracking[pktNum]:
for x in xrange(1,10):
time.sleep(0.1)
print "sending packet " + str(pktNum) + " again..."
class listen(threading.Thread):
def __init__(self):
print "Starting listen() thread..."
threading.Thread.__init__(self)
self.running = 1
def run(self):
# create dgram udp socket
try:
leftsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
except socket.error:
print 'Failed to create socket'
sys.exit()
# Bind socket to local host and port
try:
leftsock.bind(('localhost', 1776))
except socket.error , msg:
print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
sys.exit()
while self.running:
global tracking
ready = select.select([leftsock], [], [], 1)
if ready[0]:
d = leftsock.recvfrom(1024)
reply = d[0]
addr = d[1]
tracking[int(reply)] = False
print reply
if reply == "":
self.kill()
print "...listen() stopped."
def kill(self):
self.running = 0
print "Killing listen()..."
if __name__ == '__main__':
listener = listen()
listener.start() # listen for response from server
main()
time.sleep(5)
listener.kill() # kill listener in case it's still running