-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTCPFileTransfer_Recevie.py
More file actions
95 lines (60 loc) · 1.74 KB
/
TCPFileTransfer_Recevie.py
File metadata and controls
95 lines (60 loc) · 1.74 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
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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import socket
import json
import hashlib
import progressbar, time
import sys
# In[2]:
def print_head(head):
print("File name:",head['fname'])
print("File size:",head['fsize'])
print("Hash value",head['hash'])
# In[3]:
def hash_file(filename):
sha1 = hashlib.sha1()
with open(filename, 'rb') as fh:
while True:
data = fh.read(1024)
if not data:
break
sha1.update(data)
return sha1.hexdigest()
# In[ ]:
arginput = sys.argv
#arginput = ['r','9777']
#usage run.py newfilename port
if(len(arginput)==2):
port = int(arginput[1])
else :
port = 9753
main_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
main_socket.bind(("",port))
main_socket.listen(5)
while True:
recv_file_sock, recv_file_addr = main_socket.accept()
head = recv_file_sock.recv(256)
head = json.loads(head)
print_head(head)
recv_file_sock.send("Confirmed".encode(encoding='UTF-8'))
newfilename = input("Save as:")
f = open(newfilename,"wb")
sizecnt = 0
pgbar = progressbar.ProgressBar(widgets=[progressbar.Bar(),progressbar.Percentage(),
' (', progressbar.SimpleProgress(), ') ',],max_value=head['block'])
#pgbar.start()
for i in range(head['block']):
content = recv_file_sock.recv(4096)
f.write(content)
time.sleep(0.000000001)
pgbar.update(i)
sizecnt += len(content)
f.close()
if(sizecnt == head['fsize'] and hash_file(newfilename)==head['hash']):
print("Hash verified ", head['hash'])
print("File saved as: ",newfilename)
recv_file_sock.close()
# In[ ]:
recv_file_sock.close()
# In[ ]: