def logout(self, data): logging.debug(self.user.name + " logout.") # self.send_msg("U logout") self.send_msg("disconnect") UserHandler.stop_activity(self.user.name) self.conn.close() return -1
def __init__(self, conn_, adr_): self.conn = conn_ self.adr = adr_ msg = self.read_protokol(conn_) print(msg) try: login, password = msg.split() except ValueError: print("Bad par") self.send_msg("killed") conn_.shutdown(1) return perm = UserHandler.check_user(login, password) if perm >= 0: logging.debug("User " + adr_[0] + ":" + str(adr_[1]) + " successful login " + login) Path("./users/" + login).mkdir(parents=True, exist_ok=True) self.user = User(adr_, login, perm) self.start() else: logging.debug("User " + adr_[0] + ":" + str(adr_[1]) + " failed login " + login) if perm == -1: self.send_msg("wrong data") elif perm == -2: self.send_msg("user already use") self.send_msg("killed") conn_.shutdown(1)
def read_protokol(self, conn_): try: size = int(conn_.recv(10).decode("utf-8")) except ConnectionResetError: UserHandler.stop_activity(self.user.name) logging.debug(self.user.name + " logout.") return -1 data = "" print("size:", size) while size > 0: newdata = conn_.recv(size if size <= 1024 else 1024).decode( "utf-8") size -= 1024 print("read:", newdata) data = data + newdata print("i'm read: " + data) return data
def change_pass(self, data): if len(data.split()) != 3: self.send_msg("bad arg") return a, login, newpass = data.split() if UserHandler.pasasword_change(login, newpass): self.send_msg("ok") else: self.send_msg("err") pass
def del_user(self, data): if len(data.split()) != 2: self.send_msg("bad arg") return a, login = data.split() if UserHandler.delete_user(login): self.send_msg("ok") else: self.send_msg("err") pass
def reg_user(self, data): if len(data.split()) != 3: self.send_msg("bad arg") return a, login, password = data.split() if UserHandler.create_user(login, password): self.send_msg("ok") else: self.send_msg("err") pass
def start(self): self.commands["logout"] = [0, self.logout, "- разлогиниться."] self.commands["ls"] = [ 0, self.ls, " [dirname] - посмотреть информацию о директории." ] self.commands["help"] = [ 0, self.help, " [command] - помощь по команде" ] self.commands["pwd"] = [0, self.pwd, " - текущая директория"] self.commands["mkdir"] = [ 1, self.mkdir, " <dirname> - создать директорию с именем dirname." ] self.commands["write"] = [ 1, self.write, " <file> [text] - записать в file следующий текст. " "Если файл не существует, то он будет содан." ] self.commands["read"] = [1, self.read, " <file> - считать файл."] # self.commands["reg"] = [ 2, self.reg_user, " <login> <password> - зарегистрировать." ] self.commands["del"] = [ 2, self.del_user, " <login> - удалить пользователя." ] self.commands["passchange"] = [ 2, self.change_pass, " <login> <password> - заменить текущий пароль пользователя " "на предоставленный." ] self.commands["list"] = [ 2, self.list_user, " - информация о пользователях." ] UserHandler.set_activity(self.user.name) self.send_msg("successful login!") while 1: result = self.handler() if result == -1: break
def list_user(self, data): self.send_msg(UserHandler.list()) pass
from server import UserHandler from server.CommandHandler import CommandHandler port = 5050 sock = socket.socket() sock.bind(('', port)) sock.listen() users = [] logging.basicConfig( format='%(asctime)s [Partenios Server] [%(levelname)s] %(message)s', level=logging.DEBUG, datefmt='%H:%M:%S') logging.info("Server is running on the port " + str(port)) UserHandler.init() threads = {} while 1: conn, adr = sock.accept() logging.info("User connect " + adr[0] + str(adr[1])) my_thread = threading.Thread(target=CommandHandler, args=( conn, adr, )) my_thread.start() threads[adr[1]] = my_thread