作业要求

0、实现用户登陆

1、实现上传和下载

3、每个用户都有自己的家目录,且只可以访问自己的家目录

4、对用户进行磁盘配额,每个用户的空间不同,超过配额不允许下载和上传

5、允许用户在指定的家目录随意切换目录

6、允许用户在自己的家目录切换目录

7、允许上传和下载文件,并判断文件的一致性

目前还未终稿,还在持续优化中

客户端核心代码

  1. import socket
    import os
    import hashlib
    import subprocess
    import json
    import time
  2.  
  3. HOST = "127.0.0.1"
    PORT = 8000
    ip_bind = (HOST, PORT)
    ftp_client = socket.socket()
    ftp_client.connect(ip_bind)
    def test_md5(path):
    md5 = hashlib.md5()
    f = open(path,"rb")
    while True:
    d = f.read(10)
    if not d:
    break
    else:
    f_md5 = md5.update(d)
    file_md5 = md5.hexdigest()
    return file_md5
  4.  
  5. while True:
    client_login = ftp_client.recv(8192)
    print(str(client_login, encoding="utf-8"))
    cline_user = input("用户名:")
    ftp_client.sendall(bytes(cline_user, encoding="utf-8"))
    client_login = ftp_client.recv(8192)
    print(str(client_login, encoding="utf-8"))
    cline_password = input("密码:")
    ftp_client.sendall(bytes(cline_password, encoding="utf-8"))
    ftp_server = ftp_client.recv(8192)
    if str(ftp_server, encoding="utf-8") == "用户名或者密码错误":
    print(str(ftp_server, encoding="utf-8"))
    continue
    else:
    print(str(ftp_server, encoding="utf-8"))
    break
  6.  
  7. while True:
    client_func = input("你可以查看和切换目录,你可以上传和下载文件:")
    ftp_client.sendall(bytes(client_func, encoding="utf-8"))
  8.  
  9. if client_func.startswith("get"):
    server_reply = ftp_client.recv(8192)
    get_info = str(server_reply,encoding="utf-8")
    print(get_info)
    file_md5 = get_info.split("|")[1].split(":")[1]
    file_size = int(get_info.split("|")[2].split(":")[1])
    file_name = get_info.split("|")[0].split(":")[1]
    sever_ack = ftp_client.recv(8192)
    if str(sever_ack,encoding="utf-8") == "ok":
    print(str(sever_ack,encoding="utf-8"))
    get_file_path = "F:\\ftp\\ftpclient\\%s" %(file_name)
    with open(get_file_path,"wb") as file:
    temp_size = int(os.path.getsize(get_file_path))
    while temp_size < file_size:
    # print("临时大小:",temp_size,"实际大小:",file_size)
    data = ftp_client.recv(8192)
    file.write(data)
    temp_size = int(os.path.getsize(get_file_path))
    print(temp_size,file_size)
    print("下载文件[%s]完成" %(file_name))
    elif client_func.startswith("put"):
    pass
    else:
    server_reply = ftp_client.recv(8192)
    print(str(server_reply, encoding="utf-8"))

