-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrfile.py
73 lines (59 loc) · 1.43 KB
/
rfile.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
import sys
import socket
import os
import math
import ipaddress
# read arguments
filename = sys.argv[1]
host_ip = sys.argv[2]
port = int(sys.argv[3])
print(filename)
#check what we got: ip or hostname, then take ip
try:
ipaddress.ip_address(host_ip)
except:
try:
host_ip = socket.gethostbyname(host_ip)
except:
print("Incorrect hostname or IP")
# create the client socket
s = socket.socket()
print(f"[+] Connecting to {host_ip}:{port}")
s.connect((host_ip, port))
print("[+] Connected.")
# check if filename exists
if filename:
print("filename:", filename)
else:
print("No file")
exit()
file_loc = filename
# send filename
s.send(f"{filename}".encode())
#get filesize
try:
filesize = os.path.getsize(file_loc)
print("filesize:", filesize)
except:
print("No such file")
exit()
Ok = s.recv(1024).decode('utf-8')
# ensure server got size
if(Ok != 'Ok'):
print("Error")
exit()
# send file
with open(file_loc, "rb") as f:
count = 0
while count < filesize:
# read the bytes from the file
bytes_read = f.read(1024)
count += len(bytes_read)
if not bytes_read:
break
s.sendall(bytes_read)
# update the progress bar
print("\r" + "#" * math.floor(60 * count / filesize) + "-" * (60 - math.floor(60 * count / filesize)) +
"|" + str(math.floor(100 * count / filesize)) + "%|", end="")
print()
s.close()