码迷,mamicode.com
首页 > 编程语言 > 详细

python之ftp作业【还未完成】

时间:2017-07-25 10:15:10      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:目录切换   pass   下载   []   输入密码   编程   dom   核心   切换   

作业要求

0、实现用户登陆

1、实现上传和下载

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

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

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

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

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

 

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

客户端核心代码

import socket
import os
import hashlib
import subprocess
import json

HOST = "127.0.0.1"
PORT = 8000
ip_bind = (HOST,PORT)
ftp_client = socket.socket()
ftp_client.connect(ip_bind)
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)
    # print(str(ftp_server,encoding="utf-8"))
    if str(ftp_server,encoding="utf-8") == "用户名或者密码错误":
        print(str(ftp_server,encoding="utf-8"))
        continue
    else:
        print(str(ftp_server, encoding="utf-8"))
        break


while True:
    client_func = input("你可以查看和切换目录,你可以上传和下载文件:")
    ftp_client.sendall(bytes(client_func,encoding="utf-8"))
    while True:
        server_reply = ftp_client.recv(8192)
        print(str(server_reply,encoding="utf-8"))

 

服务端核心代码

import socket
import hashlib
import subprocess
import json
import os
import random
cyr = "G:\\FTP_server\\bob"
root = "G:\\FTP_server\\root"
user_info = {}

with open("E:\\python\\pycharm\\goto_end\\week4_网络编程\\FTP作业\db\\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 = {}


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 = "欢迎用户[%s]登陆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

client_path = "G:\\FTP_server\\" + 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
        # print(b)
        ret = json.dumps(b)
        conn.sendall(bytes(ret,encoding="utf-8"))
    elif str(client_func,encoding="utf-8").startswith("cd"):
        # print(str(client_func,encoding="utf-8"))
        # print(client_path)
        # print(client_pwd)
        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)

        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"):
        a = str(client_func,encoding="utf-8").split(" ")
        # print(client_pwd + a[1])
        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]):
                with open(client_pwd + "\\" + a[1],"rb") as file:
                    for line in file:
                        conn.sendall(line)
        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作业【还未完成】

标签:目录切换   pass   下载   []   输入密码   编程   dom   核心   切换   

原文地址:http://www.cnblogs.com/bainianminguo/p/7231971.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!