-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
138 lines (131 loc) · 2.82 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
129
130
131
132
133
134
135
136
137
138
import socket
import os
from datetime import datetime
import hashlib
def close_server():
timel = datetime.now().strftime("%I:%M%p %B %d, %Y")
log.write("------- Connection Closed at " + timel + " -------\n")
log.close()
s.close()
exit(0)
def file_download(args, filename, flag):
s.send(args)
data = s.recv(1024)
if flag != "UDP" and flag != "TCP":
print "Wrong Arguments"
print "Format FileDownload <TCP/UDP> <file_name>"
return
if data != "received":
print data
return
if flag == "UDP":
nport = int(s.recv(1024))
ncs = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
addr = (host, nport)
ncs.sendto("received", addr)
try:
f = open(filename, "wb+")
except:
print "Insufficient Privileges or Space"
return
while True:
data, addr = ncs.recvfrom(1024)
if data == "done":
break
f.write(data)
ncs.sendto("received", addr)
f.close()
ncs.close()
elif flag == "TCP":
try:
f = open(filename, "wb+")
except:
print "Insufficient Privileges or Space"
return
while True:
data = s.recv(1024)
if data == "done":
break
f.write(data)
s.send("received")
f.close()
hash1 = s.recv(1024)
f = open(filename, 'rb')
orig_hash = hashlib.md5(f.read()).hexdigest()
if hash1 != orig_hash:
# print hash,orig_hash
print "File Sent Failed"
else:
s.send("sendme")
data = s.recv(1024)
print
print data
print "md5hash: ", hash1
print "Successfully Downloaded"
def receive_data(inp):
try:
s.send(inp)
except:
print "Error in Connection"
return
while True:
try:
data = s.recv(1024)
except:
print "Error in Connection"
close_server()
break
if data == "done":
break
try:
s.send("received")
except:
print "Connection Error"
close_server()
print data
return
#making a socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#
host = raw_input("Host ip: ")
port = input("PORT: ")
down = raw_input("Download Folder: ")
if not os.path.exists(down):
print "No Such Folder"
exit(0)
elif not os.access(down, os.W_OK):
print "No Privilleges"
exit(0)
else:
os.chdir(down)
try:
log = open("client_log.log", "a+")
except:
print "Cannot Open Log file"
exit(0)
try:
s.connect((host, port))
except:
print "No available server found on given address"
s.close()
exit(0)
cnt = 0
print "Connection Established"
time = datetime.now().strftime("%I:%M%p %B %d, %Y")
log.write("------- Connected to " + host + " at " + time + " -------\nCommands Sent:\n")
while True:
cnt += 1
args = raw_input("Enter Command: ")
inp = args.split()
log.write(str(cnt) + ". " + args + "\n")
if len(inp) == 0 or inp[0] == "close":
s.send(args)
print "Bye"
close_server()
elif inp[0] == "IndexGet" or inp[0] == "FileHash":
receive_data(args)
elif inp[0] == "FileDownload":
file_download(args, " ".join(inp[2:]), inp[1])
else:
print "Invalid Command"
s.close()