Python基础应用---类

python的应用:python


应用:定义一个类,表示“服务器”shell

      服务器类,有一个公开的属性:一共有多少台服务器centos

      每一台服务器有这些属性:主机名,ip地址bash

      每一台服务器对外提供方法:1)重启Apache服务, 服务器

                             

   并使用这个类建立2台服务器。ssh

[root@centos pytonjiaoben]# cat  lie.py 
# -*- coding:utf-8 -*-
import os
class server:
  host = 0
  def __init__(self,name="localhost",ip="127.0.0.1"):
         self.name = name
         self.ip = ip
         server.host += 1
         print "%s - %s" %(self.name,self.ip)
  def  restart_httpd(self):
         com='ssh  %s  service httpd restart' %(self.ip)    ###执行shell命令
         os.system(com)
  
host1 = server()
host1.restart_httpd()
host2 = server(ip="172.25.0.31") 
print server.host
host2.restart_httpd()



执行结果:ide

[root@centos pytonjiaoben]# python lie.py 
localhost - 127.0.0.1
root@127.0.0.1's password: 
中止 httpd:[肯定]
正在启动 httpd:[肯定]
localhost - 172.25.0.31
2
root@172.25.0.31's password: 
中止 httpd:[肯定]
正在启动 httpd:[肯定]
相关文章
相关标签/搜索