-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
44 lines (36 loc) · 1.27 KB
/
server.py
File metadata and controls
44 lines (36 loc) · 1.27 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
import socket
from key import HOST,PORT
target = '' #file path of your locally stored file
def start_ftp_server(host, port, target):
"""
RECIEVES A FILE FROM A CLIENT ON PORT, STORES IT AT TARGET FILE LOCATION ON THE SERVER
Arguments:
host (str): Server IP address
port (int): Port used by the server
target (str): File path where the files are stored
"""
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((host, port))
server_socket.listen()
print(1)
try:
while True:
client_socket, client_addr = server_socket.accept()
with client_socket:
with open(target, 'a') as file:
while True:
data = client_socket.recv(1024).decode('utf-8')
if not data or data == "END":
break
file.write(data)
print("File recieved")
client_socket.sendall(b"1")
except KeyboardInterrupt:
print("Server shutting down")
server_socket.close()
except Exception as e:
print("Error has occured")
print(e)
finally:
server_socket.close()
start_ftp_server(HOST, PORT, target)