1 #!/user/bin/env python 2 # -*- coding:utf-8 -*- 3 4 import sys 5 class WebServer(object): 6 def __init__(self,host,port): 7 self.host = host 8 self.port = port 9 10 def start(self): 11 print("Servser is starting....") 12 13 def stop(self): 14 print("server is stop....") 15 16 def restart(self): 17 self.stop() 18 self.start() 19 print("server isrestart.....") 20 21 def test_run(f,name): 22 print("runngin...",f.host,name) 23 24 25 if __name__ == "__main__": 26 webServer = WebServer("localhost",3304) 27 webServer2 = WebServer("localhost",3306) 28 29 if hasattr(webServer,sys.argv[1]): 30 f = getattr(webServer,sys.argv[1]) #获取webServer.star的内存地址 31 f() #执行该函数 32 33 #把test_run的方法绑定到实例中 34 # setattr(webServer,"r_run",test_run) 35 # webServer.r_run(webServer,"tommie") #绑定在webServer实例上 36 # webServer2.r_run(webServer,"tommie") # 37 38 #删除 39 # delattr(webServer,"host") 40 delattr(WebServer,"start") #删除了start方法 41 print(webServer.restart())