/
tftp_server.py
64 lines (54 loc) · 2.16 KB
/
tftp_server.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
import tftpy
import os
import threading
import time
import logging
DEFAULT_PORT = 50000
DEFAULT_ROOT = os.getcwd()+'/resources/received/'
class Server:
"""
Create a Server object that uses the tftpy module to host a TFTP server
"""
def __init__(self, root_folder=DEFAULT_ROOT, port=DEFAULT_PORT):
self.server = tftpy.TftpServer(root_folder)
self.port = port
self.thread = threading.Thread(target=self.server_listen)
def stop(self, now=False):
"""
Stops the server and frees the socket.
:param now: False by default, will wait for processes to end before shutting down.
True if the server needs to be killed immediately.
"""
self.server.stop(now)
def start(self):
"""
Starts the thread which will initiate the server.
"""
self.thread.start()
def server_listen(self):
"""
Start listening to the given port. Will initiate the endless loop and wait for any events.
If the method returns a thread error then the socket was not closed properly the last run.
The socket can be free by manually killing the process.
Process id can be found by using the \'lsof -i UDP\' command
to find the right PID then followed by \'kill PID\'
"""
self.server.listen(listenport=self.port)
def is_running(self):
"""
Checks if the server is currently active.
:return: True if the server is running, False if the server is not.
"""
return self.server.is_running.is_set()
@staticmethod
def set_logging(write_path, level):
"""
Sets the logging level and adds a FileHandler object to write the logging to file.
:param write_path: The path where the file should be written.
:param level: Desired level of logging.
"""
tftpy.setLogLevel(level)
fh = logging.FileHandler(write_path+'log_server_'+time.strftime('%d-%m-%Y_%H:%M:%S'))
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
tftpy.log.addHandler(fh)