py学习之FTP
1.FTP之参数解析与命令分发
a) 层级目录如下
b) 配置文件如下
#!/usr/bin/env python # -*- coding:utf8 -*- import socket sk=socket.socket() sk.connect(('127.0.0.1',8080))#括号里是元组
#!/usr/bin/env python # -*- coding:utf8 -*- import os,sys PATH=os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.append(PATH) # print(os.path.dirname(__file__)) #打印文件所在当前目录 # print(os.path.abspath(__file__))#定位到文件绝对路径 from core import main if __name__ == '__main__': main.ArgvHandler()
#!/usr/bin/env python # -*- coding:utf8 -*- IP='127.0.0.1' PORT=8080
#!/usr/bin/env python # -*- coding:utf8 -*- import optparse #解析命令行的命令 import socketserver from conf import settings from core import server #必须这样导入,因为path路径找不到 class ArgvHandler(): def __init__(self): #初始方法 self.op=optparse.OptionParser() # self.op.add_option("-s","--s",dest="server") # -s 自己起的,--s 固定参数 # self.op.add_option("-P","--port",dest="port") options,args=self.op.parse_args() #参数解析,返回两个结果,option是绑定参数, # args返回没绑定的参数列表 self.verify_args(options,args) def verify_args(self,options,args): cmd = args[0] if hasattr(self,cmd): #反射 func=getattr(self,cmd) #拿到对应 func() def start(self): print('The server is working on port 8080......') s=socketserver.ThreadingTCPServer((settings.IP,settings.PORT),server.Serverhandler) s.serve_forever() def help(self): pass print(type(options)) print(options.server) print(options.port) print(args[1])
#!/usr/bin/env python # -*- coding:utf8 -*- import socketserver class Serverhandler(socketserver.BaseRequestHandler): def handle(self): print('ok------yu')
努力成为一个开发者
个人站点:www.pythonav.cn