標籤:目錄切換 pass 下載 [] 輸入密碼 編程 dom 核心 切換
作業要求
0、實現使用者登陸
1、實現上傳和下載
3、每個使用者都有自己的家目錄,且只可以訪問自己的家目錄
4、對使用者進行磁碟配額,每個使用者的空間不同,超過配額不允許下載和上傳
5、允許使用者在指定的家目錄隨意切換目錄
6、允許使用者在自己的家目錄切換目錄
7、允許上傳和下載檔案,並判斷檔案的一致性
目前還未終稿,還在持續最佳化中
用戶端核心代碼
import socketimport osimport hashlibimport subprocessimport jsonHOST = "127.0.0.1"PORT = 8000ip_bind = (HOST,PORT)ftp_client = socket.socket()ftp_client.connect(ip_bind)while True: client_login = ftp_client.recv(8192) print(str(client_login,encoding="utf-8")) cline_user = input("使用者名稱:") ftp_client.sendall(bytes(cline_user,encoding="utf-8")) client_login = ftp_client.recv(8192) print(str(client_login, encoding="utf-8")) cline_password = input("密碼:") ftp_client.sendall(bytes(cline_password,encoding="utf-8")) ftp_server = ftp_client.recv(8192) # print(str(ftp_server,encoding="utf-8")) if str(ftp_server,encoding="utf-8") == "使用者名稱或者密碼錯誤": print(str(ftp_server,encoding="utf-8")) continue else: print(str(ftp_server, encoding="utf-8")) breakwhile True: client_func = input("你可以查看和切換目錄,你可以上傳和下載檔案:") ftp_client.sendall(bytes(client_func,encoding="utf-8")) while True: server_reply = ftp_client.recv(8192) print(str(server_reply,encoding="utf-8"))
服務端核心代碼
import socketimport hashlibimport subprocessimport jsonimport osimport randomcyr = "G:\\FTP_server\\bob"root = "G:\\FTP_server\\root"user_info = {}with open("E:\\python\\pycharm\\goto_end\\week4_網路編程\\FTP作業\db\\user_data","r",encoding="utf-8") as f: for line in f: temp = line.split("|") temp_dict = {temp[0]:{"密碼":temp[1],"配額":temp[2]}} user_info.update(temp_dict) temp = [] temp_dict = {}HOST = "127.0.0.1"PORT = 8000ip_bind = (HOST,PORT)FTP_server = socket.socket()FTP_server.bind(ip_bind)FTP_server.listen(1)conn,add = FTP_server.accept()while True: conn.sendall(bytes("FTP_server:請輸入使用者名稱",encoding="utf-8")) cline_user = str(conn.recv(8192),encoding="utf-8") conn.sendall(bytes("FTP_server:請輸入密碼",encoding="utf-8")) cline_password = str(conn.recv(8192),encoding="utf-8") if cline_user in user_info.keys(): if cline_password == user_info[cline_user]["密碼"]: welcome = "歡迎使用者[%s]登陸FTP伺服器" %(cline_user) send_info = welcome.center(100,"-") conn.sendall(bytes(send_info,encoding="utf-8")) break else: conn.sendall(bytes("使用者名稱或者密碼錯誤", encoding="utf-8")) continue else: conn.sendall(bytes("使用者名稱或者密碼錯誤",encoding="utf-8")) continueclient_path = "G:\\FTP_server\\" + cline_useros.chdir(client_path)client_pwd = client_pathwhile True: client_func = conn.recv(8192) if str(client_func,encoding="utf-8") == "dir": os.chdir(client_pwd) a = os.listdir(client_pwd) b = [] for i in a: path = client_pwd + "\\" + i if os.path.isdir(path): c = "dir|" + i b.append(c) elif os.path.isfile(path): c = "file|" + i b.append(c) else: pass # print(b) ret = json.dumps(b) conn.sendall(bytes(ret,encoding="utf-8")) elif str(client_func,encoding="utf-8").startswith("cd"): # print(str(client_func,encoding="utf-8")) # print(client_path) # print(client_pwd) if str(client_func,encoding="utf-8").strip() == "cd": os.chdir(client_path) s = "切換成功" conn.sendall(bytes(s, encoding="utf-8")) client_pwd = client_path # print(client_pwd) else: cd_func = str(client_func, encoding="utf-8").split(" ") cd_path = client_pwd + "\\" + cd_func[1] if os.path.exists(cd_path): if os.path.isdir(cd_path): os.chdir(cd_path) s = "切換成功" conn.sendall(bytes(s, encoding="utf-8")) client_pwd = cd_path else: s = "請輸入正確的目錄資訊" conn.sendall(bytes(s, encoding="utf-8")) else: s = "該目錄不存在" conn.sendall(bytes(s,encoding="utf-8")) elif str(client_func,encoding="utf-8").startswith("get"): a = str(client_func,encoding="utf-8").split(" ") # print(client_pwd + a[1]) if os.path.exists(client_pwd + "\\" + a[1]): if os.path.isdir(client_pwd + a[1]): s = "請輸入正確的目錄資訊" conn.sendall(bytes(s, encoding="utf-8")) elif os.path.isfile(client_pwd + "\\" + a[1]): with open(client_pwd + "\\" + a[1],"rb") as file: for line in file: conn.sendall(line) else: s = "該路徑不存在" conn.sendall(bytes(s, encoding="utf-8")) elif str(client_func,encoding="utf-8").startswith("put"): pass else: s = "錯誤的輸入,請重新輸入" conn.sendall(bytes(s,encoding="utf-8"))
python之ftp作業【還未完成】