Ejemplo n.º 1
0
 def info_read(name):
     user_dir = os.path.join(settings.user_info, name)
     if os.path.exists(user_dir):
         with open(user_dir, "rb") as f:
             dic = pickle.load(f)
             return dic
     else:
         server_show("user data is empty....", "error")
Ejemplo n.º 2
0
 def login(self):
     user_dic = User.info_read(self.username)
     if user_dic[self.username] == self.password:
         server_show("login success....", "info")
         return True
     else:
         server_show("login error....", "error")
         return False
Ejemplo n.º 3
0
 def login(self, res):
     server_show("Receiving client login request is landing....", "msg")
     name = res[1]
     psd = res[2]
     user = User(name, psd)
     sign = user.login()
     if sign:
         self.request.sendall(bytes("Success", encoding="utf-8"))
     else:
         self.request.sendall(bytes("Failure", encoding="utf-8"))
Ejemplo n.º 4
0
 def register(self, res):
     server_show(
         "Receiving client registration request is being registered....",
         "msg")
     name = res[1]
     psd = res[2]
     user = User(name, psd)
     if user.register():
         self.request.sendall(bytes("Success", encoding="utf-8"))
     else:
         self.request.sendall(bytes("Failure", encoding="utf-8"))
Ejemplo n.º 5
0
 def login(self):
     user_dic = User.info_read(self.name)
     print(user_dic,)
     if user_dic.get(self.name) == self.password:
         server_show("login success....", "info")
         user_dic["dir"] = self.home_path
         User.info_write(self.name, user_dic)
         return True
     else:
         server_show("login error....", "error")
         return False
Ejemplo n.º 6
0
 def upload(self, res):
     server_show(
         "Receive the request from the client to upload the file....",
         "msg")
     name = res[1]
     filename = res[3]
     self.request.sendall(bytes("True", encoding="utf-8"))
     res = int(self.request.recv(1024).decode())
     if User.receive(filename, name, res, self.request):
         self.request.sendall(bytes("True", encoding="utf-8"))
     else:
         self.request.sendall(bytes("False", encoding="utf-8"))
Ejemplo n.º 7
0
 def register(self):
     dic = {}
     dic[self.username] = self.password
     if User.info_write(self.username, dic):
         server_show("register success....", "info")
         os.mkdir(self.home_path)
         os.mkdir("%s/others" % self.home_path)
         with open("%s\empty_file" % self.home_path, "w") as f:
             f.write("empty_file")
         return True
     else:
         server_show("register error....", "error")
         return False
Ejemplo n.º 8
0
 def mkdir(self, con, res_dir):
     user_dic = User.info_read(self.name)
     if user_dic["dir"] == os.path.join(os.path.join(Base_dir, "home"), self.name):
         dir = os.path.join(os.path.join(Base_dir, "home"), self.name)
     else:
         dir = user_dic["dir"]
     next_dir = dir + "/" + res_dir
     if os.path.exists(next_dir):
         server_show("The directory has already existed.....", "error")
     else:
         os.mkdir(next_dir)
         server_show("Directory create success....", "info")
         return True
Ejemplo n.º 9
0
 def register(self):
     dic = {}
     dic[self.name] = self.password
     dic["storage"] = random.randint(10240, 20480)
     dic["dir"] = self.home_path
     if User.info_write(self.name, dic):
         server_show("register success....", "info")
         os.mkdir(self.home_path)
         os.mkdir("%s/others" % self.home_path)
         with open("%s\ofile" % self.home_path, "w") as f:
             f.write("ofile")
         return True
     else:
         server_show("register fail....", "error")
         return False
Ejemplo n.º 10
0
 def cd(self, res):
     server_show("Receive the request from the client 'cd' command", "msg")
     name = res[1]
     psd = res[2]
     user = User(name, psd)
     res = user.cd_command(self.request)
     dirs = str(res)
     self.request.sendall(str(dirs).encode("utf-8"))
     dir1 = self.request.recv(1024).decode()
     if dir1 == "error":
         server_show("client input error", "error")
         self.request.close()
     else:
         sign = user.cd_dir(self.request, dir1, name)
         self.request.sendall(str(sign).encode("utf-8"))
Ejemplo n.º 11
0
 def receive(filename, name, res_length, con):
     con.send("True".encode("utf-8"))
     time.sleep(0.5)
     dir = os.path.join(os.path.join(os.path.join(Base_dir, "home"), name),
                        filename)
     length = 0
     f = open(dir, "wb")
     while length < int(res_length):
         if int(res_length) - length > 1024:
             size = 1024
         else:
             size = int(res_length) - length
         data = con.recv(size)
         length += len(data)
         f.write(data)
     if length == int(res_length):
         time.sleep(0.5)
         server_show("file download success....", "info")
Ejemplo n.º 12
0
 def view(self, res):
     server_show(
         "Receives the client's request to view the current directory file...",
         "msg")
     name = res[1]
     psd = res[2]
     user = User(name, psd)
     dirs, files = user.view_file()
     dir = str(dirs)
     file = str(files)
     if len(dirs) == 0:
         self.request.sendall("False".encode("utf-8"))
     else:
         self.request.sendall(bytes(dir, encoding="utf-8"))
     self.request.sendall(bytes(file, encoding="utf-8"))
     self.request.recv(1024)
     dic = User.info_read(name)
     storage = str(dic["storage"])
     self.request.sendall(bytes(storage, encoding="utf-8"))
     server_show("Current directory file view success...", "info")
