python scoket、SocketServer简单实现文件上传下载
生活随笔
收集整理的這篇文章主要介紹了
python scoket、SocketServer简单实现文件上传下载
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
1.描述 實(shí)現(xiàn)任何位置文件下載到客戶端執(zhí)行的當(dāng)前目錄下 實(shí)現(xiàn)任何位置文件上傳到服務(wù)端執(zhí)行的當(dāng)前目錄下服務(wù)端: #!/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 #定義當(dāng)前目錄 current_dir?=?os.getcwd() #定義一個(gè)類 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的一個(gè)方法???????????????????????????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":?#上傳文件保存到當(dāng)前目錄下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#實(shí)例化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)? #定義當(dāng)前目錄 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() 運(yùn)行服務(wù)端 python?EddyFtp_server.py? get?connection?from?:?('127.0.0.1',?56051)? 運(yùn)行客戶端 python?EddyFtp_client.py? >>put?/tmp/b.pdf server?ready,?now?client?sending?file~~ send?file?success!轉(zhuǎn)載于:https://my.oschina.net/eddylinux/blog/551441
總結(jié)
以上是生活随笔為你收集整理的python scoket、SocketServer简单实现文件上传下载的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 查看oracle连接客户端
- 下一篇: 开机启动--注册表