python之ftp作業【還未完成】

來源:互聯網
上載者:User

標籤:目錄切換   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作業【還未完成】

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.