Ejemplo n.º 13
0
 def handle(self):
     if self.request.recv(1024) == b'True':
         server_show(
             "Receives the {0} connection request, is communicating....".
             format(self.client_address), "info")
         # try:
         while True:
             self.cmd = self.request.recv(4069)
             if not self.cmd:
                 break
             elif self.cmd == b'':
                 break
             else:
                 data = str(self.cmd.decode(encoding="utf-8"))
                 res = data.split("+")
                 if hasattr(self, res[0]):
                     func = getattr(self, res[0])
                     func(res)
                 else:
                     server_show("wrong action", "error")
Ejemplo n.º 14
0
 def download(self, res):
     server_show(
         "Receive the request from the client to download the file....",
         "msg")
     name = res[1]
     psd = res[2]
     user = User(name, psd)
     dirs, files = user.view_file()
     file = str(files)
     self.request.sendall(bytes(file, encoding="utf-8"))
     res = self.request.recv(1024).decode()
     if User.download_file(res, name, self.request):
         server_show("The file download success....", "info")
     else:
         server_show("The file download failure....", "error")
Ejemplo n.º 15
0
 def receive(filename, name, res, con):
     user_dic = User.info_read(name)
     if user_dic["dir"] == os.path.join(os.path.join(Base_dir, "home"), name):
         dir = os.path.join(os.path.join(os.path.join(Base_dir, "home"), name), filename)
     else:
         dir_name = user_dic["dir"]
         dir = os.path.join(dir_name, filename)
     if res / 1024 < user_dic["storage"]:
         con.sendall("ok".encode("utf-8"))
         length = 0
         f = open(dir, "wb")
         md5 = hashlib.md5()
         while length < res:
             if res - length > 1024:
                 size = 1024
             else:
                 size = res - length
             data = con.recv(size)
             length += len(data)
             md5.update(data)
             f.write(data)
             progressbar(length, res)
         else:
             new_md5 = md5.hexdigest()
             f.close()
         or_md5 = con.recv(1024)
         if new_md5 == or_md5.decode():
             server_show("The file download success....", "info")
             return True
         else:
             server_show("Inconsistency of documents", "error")
             return False
     elif res / 1024 > user_dic["storage"]:
         con.sendall("no".encode("utf-8"))
         server_show("Lack of disk space", "error")
         return False
Ejemplo n.º 16
0
#!/usr/bin/env python
# -*- coding:utf-8 -*-

import os
import sys

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)

from src.main import func
from src.common import server_show

if __name__ == "__main__":
    server_show("|----- Start Select FTP Server -----", "info")
    func()
Ejemplo n.º 17
0
def server_method(con,mask):
    cmd = con.recv(1024)
    if not cmd:
        sel.unregister(con)
        con.close()
    else:
        data = cmd.decode()
        res = data.split("+")
        name = res[1]
        pswd = res[2]
        if res[0] == "login":
            server_show("The request to receive the client landing is landing....", "msg")
            user = User(name, pswd)
            sign = user.login()
            if sign:
                con.sendall(bytes("Success", encoding="utf-8"))
            else:
                con.sendall(bytes("Failure", encoding="utf-8"))
        if res[0] == "register":
            server_show("The request to receive the client register is register....", "msg")
            user = User(name, pswd)
            if user.register():
                con.sendall(bytes("Success", encoding="utf-8"))
            else:
                con.sendall(bytes("Failure", encoding="utf-8"))
        if res[0] == "view":
            server_show("Request to view the list of files under the directory....", "msg")
            user = User(name, pswd)
            if user.view_file():
                con.sendall(bytes("Success", encoding="utf-8"))
            else:
                con.sendall(bytes("Failure", encoding="utf-8"))
        if res[0] == "upload":
            server_show("receiving a request from a client to upload a file....","msg")
            con.send(bytes("True", encoding="utf-8"))
        if res[0] == "uploadfile":
            res_length = res[3]
            filename = res[4]
            User.receive(filename, name, res_length, con)
        if res[0] == "download":
            server_show("receiving a request from a client to download a file....","msg")
            user = User(name, pswd)
            res = str(user.view_file())
            con.sendall(bytes(res, encoding="utf-8"))
        if res[0] == "downloadfile":
            filename = res[3]
            User.download_file(filename, name, con)
            server_show("file download success....", "info")
Ejemplo n.º 18
0
def accept(server,mask):
    server_show("listen in[%s]address[%s]port,waitting connect...." % (settings.HOST, settings.PORT), "info")
    con, addr = server.accept()
    server_show("receive the connection request of the {0},now communication....".format(addr), "info")
    con.setblocking(False)
    sel.register(con, selectors.EVENT_READ, server_method)
Ejemplo n.º 19
0
        file = str(files)
        self.request.sendall(bytes(file, encoding="utf-8"))
        res = self.request.recv(1024).decode()
        if User.download_file(res, name, self.request):
            server_show("The file download success....", "info")
        else:
            server_show("The file download failure....", "error")

    def cd(self, res):
        server_show("Receive the request from the client 'cd' command", "msg")
        name = res[1]
        psd = res[2]
        user = User(name, psd)
        res = user.cd_command(self.request)
        dirs = str(res)
        self.request.sendall(str(dirs).encode("utf-8"))
        dir1 = self.request.recv(1024).decode()
        if dir1 == "error":
            server_show("client input error", "error")
            self.request.close()
        else:
            sign = user.cd_dir(self.request, dir1, name)
            self.request.sendall(str(sign).encode("utf-8"))


if __name__ == '__main__':
    server_show("wait for the client connection....", "info")
    server = socketserver.ThreadingTCPServer(("localhost", settings.PORT),
                                             FTPserver)
    server.serve_forever()