FTP之验证功能第二步

FTP之验证功能第二步_第1张图片
image.png

ftp_client.py

import optparse
import socket
import configparser
import json
class ClientHandler():

    def __init__(self):
        self.op = optparse.OptionParser()
        self.op.add_option("-s", "--s", dest="server")
        self.op.add_option("-P", "--port", dest="port")
        self.op.add_option("-u", "--username", dest="username")
        self.op.add_option("-p", "--password", dest="password")
        self.options, self.args = self.op.parse_args()  # 参数解析

        self.verify_args(self.options, self.args)
        self.make_connection()

    def verify_args(self,options, args):
        server = options.server
        port = options.port
        if 0 < int(port) < 65535:
            return True
        else:
            exit("the port is in 0-65535")

    def make_connection(self):
        self.sock = socket.socket()
        # self.sock.connect((self.options.server,self.options.port))
        self.sock.connect((self.options.server,int(self.options.port)))

    def interactive(self):
        #判断是不是我的用户,先登录
        self.authenticate()

    def authenticate(self):
        if self.options.username is None or self.options.password is None:
            username = input("username:")
            password = input("password:")
            #走get_auth_result这个函数
            return self.get_auth_result(username,password)
        else:
            return self.get_auth_result(self.options.username,self.options.password)
 
   #来自server端的应答信息
    def response(self):
        data = self.sock.recv(1024).decode("utf8")
        data = json.loads(data)
        return data

    def get_auth_result(self,user,pwd):

        data = {
            "action":"auth",
            "username":user,
            "password":pwd
        }

        self.sock.send(json.dumps(data).encode("utf8"))
        response = self.response()
        print(response)


ch = ClientHandler()
ch.interactive()

ftp_server.py

import os,sys
PATH= os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(PATH)
from core import main

if __name__ == '__main__':
    main.ArgvHandler()

main.py

import optparse  #解析命令行命令的
import socketserver

from conf import settings
from core import server


class ArgvHandler():
    def __init__(self):
        #参数解析的功能
        self.op = optparse.OptionParser()
        # self.op.add_option("-s","--s",dest="server")
        # self.op.add_option("-P", "--port", dest="port")
        options,args = self.op.parse_args() #参数解析

        self.verify_args(options,args)


    def verify_args(self,options,args):
        cmd = args[0]

        #  类下是否有cmd方法,类的反射
        if hasattr(self,cmd):
            func = getattr(self,cmd)
            func()

    def start(self):
        print("the server is working")
        s =socketserver.ThreadingTCPServer((settings.IP,settings.PORT),server.ServerHandler)
        s.serve_forever()

server.py

import socketserver
import json
class ServerHandler(socketserver.BaseRequestHandler):

    def handle(self):

        while True:
            #conn = self.request
            data = self.request.recv(1024).strip()
            #传输过来的是字符串形式,通过json.loads()方法,转换成字典
            data = json.loads(data.decode("utf8"))
            """
            {"action":"auth",
             "usernam":"yuan",
             "pwd":123}
           """
            if data.get("action"):
                if hasattr(self,data.get("action")):
                    func = getattr(self,data.get("action"))
                    func(**data)
                #这个异常代表action里面的命令,没有相应的方法匹配
                else:
                    print("invalid cmd")
            #这个异常代表action里没有命令
            else:
                print("invalid cmd")
    def auth(self,**data):
        print("data:",data)

    def put(self,**data):
        pass

settings.py

IP= "127.0.0.1"
PORT = 8080

accounts.cfg

[DEFAULT]

[yuan]
Password = 123
Quotation = 100

[root]
Password = root
Quotation = 100


你可能感兴趣的:(FTP之验证功能第二步)