简单ftp—文件上传

一、客户端

 1 # Author:David Liu 
 2 # Be Happy!
 3 
 4 import socket
 5 import hashlib,os,json
 6 
 7 class FtpClient(object):
 8     def __init(self,client):
 9         self.client=socket.socket()
10     def help(self):
11         print('''
12         请按照以下格式操作:
13         put
14         get
15 16         '''
17         )
18     def connect(self,ip,port):
19         self.client = socket.socket()
20         self.client.connect((ip, port))
21     def interactive(self):
22         #self.authenticate()
23         while True:
24             cmd=input(">>:").strip()
25             if len(cmd)==0:continue
26             cmd_str=cmd.split()[0]
27             if hasattr(self,'cmd_%s'%cmd_str):
28                 func=getattr(self,'cmd_%s'%cmd_str)
29                 func(cmd)
30             else:
31                 self.help()
32     def cmd_put(self,*args):
33         cmd_split=args[0].split()
34         if len(cmd_split)>1:
35             filename=cmd_split[1]
36             if os.path.isfile(filename):
37                 filesize=os.stat(filename).st_size
38                 msg_dic={
39                     'action':'put',
40                     'filename':filename,
41                     'size':filesize
42                 }
43                 self.client.send(json.dumps(msg_dic).encode())
44                 #为了防止粘包,等服务器确认
45                 server_response=self.client.recv(1024)
46                 f=open(filename,'rb')
47                 for line in f:
48                     self.client.send(line)
49                 else:
50                     print(filename,'upload succeed!')
51                     f.close()
52             else:
53                 print('[%s] is not exit'%filename)
54     def cmd_get(self,*args):#下载还没写
55         pass
56 ftp=FtpClient()
57 ftp.connect('localhost',9999)
58 ftp.interactive()

二、服务器端

 1 # Author:David Liu 
 2 # Be Happy!
 3 import socketserver,json,os
 4 
 5 class MyTCPHandler(socketserver.BaseRequestHandler):
 6     def put(self,*args):
 7         '''接收客户端文件'''
 8         cmd_dic=args[0]
 9         filename=cmd_dic['filename']
10         filesize=cmd_dic['size']
11         if os.path.isfile(filename):
12             f=open(filename+'new','wb')
13         else:
14             f = open(filename, 'wb')
15 
16         self.request.send(b"200 ok")
17         received_size=0
18         while received_size<filesize:
19             data=self.request.recv(1024)
20             f.write(data)
21             received_size+=len(data)
22         else:
23             print('file [%s] has uploaded'%filename)
24 
25     def handle(self):
26         #和客户端所有的交互都在这里面写
27         while True:
28             try:
29                 self.data=self.request.recv(1024).strip()
30                 print("{} wrote:".format(self.client_address[0]))
31                 print(self.data.decode())
32                 cmd_dic=json.loads(self.data.decode())
33                 action=cmd_dic['action']
34                 if hasattr(self,action):
35                     func=getattr(self,action)
36                     func(cmd_dic)
37 
38                 if not self.data:
39                     print(self.client_address,"客户端已断开")
40                     break
41                 self.request.send(self.data.upper())
42             except ConnectionResetError as e:
43                 print("客户端已断开>>--",e)
44                 break
45 
46 if __name__=="__main__":
47     HOST,PORT="localhost",9999
48     server=socketserver.ThreadingTCPServer((HOST,PORT),MyTCPHandler)
49     server.serve_forever()

注:用于下载的get方法还没有写

2019年5月24日

转载于:https://www.cnblogs.com/David0207AlwaysHappy/p/10917616.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值