Python system Administration Chapter 1th, python execution commands, Python functions, orientation-oriented programming, code reuse via import statements

Source: Internet
Author: User

1. Execute commands in Python

Example 1:

[email protected] opt]# cat pyls.py #!/usr/bin/env Python#python wrapper for the LS commandimport subprocess subproces S.call (["LS", "-l"])

Example 2:

[email protected] opt]# cat pysysinfo.py #!/usr/bin/env pythonimport subprocess #Command 1uname= "uname" uname_arg= "-a" Print "Gathering System information with%s command:\n"% uname subprocess.call ([Uname,uname_arg]) #Command 2diskspace= " DF "diskspace_arg="-H "print" Gathering diskspace information with%s command:\n "% Diskspacesubprocess.call ([DiskSpace, Diskspace_arg])


2. Python function

[[email protected] opt]# cat pysysinfo_func.py #!/usr/bin/env python#_*_  Coding:utf-8 _*_import subprocess#command 1def uname_func ():           #函数体下面要有缩进, indent to align, belong to the same level         uname= "uname"         uname_arg= "-A"          print  "gathering system information with %s command:\n"  % uname          subprocess.call ([Uname,uname_arg]) #Command  2def disk _func ():           diskspace= "DF"          diskspace_arg= "-H"         print  " gathering diskspace information with %s command:\n " % diskspace       &nBsp; subprocess.call ([Diskspace,diskspace_arg]) Def main ():         uname_func ()    #调用函数         disk_func () main ()     #调用主函数


3, oriented to the image programming

[[email protected] opt]# vim pyserverclass.py    #!/usr/bin/env  Python#_*_ coding:utf-8 _*_class server (object):    #class关健字, name of the Server class, The object class, the name of the object class, is the parent class         def __init__ (Self,ip, hostname):     #定义构造器                  self.ip=ip                 self.hostname=hostname        def  Set_user (Self,user):                 self.test=user        def ping (SELF,IP_ADDR):                 print  "pinging % s from  %s  (%s)  user is %s " %  (ip_addr,self.ip,self.hostname,self.test) #__name__ This means that if the py file is executed at the top level, it will be equal to the __main__if __name__== ' __main__ ':    #__name__属于模块内置属性, a py file that is written is a module. Store his name         server=server (' 192.168.1.20 ', ' bumbly ')          server.set_user (' Wsyht ')          Server.ping (' 192.168.1.15 ')


4. Implement code reuse via import statement

[email protected] opt]# cat pynewsysinfo.py #!/usr/bin/env pythonimport subprocessfrom pysysinfo_func Import disk_ Funcdef tmp_spack (): tmp_usage= "du" tmp_arg= "-H" path= "/opt" print "Space used in/opt Directo Ry "Subprocess.call ([Tmp_usage,tmp_arg,path]) def main (): Disk_func tmp_spack () if __name__== ' __main__ ' : Main ()


This article is from the "Wsyht blog" blog, make sure to keep this source http://wsyht2015.blog.51cto.com/9014030/1794438

Python system Administration Chapter 1th, python execution commands, Python functions, orientation-oriented programming, code reuse via import statements

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.