#-*- 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. HDU 4081 Qin Shi Huang's National Road System 最小生成树

    分析:http://www.cnblogs.com/wally/archive/2013/02/04/2892194.html 这个题就是多一个限制,就是求包含每条边的最小生成树,这个求出原始最小生成 ...

  2. HOG:从理论到OpenCV实践

    (转载请注明出处:http://blog.csdn.net/zhazhiqiang/ 未经允许请勿用于商业用途) 一.理论 1.HOG特征描述子的定义:     locally normalised ...

  3. [娱乐]GameMaker绘制参数方程的图像

    今天,我翻了旧物,硬着头皮看了这源码.突然恍然大悟,这岂不就是当年学的参数方程! 目前,最早开始教授参数方程实在高三时,并作为一门选修课程,简化了求解圆锥曲线方程的难度,在高考中也很容易拿分,考试过后 ...

  4. ASP.NET_验证控件(class0620)

    为什么使用验证控件 当需要让用户输入数据时,用户有可能输入不符合我们程序逻辑要求的信息,所以我们要对输入进行验证. 客户端验证(用户体验,减少服务器端压力) 服务器端验证(防止恶意攻击,客户端js很容 ...

  5. C++ 继承之虚继承与普通继承的内存分布

    仅供互相学习,请勿喷,有观点欢迎指出~ class A { virtual void aa(){}; }; class B : public virtual A { ]; //加入一个变量是为了看清楚 ...

  6. Turn Your Raspberry Pi Into a WiFi Hotspot with Edimax Nano USB EW-7811Un (RTL8188CUS chipset)

    http://www.daveconroy.com/turn-your-raspberry-pi-into-a-wifi-hotspot-with-edimax-nano-usb-ew-7811un- ...

  7. A Tour of Go Interfaces

    An interface type is defined by a set of methods. A value of interface type can hold any value that ...

  8. [OC Foundation框架 - 2] NSString 的创建

    A. 不可变字符串 void stringCreate() { //Don't need to release memory by this way NSString *str1 = @"S ...

  9. 转载 C#中使用结构来传递多个参数

    C#中当参数超过5个时,建议用结构来传递多个参数. 示例代码如下: public struct MyStruct { public string str; public int number; } c ...

  10. UIDynamic(一)

    UIDynamic(一) 前言 最近看了一下UIDynamic,UIDynamic是13年WWDC出的技术.其实本人一直热衷于比较有趣的动画,特别是带物理力学的动画,感觉物理力学就是动画的灵魂,一直想 ...