用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")
上一篇:基于C语言libvirt API简单小程序


下一篇:Windows安装Ubuntu桌面操作系统到移动硬盘中以及错误解决