ftp服务端

#coding=utf-8
import SocketServer
import json
import os

class MyTcpHandler(SocketServer.BaseRequestHandler):
    def handle(self):
        while True:
            try:
                self.data = self.request.recv(1024).strip()
                print '{} wrote:'.format(self.client_address)
                print self.data
                cmd_dict =  json.loads(self.data)
                action = cmd_dict['action']
                if hasattr(self,action):
                    funtion = getattr(self,action)
                    funtion(cmd_dict)
                self.request.send(self.data.upper())
            except Exception as e:
                print 'error',e
                break

    def put(self,*args):
        cmd_dict = args[0]
        filename = cmd_dict['filename']
        filesize = cmd_dict['size']
        if os.path.isfile(filename):
            f = open(filename+'new','wb')
        else:
            f = open(filename,'wb')
        self.request.send('可以上传了')
        received_size= 0
        while received_size < filesize:
            data = self.request.recv(1024)
            f.write(data)
            received_size += len(data)
        else:
            '文件上传完成'



if __name__ == '__main__':
    Host,Port = 'localhost',9999
    server = SocketServer.ThreadingTCPServer((Host,Port),MyTcpHandler)   #ThreadingTCPServer多线程并发 TCPServer为单发的
    server.serve_forever()

 

转载于:https://www.cnblogs.com/qiangayz/p/8613644.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值