欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

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

发布时间:2024/1/17 python 40 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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简单实现文件上传下载的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。