服务端核心代码

  1. import socket
    import hashlib
    import subprocess
    import json
    import os
    import random
    import hashlib
    root = "F:\\ftp\\ftpserver\\root"
    bob = "F:\\ftp\\ftpserver\\bob"
    user_info = {}
    test_md5 = hashlib.md5()
  2.  
  3. def test_md5(path):
    md5 = hashlib.md5()
    f = open(path,"rb")
    while True:
    d = f.read(10)
    if not d:
    break
    else:
    f_md5 = md5.update(d)
    file_md5 = md5.hexdigest()
    return file_md5
  4.  
  5. with open("E:\\python\\网络编辑_socket\\ftp作业\\user_data", "r", encoding="utf-8") as f:
    for line in f:
    temp = line.split("|")
    temp_dict = {temp[0]: {"密码": temp[1], "配额": temp[2]}}
    user_info.update(temp_dict)
    temp = []
    temp_dict = {}
  6.  
  7. HOST = "127.0.0.1"
    PORT = 8000
    ip_bind = (HOST, PORT)
    FTP_server = socket.socket()
    FTP_server.bind(ip_bind)
    FTP_server.listen(1)
    conn, add = FTP_server.accept()
    while True:
    conn.sendall(bytes("FTP_server:请输入用户名", encoding="utf-8"))
    cline_user = str(conn.recv(8192), encoding="utf-8")
    conn.sendall(bytes("FTP_server:请输入密码", encoding="utf-8"))
    cline_password = str(conn.recv(8192), encoding="utf-8")
    if cline_user in user_info.keys():
    if cline_password == user_info[cline_user]["密码"]:
    welcome = "欢迎用户\033[31;1m[%s]\033[0m登陆FTP服务器" % (cline_user)
    send_info = welcome.center(100, "-")
    conn.sendall(bytes(send_info, encoding="utf-8"))
    break
    else:
    conn.sendall(bytes("用户名或者密码错误", encoding="utf-8"))
    continue
    else:
    conn.sendall(bytes("用户名或者密码错误", encoding="utf-8"))
    continue
  8.  
  9. client_path = "F:\\ftp\\ftpserver\\" + cline_user
    os.chdir(client_path)
    client_pwd = client_path
    while True:
    client_func = conn.recv(8192)
    if str(client_func, encoding="utf-8") == "dir":
    os.chdir(client_pwd)
    a = os.listdir(client_pwd)
    b = []
    for i in a:
    path = client_pwd + "\\" + i
    if os.path.isdir(path):
    c = "dir|" + i
    b.append(c)
    elif os.path.isfile(path):
    c = "file|" + i
    b.append(c)
    else:
    pass
    ret = json.dumps(b)
    conn.sendall(bytes(ret, encoding="utf-8"))
    elif str(client_func, encoding="utf-8").startswith("cd"):
    if str(client_func, encoding="utf-8").strip() == "cd":
    os.chdir(client_path)
    s = "切换成功"
    conn.sendall(bytes(s, encoding="utf-8"))
    client_pwd = client_path
    # print(client_pwd)
  10.  
  11. else:
    cd_func = str(client_func, encoding="utf-8").split(" ")
    cd_path = client_pwd + "\\" + cd_func[1]
    if os.path.exists(cd_path):
    if os.path.isdir(cd_path):
    os.chdir(cd_path)
    s = "切换成功"
    conn.sendall(bytes(s, encoding="utf-8"))
    client_pwd = cd_path
    else:
    s = "请输入正确的目录信息"
    conn.sendall(bytes(s, encoding="utf-8"))
    else:
    s = "该目录不存在"
    conn.sendall(bytes(s, encoding="utf-8"))
    elif str(client_func, encoding="utf-8").startswith("get"):
  12.  
  13. a = str(client_func, encoding="utf-8").split(" ")
    file_path = client_pwd + "\\" + a[1]
    file_size = str(os.path.getsize(file_path))
  14.  
  15. if os.path.exists(client_pwd + "\\" + a[1]):
    if os.path.isdir(client_pwd + a[1]):
    s = "请输入正确的文件路径信息"
    conn.sendall(bytes(s, encoding="utf-8"))
    elif os.path.isfile(client_pwd + "\\" + a[1]):
    print(client_pwd + "\\" + a[1],type(client_pwd + "\\" + a[1]))
    md5 = str(test_md5(client_pwd + "\\" + a[1]))
    print(md5)
    with open(client_pwd + "\\" + a[1], "rb") as file:
    s = "文件名称:%(file_name)s|文件md5:%(file_md5)s|文件大小:%(size)s" %{"file_name":a[1],"file_md5":md5,"size":file_size}
    print(s)
    conn.sendall(bytes(s,encoding="utf-8"))
    conn.sendall(bytes("ok",encoding="utf-8"))
    for line in file:
    # f2.write(line)
    data = conn.sendall(line)
    conn.sendall(bytes("",encoding="utf-8"))
    else:
    s = "该路径不存在"
    conn.sendall(bytes(s, encoding="utf-8"))
    elif str(client_func, encoding="utf-8").startswith("put"):
    pass
    else:
    s = "错误的输入,请重新输入"
    conn.sendall(bytes(s, encoding="utf-8"))

  

