Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/Server'
Browse files Browse the repository at this point in the history
  • Loading branch information
Nakama3942 committed Oct 8, 2023
2 parents 58d6f02 + 6c0e999 commit 17273e3
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
2 changes: 2 additions & 0 deletions FTPServerPyWinCon/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
### PyCharm ###
.idea/
2 changes: 2 additions & 0 deletions FTPServerPyWinCon/compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
pyinstaller --onefile --name serv --console main.py
117 changes: 117 additions & 0 deletions FTPServerPyWinCon/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import os
import logging
import keyboard
import threading
import argparse

from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import ThreadedFTPServer

class Server:
def __init__(self, username, password, homedir, perm, log_levels):
self.__username = username
self.__password = password
self.__homedir = homedir
self.__perm = perm
self.__log_levels = log_levels

self.__authorizer = DummyAuthorizer()
self.__handler = FTPHandler
self.__server = None

self.__starter = None
self.__stopper = None

self.__start()

def __start(self):
self.__build()
self.__log()
self.__run()
self.__expectant()

def __build(self):
self.add_user(
self.__username,
self.__password,
self.__homedir,
self.__perm
)

self.__handler.authorizer = self.__authorizer

self.__handler.banner = "pyftpdlib основанный на ftpd."

self.__server = ThreadedFTPServer(('192.168.0.102', 21), self.__handler)

self.__server.max_cons = 256
self.__server.max_cons_per_ip = 5

def __log(self):
# Настройка логгирования
logger = logging.getLogger()
match self.__log_levels:
case "info":
logger.setLevel(logging.INFO)
case "debug":
logger.setLevel(logging.DEBUG)
case "warning":
logger.setLevel(logging.WARNING)
case "error":
logger.setLevel(logging.ERROR)
case "critical":
logger.setLevel(logging.CRITICAL)
case "fatal":
logger.setLevel(logging.FATAL)
case "notset":
logger.setLevel(logging.NOTSET)

formatter = logging.Formatter('[%(levelname)s %(asctime)s] %(message)s', datefmt='%Y-%m-%d %H:%M:%S')

# Создание обработчика для вывода в консоль
console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)
console_handler.encoding = 'utf-8'
logger.addHandler(console_handler)

# Создание обработчика для записи в файл
file_handler = logging.FileHandler('pyftpd.log', encoding="utf-8")
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)

def __run(self):
self.__starter = threading.Thread(target=self.__server.serve_forever)
self.__starter.start()

def __expectant(self):
while True:
event = keyboard.read_event()
if event.event_type == keyboard.KEY_DOWN:
match event.name:
case "esc":
logging.shutdown()
self.__server.close_all()
break
case "a":
self.add_user(
input("Enter the user name: "),
input("Enter the user password: "),
input("Enter the user home directory: "),
input("Enter the user permissions: "),
)

def add_user(self, username, password, homedir, perm):
self.__authorizer.add_user(username, password, homedir, perm)


if __name__ == '__main__':
parser = argparse.ArgumentParser(description='FTP Server')
parser.add_argument('-u', '--username', default='user', required=False, help='Username')
parser.add_argument('-p', '--password', default='12345', required=False, help='Password')
parser.add_argument('-d', '--homedir', default='.', required=False, help='Home directory')
parser.add_argument('-l', '--log', default='info', required=False, help='Logger levels')

args = parser.parse_args()

serv = Server(args.username, args.password, args.homedir, 'elradfmwMT', args.log)

0 comments on commit 17273e3

Please sign in to comment.