用python开发简单ftp程序

根据alex老师视频开发的简单ftp程序,只能实现简单的get功能

ftp客户端程序:

#!/usr/bin/env  python
#_*_ coding:utf-8 _*_
import socket
host = "192.168.100.100"
port = 20000
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect((host,port))
while True:
    cmd = raw_input('input cmd:').strip()
    if len(cmd) ==0:continue
    s.sendall(cmd)
    if cmd.split()[0] =="get":
        print "开始传输文件..."
        with open(cmd.split()[1],'wb') as f:
            while True:
                data = s.recv(1024)
                if data == "FileTransferDone":break
                f.write(data)
        print "文件传输完成..."
        continue
    else:
        print s.recv(1024)
s.close()

ftp服务器端程序:

#!/usr/bin/env python
#_*_ coding:utf-8 _*_
import SocketServer,commands,time

class MyTcpHandler(SocketServer.BaseRequestHandler):
    def handle(self):
        while True:
            self.data = self.request.recv(8192).strip()
            #print "{} wrote:".format(self.client_address[0])
            if not self.data:
                print "client %s is dead!" % self.client_address[0]
                break
            user_input = self.data.split()
            if user_input[0] =='get':
                with open(user_input[1],'rb') as f:
                    self.request.sendall(f.read())
                time.sleep(0.5)
                self.request.send('FileTransferDone')
                continue
            cmd_status,result = commands.getstatusoutput(self.data)
            if len(result.strip()) !=0:
                self.request.sendall(result)
            else:
                self.request.sendall("DONE")

转载于:https://www.cnblogs.com/pengyc/p/5374220.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值