#-*- coding:utf-8 -*-
import socketserver
from module import *
class server:
def __init__(self,request):
self.conn=request
self.conn.sendall(by('欢迎光临大龙FTP!'))
def login(self):
self.user=st(self.conn.recv(1024))
self.conn.sendall(by(''))
self.password=st(self.conn.recv(1024))
if self.user in userdict.keys() and self.password==userdict[self.user]:
self.result='Success'
self.conn.sendall(by('\033[32m登陆成功!\033[0m'))
else:
self.result='Failed'
self.conn.sendall(by('\033[31m登陆失败!\033[0m'))
log(self.user,self.result,'users_log.txt')
return self.result
def register(self):
self.user=st(self.conn.recv(1024))
self.conn.sendall(by(''))
self.password=st(self.conn.recv(1024))
if self.user in userdict.keys():
self.conn.sendall(by('\033[031m注册失败,该用户已存在\033[0m'))
else:
self.conn.sendall(by('\033[032m注册成功!\033[0m'))
userdict[self.user]=self.password
self.userdump()
log(self.user,self.result,'users_log.txt')
def put(self):
self.use=0
self.name=st(self.conn.recv(1024))
self.conn.sendall(by(''))
self.size=int(st(self.conn.recv(1024)))
if os.path.isfile('ftp\\'+self.name):
print('cun zai')
print('ftp\\'+self.name)
self.have=os.path.getsize('ftp\\'+self.name)
self.conn.sendall(by(str(self.have)))
self.choose=st(self.conn.recv(1024))
if self.choose=='':
self.have=0
self.conn.sendall(by(''))
# self.size=int(st(self.conn.recv(1024)))
self.conn.sendall(by(''))
f=open('ftp\\'+self.name,'ab')
while self.size != self.use:
self.line=self.conn.recv(1024)
f.write(self.line)
self.use+=len(self.line)
f.close()
print('wanbi')
log(self.user,'Success','file_log.txt')
if self.choose=='':
self.conn.sendall(by(''))
# self.size=int(st(self.conn.recv(1024)))
self.conn.sendall(by(''))
f=open('ftp\\'+self.name,'ab')
while self.size != self.use:
self.line=self.conn.recv(1024)
f.write(self.line)
self.use+=len(self.line)
f.close()
print('wanbi')
log(self.user,'Success','file_log.txt')
else:
print('no zai')
self.conn.sendall(by(str(0)))
# self.size=int(st(self.conn.recv(1024)))
self.conn.sendall(by(''))
f=open('ftp\\'+self.name,'wb')
while self.size > self.use:
self.line=self.conn.recv(1024)
f.write(self.line)
self.use+=len(self.line)
f.close()
print('wanbi')
log(self.user,'Success','file_log.txt')
def get(self):
self.cmd()
self.path=st(self.conn.recv(1024))
if self.path not in os.listdir('ftp'):
self.conn.sendall(by(''))
else:
self.conn.sendall(by(''))
self.size=os.path.getsize(self.path)
self.conn.sendall(by(str(self.size)))
self.conn.recv(1024)
with open('ftp\\%s'%self.path,'rb') as f :
for line in f:
self.conn.sendall(line)
log(self.user,'Success','file_log.txt')
def cmd(self):
cmd=st(self.conn.recv(1024))
p = os.popen(cmd)
x = p.read()
self.conn.sendall(by(x))
@staticmethod
def userdump():
with open('user.txt','wb') as f:
pickle.dump(userdict,f)
f.close()
class Myserver(socketserver.BaseRequestHandler):
def handle(self):
s=server(self.request)
while True:
opt=st(s.conn.recv(1024))
print(userdict)
if opt =='':
self.result=s.login()
if 'Failed' in self.result:continue
while True:
opt2 = st(s.conn.recv(1024))
if opt2 =='':
print('put')
s.put()
elif opt2=='':
s.cmd()
elif opt2=='':
print('get')
s.get()
elif opt2=='':
break
elif opt=='':
s.register()
elif opt=='':
break if __name__=='__main__':
Server=socketserver.ThreadingTCPServer(('127.0.0.1',8888),Myserver)
Server.serve_forever()
# print(dir(socketserver))

server端程序

