日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python scoket、SocketServer简单实现文件上传下载

發布時間:2024/1/17 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python scoket、SocketServer简单实现文件上传下载 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

1.描述 實現任何位置文件下載到客戶端執行的當前目錄下 實現任何位置文件上傳到服務端執行的當前目錄下服務端: #!/usr/bin/env?python #-*-?coding:?utf-8?-*- #?@Date????:?2015-12-23?23:24:32 #?@Author??:?eddy?(278298125@qq.com) #?@Link????:?http://my.oschina.net/eddylinux #?@Version?:?1.0import?SocketServer?? import?time? import?os #定義當前目錄 current_dir?=?os.getcwd() #定義一個類 class?EddyFtpserver(SocketServer.BaseRequestHandler):?#定義接收文件方法def?recvfile(self,?filename):?print?"starting?reve?file!"f?=?open(filename,?'wb')?self.request.send('ready')?while?True:?data?=?self.request.recv(4096)?if?data?==?'EOF':?print?"recv?file?success!"breakf.write(data)?f.close()?#定義放送文件方法?????????????????????????????????????def?sendfile(self,?filename):?print?"starting?send?file!"self.request.send('ready')?time.sleep(1)?f?=?open(filename,?'rb')?while?True:?data?=?f.read(4096)?if?not?data:?breakself.request.sendall(data)?f.close()?time.sleep(1)?self.request.send('EOF')?print?"send?file?success!"#SocketServer的一個方法???????????????????????????def?handle(self):?print?"get?connection?from?:",self.client_address?while?True:?try:?data?=?self.request.recv(4096)?print?"get?data:",?data????if?not?data:?print?"break?the?connection!"break????????????????else:?action,?filename?=?data.split()?#判斷上傳if?action?==?"put":?#上傳文件保存到當前目錄下filename?=?current_dir?+?'/'?+?os.path.split(filename)[1]self.recvfile(filename)?#判斷下載elif?action?==?'get':?self.sendfile(filename)??else:?print?"get?error!"continueexcept?Exception,e:?print?"get?error?at:",e?if?__name__?==?"__main__":?host?=?'localhost'?port?=?8888#實例化s?=?SocketServer.ThreadingTCPServer((host,port),?EddyFtpserver)?s.serve_forever()?客戶端? #!/usr/bin/env?python #?-*-?coding:?utf-8?-*- #?@Date????:?2015-12-23?23:24:53 #?@Author??:?eddy?(278298125@qq.com) #?@Link????:?http://my.oschina.net/eddylinux #?@Version?:?1.0import?socket? import?time? import?os ip?=?'localhost' port?=?8888 s?=?socket.socket(socket.AF_INET,?socket.SOCK_STREAM)? #定義當前目錄 current_dir?=?os.getcwd() def?recvfile(filename):?print?filenameprint?"server?ready,?now?client?rece?file~~"f?=?open(filename,?'wb')?while?True:?data?=?s.recv(4096)?if?data?==?'EOF':?print?"recv?file?success!"breakf.write(data)?f.close()? def?sendfile(filename):?print?"server?ready,?now?client?sending?file~~"f?=?open(filename,?'rb')?while?True:?data?=?f.read(4096)?if?not?data:?breaks.sendall(data)?f.close()?time.sleep(1)?s.sendall('EOF')?print?"send?file?success!"def?confirm(s,?client_command):?s.send(client_command)?data?=?s.recv(4096)?if?data?==?'ready':?return?Truetry:?s.connect((ip,port))?while?1:?client_command?=?raw_input(">>")?if?not?client_command:?continueaction,?filename?=?client_command.split()?if?action?==?'put':?if?confirm(s,?client_command):?sendfile(filename)?else:?print?"server?get?error!"elif?action?==?'get':?if?confirm(s,?client_command):?print?current_dirprint?filenamefilename?=?current_dir?+?'/'?+?os.path.split(client_command)[1]print?filenamerecvfile(filename)?else:?print?"server?get?error!"else:?print?"command?error!" except?socket.error,e:?print?"get?error?as",e? finally:?s.close() 運行服務端 python?EddyFtp_server.py? get?connection?from?:?('127.0.0.1',?56051)? 運行客戶端 python?EddyFtp_client.py? >>put?/tmp/b.pdf server?ready,?now?client?sending?file~~ send?file?success!


轉載于:https://my.oschina.net/eddylinux/blog/551441

總結

以上是生活随笔為你收集整理的python scoket、SocketServer简单实现文件上传下载的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。