好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

SELECT版FTP的实例详解

SELECT版FTP:
使用SELECT或SELECTORS模块实现并发简单版FTP
允许多用户并发上传下载文件

必须使用select or selectors模块支持多并发,禁止使用多线程或多进程

REDMAE

用户登陆

1、查看共享目录文件
2、上传文件,
3、下载方件
4、退出

程序结构:
socket_server_client/#程序目录
|- - -clients/#client程序主目录
|      |- - -__init__.py
|      |- - -bin/#启用目录
|      |       |- - - __init__.py
|      |       |- - -socket_client.py#客户端启动
|      |
|      |- - -cfg/#配置文件目录
|      |       |- - - __init__.py
|      |       |- - -config.py#配置文件
|      |
|      |- - -core/#主要程序目录
|      |       |- - - __init__.py
|      |       |- - -client_func.py#主要函数
|      |
|      |- - -home/#客户端下载文件目录
|
|- - -servers/#server程序主目录
|      |- - -__init__.py
|      |- - -bin/#启用目录
|      |       |- - - __init__.py
|      |       |- - -registration.py#用户注册
|      |       |- - -server.py#服务端启动(selectors版)
|      |       |- - -socket_server.py#服务端启动(select版)

|      |
|      |- - -cfg/#配置文件目录
|      |       |- - - __init__.py
|      |       |- - -config.py#配置文件
|      |
|      |- - -core/#主要程序目录
|      |       |- - - __init__.py
|      |       |- - -server_classc.py#主要函数
|      |
|      |- - -db/#用户上传文件主目录
|              |- - -user_file/#用户上传目录(共享)
|              |- - -user_names#注册用户文件
| 
程序结构:
socket_server_client/#程序目录
|- - -clients/#client程序主目录
|      |- - -__init__.py
|      |- - -bin/#启用目录
|      |       |- - - __init__.py
|      |       |- - -socket_client.py#客户端启动 

  1 #!usr/bin/env python  2 #-*-coding:utf-8-*-  3 # Author calmyan  4   5 import socket,os,json,sys  6 BASE_DIR=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))#获取相对路径转为绝对路径赋于变量  7 sys.path.append(BASE_DIR)#增加环境变量  8 from core.client_func import user_pwd  9 #from core.client_func import show_process 10 from cfg import config 11  12 #进度条 13 def show_process(lens): 14     received_size=0#定义大小 15     current_percent=0#当前大小百分比 16     while received_size<lens: 17         if int((received_size/lens)*100)>current_percent: 18             print('#',end='',flush=True) 19             current_percent=int((received_size/lens)*100) 20         new_size=yield 21         received_size+=new_size 22  23 server_addr=('localhost',9500)#设置绑定的 IP 端口 24 #server_addr=('192.168.11.50',9500)#设置绑定的 IP 端口 25 client=socket.socket() 26 client.connect(server_addr) 27 while True: 28     data_d=user_pwd(client) 29     if data_d['tag']:#运行#用户名登陆成功 30         while True: 31             print('''=====指令提示==== 32             查看目录文件: ls 33             下载文件: get 文件名 或 文件编号  如: get test.txt  或  get 1 34             上传方件: put 路径/文件名 如 put e:/test.txt 35             退出:exit 36             ''') 37             cho=input('指令 >>:').strip() 38             if len(cho)==0:continue 39             if cho=='exit':exit()#退出指令 40             cmd_list=cho.split() 41             if cmd_list[0]=='put':#如果等于下载指令 42                 if len(cmd_list)==1: 43                     print('没有输入相关文件名') 44                     continue 45                 filename=cmd_list[1] 46                 file_dir=config.USER_DIR+'/'+filename 47                 if os.path.isfile(file_dir):#如果文件存在 48                     file_obj=open(file_dir,"rb")#打开文件 49                     name=file_obj.name.split('/')[-1]#文件名 50                     #name=filename.split("\\")[-1]#文件名 51                     sez=os.path.getsize(file_dir)#获取文件大小 52                     if sez<1: 53                         print('\033[41;1m文件为空!,不能上传\033[0m') 54                         continue 55                     progress = show_process(sez) #进度条 传入文件大小 56                     progress.__next__() 57                     rat=0 58                     file_obj.seek(rat)#移动到位置 59                     data_header={ 60                         "action":"put", 61                         "filename":name, 62                         "size":sez 63                     } 64                     client.send(json.dumps(data_header).encode())#用json 序列化后,发送相关 信息 65  66                     print("文件[%s]发送中...."%data_header["filename"]) 67  68                     while rat<sez: 69                         line=file_obj.read(4096) 70                         client.send(line) 71                         try: 72                             progress.send(len(line))#传入当前数据大小 73                         except StopIteration as e: 74                             print("100%") 75                             break 76                     print("文件[%s]发送完毕!"%data_header["filename"]) 77                 else: 78                     print('\033[41;1m该文件不存在或为目录\033[0m') 79                     continue 80             elif cmd_list[0]=='get':#如果等于get 上传指令 81                 if len(cmd_list)==1: 82                     print('没有输入相关文件名') 83                     continue 84                 filename=cmd_list[1] 85                 print(filename) 86                 data_header={ 87                         "action":"get", 88                         "filename":filename, 89                         "size":'' 90                     } 91                 client.send(json.dumps(data_header).encode())#用json 序列化后,发送相关 信息 92                 datas=client.recv(4096)#接收数据 指令 93                 data_l= json.loads(datas.decode())#反序列 94                 # print(data_l) 95                 # print(data_l['size']) 96                 if data_l['filename']==False: 97                     print('\033[41;1m文件不存在或者出错\033[0m') 98                     continue 99                 prten=show_process(data_l["size"])100                 prten.__next__()101                 file_dir=config.USER_DIR+'/'+data_l["filename"]102                 file_obj=open(file_dir,'wb')#打开新建 这个文件103                 rece_size=0#定义 文件大小值104 105 106                 while rece_size<data_l["size"]:#小于接收的文件大小时,107                     recv_data=client.recv(4096)108                     file_obj.write(recv_data)#写入文件109                     rece_size+=len(recv_data)#增加文件大小计算110                     try:111                         prten.send(len(recv_data))112                     except StopIteration as e:113                         print('100%')114 115                 else:116                     print("文件[%s]接收完毕!"%data_l["filename"])117                     file_obj.flush()118                     file_obj.close()#关闭文件119             elif cmd_list[0]=='ls':#查看目录文件120                 data_header={121                         "action":"ls",122                         "filename":'',123                         "size":''124                     }125                 client.send(json.dumps(data_header).encode())#用json 序列化后,发送相关 信息126                 datas=client.recv(4096)#接收数据 指令127                 data_l= json.loads(datas.decode())#反序列128                 for k,v in enumerate(data_l):129                     print('编号: %s  文件名:%s'%(k,v))130 131     else:132         print(data_d['mag']) 

查看更多关于SELECT版FTP的实例详解的详细内容...

  阅读:45次