#-*- coding:utf-8 -*-
from module import *
import os
import socket
import pickle
import os
import sys
try:
userdict = pickle.load(open('user.txt', 'rb'))
except Exception as e:
userdict = {}
def log(user,result,file):
with open(file,'a') as f:
attime=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
f.write('%s %s %s\n'%(attime,result,user)) class client:
def __init__(self,address,port):
self.obj=socket.socket()
self.obj.connect((address,port))
print(st(self.obj.recv(1024)))
def login(self,user,password):
self.obj.sendall(by(user))
self.obj.recv(1024)
self.obj.sendall(by(password))
self.result=st(self.obj.recv(1024))
print(self.result)
def register(self,user,password):
self.obj.sendall(by(user))
self.obj.recv(1024)
self.obj.sendall(by(password))
self.result=st(self.obj.recv(1024))
print(self.result)
def put(self,path):
self.size=os.path.getsize(path)
name=path.split('\\')[-1]
self.obj.sendall(by(name))
self.obj.recv(10241)
self.obj.sendall(by(str(self.size)))
have=int(st(self.obj.recv(1024)))
if have>=self.size:
print('\033[031m该文件已存在\033[0m!')
self.obj.sendall(by(str(0)))
elif 0<have<self.size:
choose=input('1、断点续传 2、重新传\n请选择:').strip()
self.obj.sendall(by(choose))
if choose=='':have=0
self.obj.sendall(by(str(self.size)))
st(self.obj.recv(1024))
with open(path,'rb') as f :
f.seek(have)
for line in f:
self.obj.sendall(line)
have+=len(line)
schedule(self.size,have)
f.close()
print('\033[032m上传成功!\033[0m')
else:
self.obj.sendall(by(str(self.size)))
st(self.obj.recv(1024))
with open(path,'rb') as f :
f.seek(have)
for line in f:
self.obj.sendall(line)
have+=len(line)
schedule(self.size,have)
f.close()
print('\033[032m上传成功!\033[0m')
def get(self,path):
# self.obj.sendall(by(path))
# self.have=st(self.obj.recv(1024))
# if self.have=='0':
self.size=int(st(self.obj.recv(1024)))
self.obj.sendall(by(''))
self.use=0
if os.path.isfile('get\\'+path):
self.cover=input('\033[031m该文件已存在,是否覆盖?\n\t1、是\t\t2、否\n\033[0m请选择:').strip()
if self.cover=='':
f=open('get\\'+path,'wb')
while self.size != self.use:
self.line=self.obj.recv(1024)
f.write(self.line)
self.use+=len(self.line)
schedule(self.size,self.use)
f.close()
print('\033[032m下载成功\033[0m')
elif self.cover=='':pass
else:print('\033[31m输入无效\033[0m')
else:
f=open('get\\'+path,'wb')
while self.size> self.use:
self.line=self.obj.recv(1024)
f.write(self.line)
self.use+=len(self.line)
schedule(self.size,self.use)
f.close()
print('\033[032m下载成功\033[0m')
# self.obj.recv()
def cmd(self,cmd):
self.obj.sendall(by(cmd))
self.result=st(self.obj.recv(4096))
print('\033[32m%s\033[0m'%self.result)
c=client('127.0.0.1',8888)
while True:
opt=input('请选择: 1、登陆 2、注册 3、退出\n>>>')
c.obj.sendall(by(opt))
if opt=='':
user = input('请输入用户名:')
password = input('请输入密码:')
c.login(user,password)
if '失败' in c.result:continue
while True:
opt2 = input('请选择: 1、上传 2、下载 3、执行命令 4、退出\n>>>')
if opt2=='':
path=input('请输入要上传的文件路径:')
if not os.path.isfile(path):
print('\033[031m输入路径无效!\033[0m')
continue
else:
c.obj.sendall(by(opt2))
c.put(path)
elif opt2=='':
c.obj.sendall(by(opt2))
c.cmd('dir ftp | findstr /v 目录 |findstr /v 驱动器 |findstr /v 序列号|findstr /v DIR')
path=input('请输入要下载的文件名:').strip()
c.obj.sendall(by(path))
have=st(c.obj.recv(1024))
if have=='':
print('\033[031m输入路径无效!\033[0m')
continue
else:c.get(path)
elif opt2=='':
c.obj.sendall(by(opt2))
cmd=input('请输入要执行的命令:')
c.cmd(cmd)
elif opt2=='':break
else:
print('\033[31m输入无效\033[0m ')
continue
elif opt=='':
user = input('请输入用户名:')
password = input('请输入密码:')
c.register(user,password)
elif opt == '':
break
else:
print('\033[31m输入无效\033[0m')
continue

客户端程序

#-*- coding:utf-8 -*-
import socket
import pickle
import os
import sys
import time
try:
userdict = pickle.load(open('user.txt', 'rb'))
except Exception as e:
print(e)
userdict = {}
def log(user,result,file):
with open(file,'a') as f:
attime=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
f.write('%s %s %s\n'%(attime,result,user))
def by(word):
a=bytes(word,encoding = 'utf-8')
return a
def st(word):
b=str(word,encoding = 'utf-8')
return b
def schedule(size,use):
sys.stdout.write("\r")
sys.stdout.write("%s%% | %s" % (int(use / size * 100), int(use / size * 100) * '#'))
sys.stdout.flush()