python之ftp作业【还未完成】的更多相关文章

  1. python day 18: thinking in UML与FTP作业重写

    目录 python day 18 1. thinking in UML读书小感 2. FTP作业重写 2.1 软件目录结构 2.2 FTPClient端脚本 2.3 FTPServer端脚本 pyth ...

  2. Python学习笔记——基础篇【第七周】———FTP作业(面向对象编程进阶 & Socket编程基础)

    FTP作业 本节内容: 面向对象高级语法部分 Socket开发基础 作业:开发一个支持多用户在线的FTP程序 面向对象高级语法部分 参考:http://www.cnblogs.com/wupeiqi/ ...

  3. python day33 ,socketserver多线程传输,ftp作业

    一.一个服务端连多个客户端的方法 1.服务端 import socketserver class MyServer(socketserver.BaseRequestHandler): def hand ...

  4. python全栈开发day29-网络编程之socket常见方法,socketserver模块,ftp作业

    一.昨日内容回顾 1.arp协议含义 2.子网,子网掩码 3.两台电脑在网络中怎么通信的? 4.tcp和udp socket编码 5.tcp和udp协议的区别 6.tcp三次握手和四次挥手,syn洪攻 ...

  5. Python socketserver ftp功能简单讲解

    socketserver模块实现并发 为什么要讲socketserver?我们之前写的tcp协议的socket是不是一次只能和一个客户端通信,如果用socketserver可以实现和多个客户端通信.它 ...

  6. Python学习day5作业

    目录 Python学习day5作业 ATM和购物商城 1. 程序说明 2. 基本流程图 3. 程序测试帐号 4. 程序结构: 5. 程序测试 title: Python学习day5作业 tags: p ...

  7. 基于python复制蓝鲸作业平台

    前言 去年看武sir代码发布的视频无意中听到了蓝鲸平台但是一直没深究,前一段时间公司要搞一个代码发布平台,但是需求变化很多一直找不到一个很好的参考 模板,直到试用了一下蓝鲸作业平台发现“一切皆作业”的 ...

  8. Python和FTP

    1.HTTP主要用于基于Web的文件下载以及访问Web服务,一般客户端无须登录就可以访问服务器上的文件和服务.大部分HTTP文件传输请求都用于获取网页(即将网页文件下载到本地). 2.FTP主要用于匿 ...

  9. Python实现扫描作业配置自动化

    持续集成平台接入扫描作业是一项繁琐而又需要细致的工作,于是趁着闲暇时间,将代码扫描作业用Python代码实现了配置自动化. 每次配置作业的过程中,都会在checkcode1或者checkcode3上 ...

随机推荐

  1. 如何捕捉@tornado.gen.coroutine里的异常

    from tornado import gen from tornado.ioloop import IOLoop @gen.coroutine def throw(a,b): try: a/b ra ...

  2. 补充2:Golang 一些特性

    Go语言的这些地方都做的还不错: 拥有自动垃圾回收: 不用手动释放内存 一个包系统: Go 语言的源码复用建立在包(package)基础之上.包通过 package, import, GOPATH 操 ...

  3. 网络虚拟化中的 offload 技术:LSO/LRO、GSO/GRO、TSO/UFO、RSS、VXLAN

    offload offload特性,主要是指将本来在操作系统协议栈中进行的一些数据包处理(如IP分片.TCP分片.重组.checksum校验等)放到网卡硬件中去做,降低系统 CPU 消耗,提高处理的性 ...

  4. JavaScript函数及作用域

    知识内容: 1.JavaScript函数 2.JavaScript全局函数及特殊函数 3.JavaScript作用域 4.本节练习 参考资料:<JavaScript高级程序设计> 一.Ja ...

  5. Openstack虚机实例状态错误手工恢复vm_state:error

    Openstack虚机实例状态错误手工恢复vm_state:error 1.找到状态为出错状态的VM.在数据库里面表现Status为ERROR而非ACTIVE. 2.找到出错状态VM的UUID. 3. ...

  6. python入门-文件

    1 读取文件 with open('1.txt') as file_ojbect: contents = file_ojbect.read() print(contents.rstrip()) wit ...

  7. robot framework取出列表子元素

    取出嵌套列表变量的子元素 ${list}型列表: ${list} = [["A1", "first"], ["A2", "seco ...

  8. vs2015 调试IIS

    vs2015 调试IIS vs2015,menu,调试>附加到进程>w3wp 然后用浏览器打开网页,单步调试跟踪. http://blog.csdn.net/hyperhawk/artic ...

  9. eclipse Jsp 自创建tags问题

    Can not find the tag directory "/WEB-INF/tags" 网上的说法有三种情况: 1. jsp2.0开始支持自定义tag,因此 web.xml文 ...

  10. this指针的调整

    我们先来看一段代码: #include <iostream> using namespace std; class A { public: int a; A( ) { printf(&qu ...