-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
63 lines (56 loc) · 1.65 KB
/
Copy pathserver.py
File metadata and controls
63 lines (56 loc) · 1.65 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
import os
import socket
import optparse
import tempfile
tmpDir = str(tempfile.mkdtemp())
parser = optparse.OptionParser()
parser.add_option("-p", dest="port", help="port to listen on")
parser.add_option("-o", dest="output", help="directory to save files")
(options, arguments) = parser.parse_args()
if not options.port:
parser.error("Must specify a port!")
if options.output:
save_dest = options.output
else:
save_dest = None
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("0.0.0.0", int(options.port)))
start = 0
filedata = []
error_code = 0
print("Waiting for files...")
try:
while True:
server.listen(1)
conn, address = server.accept()
while True:
data = conn.recv(4096)
if not data:
break
filedata.append(data)
if data.decode() == "ERROR: 477224891":
error_code = 1
conn.close()
if error_code == 1:
print("ERROR: Client side error sending file!")
else:
filename = filedata.pop(0)
output = save_dest + "/" + filename.decode() if save_dest != None else filename.decode()
tmpFile = tmpDir + "/" + filename.decode()
with open(tmpFile, "wb") as file:
for line in filedata:
file.write(line)
filedata = []
with open(tmpFile, 'r') as fin:
data = fin.read().splitlines(True)
with open(tmpFile, 'w') as fout:
fout.writelines(data[1:])
with open(tmpFile, 'r') as fin:
data = fin.read().splitlines(True)
with open(tmpFile, 'w') as fout:
fout.writelines(data[:-1])
os.system(f"base64 -d {tmpFile} > {output}")
print(f"{filename.decode()} recieved from {address[0]} on port {address[1]}")
except KeyboardInterrupt:
print("\nStopping...")
exit()