zoukankan      html  css  js  c++  java
  • ftp实现终端start,并且反射型命令分发

    ftp_client文件夹下的ftp_client.py:

    import socket
    sk=socket.socket()
    sk.connect(('127.0.0.1',8000))

    ftp server文件夹下的:

    1 bin文件夹下ftp_server。py:bin作为启动文件夹

    #bin文件夹下 作为 启动页面,执行core文件夹下main
    #当前py文件的文件夹作为第一目录,所以不能选core,因为bin下没有core
    #from core import main#从文件夹导入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()

    2 conf文件夹下settings.py:conf作为配置文件夹

    IP='127.0.0.1'
    PORT=8000

    3 core文件夹下:3.1 main.py:

    import socketserver
    import optparse#optparse解析命令行命令
    from conf import settings
    from core import server#与server在同一文件夹下,但也要这样导入
    class ArgvHandler():
        def __init__(self):#类先写初始化
            #要连接先考虑ip地址和端口
            self.op=optparse.OptionParser()
            options,args=self.op.parse_args()
    
            self.verify_args(options,args)
    
        def verify_args(self,options,args):
    
            cmd=args[0]
            #命令分发:1.if cmd=='start'
            #            2.字典
            #              3.反射
            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()
        def help(self):
            pass

    core文件夹下:3.2 server.py

    import socketserver
    class ServerHandler(socketserver.BaseRequestHandler):
        def handle(self):
            print('ok')

    代码执行方式:终端输入语句:先cd进入ftpserver 下的bin文件夹 然后输入语句python ftp_server.py start

    (venv) C:UsershoundPycharmProjects estftp serverin>
    (venv) C:UsershoundPycharmProjects estftp serverin>python ftp_server.py start
    the server is working
    ok



  • 相关阅读:
    进程与线程
    the art of seo(chapter seven)
    the art of seo(chapter six)
    the art of seo(chapter five)
    the art of seo(chapter four)
    the art of seo(chapter three)
    the art of seo(chapter two)
    the art of seo(chapter one)
    Sentinel Cluster流程分析
    Sentinel Core流程分析
  • 原文地址:https://www.cnblogs.com/wfl9310/p/9082314.html
Copyright © 2011-2022 走看看