Python實(shí)現(xiàn)的FTP通信客戶端與服務(wù)器端功能示例
本文實(shí)例講述了Python實(shí)現(xiàn)的FTP通信客戶端與服務(wù)器端功能。分享給大家供大家參考,具體如下:
一 代碼
1、服務(wù)端代碼
import socket import threading import os import struct #用戶賬號(hào)、密碼、主目錄 #也可以把這些信息存放到數(shù)據(jù)庫中 users = {'zhangsan':{'pwd':'zhangsan1234', 'home':r'c:\python 3.5'}, 'lisi':{'pwd':'lisi567', 'home':'c:\\'}} def server(conn,addr, home): print('新客戶端:'+str(addr)) #進(jìn)入當(dāng)前用戶主目錄 os.chdir(home) while True: data = conn.recv(100).decode().lower() #顯示客戶端輸入的每一條命令 print(data) #客戶端退出 if data in ('quit', 'q'): break #查看當(dāng)前文件夾的文件列表 elif data in ('list', 'ls', 'dir'): files = str(os.listdir(os.getcwd())) files = files.encode() conn.send(struct.pack('I', len(files))) conn.send(files) #切換至上一級(jí)目錄 elif ''.join(data.split()) == 'cd..': cwd = os.getcwd() newCwd = cwd[:cwd.rindex('\\')] #考慮根目錄的情況 if newCwd[-1] == ':': newCwd += '\\' #限定用戶主目錄 if newCwd.lower().startswith(home): os.chdir(newCwd) conn.send(b'ok') else: conn.send(b'error') #查看當(dāng)前目錄 elif data in ('cwd', 'cd'): conn.send(str(os.getcwd()).encode()) elif data.startswith('cd '): #指定最大分隔次數(shù),考慮目標(biāo)文件夾帶有空格的情況 #只允許使用相對(duì)路徑進(jìn)行跳轉(zhuǎn) data = data.split(maxsplit=1) if len(data) == 2 and os.path.isdir(data[1]) \ and data[1]!=os.path.abspath(data[1]): os.chdir(data[1]) conn.send(b'ok') else: conn.send(b'error') #下載文件 elif data.startswith('get '): data = data.split(maxsplit=1) #檢查文件是否存在 if len(data) == 2 and os.path.isfile(data[1]): conn.send(b'ok') fp = open(data[1], 'rb') while True: content = fp.read(4096) #發(fā)送文件結(jié)束 if not content: conn.send(b'overxxxx') break #發(fā)送文件內(nèi)容 conn.send(content) if conn.recv(10) == b'ok': continue fp.close() else: conn.send(b'no') #無效命令 else: pass conn.close() print(str(addr)+'關(guān)閉連接') #創(chuàng)建Socket,監(jiān)聽本地端口,等待客戶端連接 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind(('', 10600)) sock.listen(5) while True: conn, addr = sock.accept() #驗(yàn)證客戶端輸入的用戶名和密碼是否正確 userId, userPwd = conn.recv(1024).decode().split(',') if userId in users and users[userId]['pwd'] == userPwd: conn.send(b'ok') #為每個(gè)客戶端連接創(chuàng)建并啟動(dòng)一個(gè)線程,參數(shù)為連接、客戶端地址、客戶主目錄 home = users[userId]['home'] t = threading.Thread(target=server, args=(conn,addr,home)) t.daemon = True t.start() else: conn.send(b'error')
2、客戶端代碼
import socket import sys import re import struct import getpass def main(serverIP): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((serverIP, 10600)) userId = input('請(qǐng)輸入用戶名:') #使用getpass模塊的getpass()方法獲取密碼,不回顯 userPwd = getpass.getpass('請(qǐng)輸入密碼:') message = userId+','+userPwd sock.send(message.encode()) login = sock.recv(100) #驗(yàn)證是否登錄成功 if login == b'error': print('用戶名或密碼錯(cuò)誤') return #整數(shù)編碼大小 intSize = struct.calcsize('I') while True: #接收客戶端命令,其中##>是提示符 command = input('##> ').lower().strip() #沒有輸入任何有效字符,提前進(jìn)入下一次循環(huán),等待用戶繼續(xù)輸入 if not command: continue #向服務(wù)端發(fā)送命令 command = ' '.join(command.split()) sock.send(command.encode()) #退出 if command in ('quit', 'q'): break #查看文件列表 elif command in ('list', 'ls', 'dir'): loc_size = struct.unpack('I', sock.recv(intSize))[0] files = eval(sock.recv(loc_size).decode()) for item in files: print(item) #切換至上一級(jí)目錄 elif ''.join(command.split()) == 'cd..': print(sock.recv(100).decode()) #查看當(dāng)前工作目錄 elif command in ('cwd', 'cd'): print(sock.recv(1024).decode()) #切換至子文件夾 elif command.startswith('cd '): print(sock.recv(100).decode()) #從服務(wù)器下載文件 elif command.startswith('get '): isFileExist = sock.recv(20) #文件不存在 if isFileExist != b'ok': print('error') #文件存在,開始下載 else: print('downloading.', end='') fp = open(command.split()[1], 'wb') while True: print('.', end='') data = sock.recv(4096) if data == b'overxxxx': break fp.write(data) sock.send(b'ok') fp.close() print('ok') #無效命令 else: print('無效命令') sock.close() if __name__ == '__main__': if len(sys.argv) != 2: print('Usage:{0} serverIPAddress'.format(sys.argv[0])) exit() serverIP = sys.argv[1] if re.match(r'^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}$', serverIP): main(serverIP) else: print('服務(wù)器地址不合法') exit()
二 運(yùn)行結(jié)果
客戶端運(yùn)行結(jié)果
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python URL操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
Python計(jì)算質(zhì)數(shù)的方法總結(jié)
質(zhì)數(shù)(Prime?Number)是指大于1且只能被1和自身整除的正整數(shù),計(jì)算質(zhì)數(shù)是數(shù)論中的一個(gè)經(jīng)典問題,本文將介紹python中多種計(jì)算質(zhì)數(shù)的方法,希望對(duì)大家有所幫助2023-11-11python 實(shí)現(xiàn)在txt指定行追加文本的方法
下面小編就為大家分享一篇python 實(shí)現(xiàn)在txt指定行追加文本的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-04-04Python3和pyqt5實(shí)現(xiàn)控件數(shù)據(jù)動(dòng)態(tài)顯示方式
今天小編就為大家分享一篇Python3和pyqt5實(shí)現(xiàn)控件數(shù)據(jù)動(dòng)態(tài)顯示方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-12-12python中JSON數(shù)據(jù)格式的詳細(xì)使用教程
這篇文章主要給大家介紹了關(guān)于python中JSON數(shù)據(jù)格式的詳細(xì)使用,JSON是一種用于存儲(chǔ)和交換數(shù)據(jù)的語法,JSON是文本,使用JavaScript對(duì)象表示法編寫,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-02-02Python開發(fā)游戲自動(dòng)化后臺(tái)腳本的實(shí)現(xiàn)
本文主要介紹了Python開發(fā)游戲自動(dòng)化后臺(tái)腳本的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01python自帶tkinter庫實(shí)現(xiàn)棋盤覆蓋圖形界面
這篇文章主要為大家詳細(xì)介紹了python自帶tkinter庫實(shí)現(xiàn)棋盤覆蓋圖形界面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07python利用標(biāo)準(zhǔn)庫如何獲取本地IP示例詳解
這篇文章主要給大家介紹了關(guān)于python利用標(biāo)準(zhǔn)庫如何獲取本地IP的相關(guān)資料,文中先對(duì)python的標(biāo)準(zhǔn)庫進(jìn)行了簡單的介紹,而后給大家詳細(xì)介紹了關(guān)于python用標(biāo)準(zhǔn)庫獲取本地IP的實(shí)現(xiàn)方法,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11