模板程序

使用socket实现FTP程序的更多相关文章

  1. Python:socket实现ftp程序

    刚开始学习socket编程,还不是特熟练,码了好长时间,中间遇到许多问题,记录一下用socketserver写ftp server端: #!/usr/bin/env python import soc ...

  2. Python开发程序:FTP程序

    作业:开发一个支持多用户在线的FTP程序 要求: 用户加密认证 允许同时多用户登录 每个用户有自己的家目录 ,且只能访问自己的家目录 对用户进行磁盘配额,每个用户的可用空间不同 允许用户在ftp se ...

  3. 用python开发简单ftp程序

    根据alex老师视频开发的简单ftp程序,只能实现简单的get功能 ftp客户端程序: #!/usr/bin/env python #_*_ coding:utf-8 _*_ import socke ...

  4. python之FTP程序(支持多用户在线)

    转发注明出处:http://www.cnblogs.com/0zcl/p/6259128.html 一.需求 1. 用户加密认证 (完成)2. 允许同时多用户登录 (完成)3. 每个用户有自己的家目录 ...

  5. python实现FTP程序

    python实现FTP程序 程序源码 上传功能 查看文件 cd功能 创建目录 程序源码 目录结构 服务端 主程序 import optparse import socketserver import ...

  6. Python3学习之路~8.6 开发一个支持多用户在线的FTP程序-代码实现

    作业: 开发一个支持多用户在线的FTP程序 要求: 用户加密认证 允许同时多用户登录 每个用户有自己的家目录 ,且只能访问自己的家目录 对用户进行磁盘配额,每个用户的可用空间不同 允许用户在ftp s ...

  7. (转)Python开发程序:支持多用户在线的FTP程序

    原文链接:http://www.itnose.net/detail/6642756.html 作业:开发一个支持多用户在线的FTP程序 要求: 用户加密认证 允许同时多用户登录 每个用户有自己的家目录 ...

  8. gevent协程、select IO多路复用、socketserver模块 改造多用户FTP程序例子

    原多线程版FTP程序:http://www.cnblogs.com/linzetong/p/8290378.html 只需要在原来的代码基础上稍作修改: 一.gevent协程版本 1. 导入geven ...

  9. 多用户在线FTP程序

    项目名:多用户在线FTP程序 一.需求 1.用户加密认证 2.允许同时多用户登录 3.每个用户有自己的家目录 ,且只能访问自己的家目录 4.对用户进行磁盘配额,每个用户的可用空间不同 5.允许用户在f ...

随机推荐

  1. [转] python程序的调试方法

    qi09 原文 python程序的调试方法 本文讨论在没有方便的IDE工具可用的情况下,使用pdb调试python程序 源码例子 例如,有模拟税收计算的程序: #!/usr/bin/python de ...

  2. POJ 2828-Buy Tickets(线段树)

    题意: 有n个人,每人有一定的价值,给n个安排,每次安排有两个数 p,v p是这个人前面人的个数 (直接插在第p个人后面其他人后移),v是它的价值,n个安排后 求最终的价值序列. 分析: 越在后面的安 ...

  3. Java 时间转换问题总结

    这几天开发中遇到时间转换出错的问题,特总结如下:   ========================================================================= ...

  4. 【原】Redis事务管理

    Redis高级篇 事务 MULTI, EXEC, DISCARD and WATCH命令用于保证Redis中的事务处理 一个事务中的所有命令被序列化并串行执行. 事务的原子性. 用法 MULTI ...

  5. Linux更改默认jdk

    RHEL默认安装Open JDK--java-1.6.0-openjdk-1.6.0.0-1.66.1.13.0.el6.x86_64,现在要换成Oracle JDK1.7.0_51 1. (可选)删 ...

  6. java@ LinkedList 学习

    package abc.com; import java.util.LinkedList; public class TestLinkedList { static void prt(Object o ...

  7. jQuery each的实现与call方法的详细介绍

    转载原出处: http://www.f2es.com/jquery-each-intro/ 先贴上jquery实现each功能的源代码(把常用的call部分提取出来,为了方便理解,就进行了一定的修改) ...

  8. 【转】简明vim练级攻略

    本文来自:http://coolshell.cn/articles/5426.html vim的学习曲线相当的大(参看各种文本编辑器的学习曲线),所以,如果你一开始看到的是一大堆VIM的命令分类,你一 ...

  9. 【55】让自己熟悉Boost

    1.网址:http://boost.org 2.有很多C++组织和网站,但是Boost库有两个优势:a.和标准委员会关系密切:b.加入C++标准的各种功能的测试场.

  10. VS2012的SVN插件VISUALSVN

    http://www.visualsvn.com/visualsvn/download/