每天写点python

1、收集系统信息python小程序

  1 #!/usr/bin/env python
  2 #A system information gathering script
  3 
  4 import subprocess
  5 #command 1
  6 uname = "uname"
  7 uname_arg = "-a"
  8 print "Gathering system information with %s command:\n" % uname
  9 subprocess.call([uname, uname_arg])
 10 
 11 #command 2
 12 diskspace = "df"
 13 diskspace_arg = "-h"
 14 print "Gathering diskspace information %s command:\n" % diskspace
 15 subprocess.call([diskspace, diskspace_arg])
                                           

 以上实例执行结果为:

# python system_information.py 
Gathering system information with uname command:

Linux gio016 2.6.32-431.29.2.lustre.el6.x86_64 #1 SMP Fri Jul 31 09:39:58 CST 2015 x86_64 x86_64 x86_64 GNU/Linux
Gathering diskspace information df command:

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3       258G   71G  174G  29% /
tmpfs            32G     0   32G   0% /dev/shm
/dev/sda1       485M  142M  318M  31% /boot
/dev/memdiska   1.1T  132G  914G  13% /home/export/tmp

 

2、收集系统信息(定义为多个函数)

  1 #!/usr/bin/env python 
  2 import subprocess
  3 
  4 def uname_func():
  5     uname = "uname"
  6     uname_arg = "-a"
  7     print "Gathering system information with %s command:\n" % uname
  8     subprocess.call([uname, uname_arg])
  9 
 10 def disk_func():
 11     diskspace = "df"
 12     diskspace_arg = "-h"
 13     print "Gathering diskspace information %s command:\n" % diskspace
 14     subprocess.call([diskspace, diskspace_arg])
 15 
 16 def main():
 17     uname_func()
 18     disk_func()
 19 
 20 main()

 

以上实例输出结果为:

# python system_information1.py
Gathering system information with uname command:

Linux gio016 2.6.32-431.29.2.lustre.el6.x86_64 #1 SMP Fri Jul 31 09:39:58 CST 2015 x86_64 x86_64 x86_64 GNU/Linux
Gathering diskspace information df command:

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3       258G   71G  174G  29% /
tmpfs            32G     0   32G   0% /dev/shm
/dev/sda1       485M  142M  318M  31% /boot
/dev/memdiska   1.1T  132G  914G  13% /home/export/tmp

 

3、使用shell实现该功能

  1 #!/usr/bin/env bash
  2 
  3 function uname_func()
  4 {
  5     uname="uname -a"
  6     printf "Gathering system information with the $uname command:\n\n"
  7     $uname
  8 }
  9 
 10 function disk_func()
 11 {
 12     diskspace="df -h"
 13     printf "Gathering diskspace information with the $diskspace command:\n\n"
 14     $diskspace
 15 }
 16 
 17 function main()
 18 {
 19    uname_func
 20    disk_func
 21 }
 22
 23 main

 

4、fork进程

  1 #!/usr/bin/env python
  2 import os
  3 pid = os.fork()
  4 # fork and exec together
  5 print("second test")
  6 if pid == 0: #This is the child
  7     print("this is the child")
  8     print("I'm going to exec another program now")
  9     os.execl('/bin/ls', 'ls','/etc/passwd')
 10 else:
 11     print("the child is pid %d" % pid)
 12 os.wait()
~                 

 以上实例执行结果为:

# python fork2.py
second test
the child is pid 5341
second test
this is the child
I'm going to exec another program now
/etc/passwd

 

5、db数据库

1 #!/usr/bin/env python
  2 import dbm
  3 db = dbm.open('websites', 'w')
  4 db['www.wrox.com'] = 'Wrox home page'
  5 if db['www.python.org'] != None:
  6     print('Found www.python.org')
  7 else:
  8     print('Error: Missing item')
  9 
 10 for key in db.keys():
 11     print("key =",key," value =", db[key])
 12 
 13 del db['www.wrox.com']
 14 print("After deleting www.wrox.com, we have:")
 15 
 16 for key in db.keys():
 17     print("key =",key," value =",db[key])
 18 
 19 db.close()

 以上实例结果:

# python dbmaccess.py 
Found www.python.org
('key =', 'www.wrox.com', ' value =', 'Wrox home page')
('key =', 'www.python.org', ' value =', 'Python home page')
After deleting www.wrox.com, we have:
('key =', 'www.python.org', ' value =', 'Python home page')

 

6、产生随机密码

  1 #!/usr/bin/env python 
  2 from random import choice
  3 import string
  4 def GenPasswd(length=8, chars=string.letters+string.digits):
  5         return ''.join([ choice(chars) for i in range(length)])
  6 

 以上实例执行的结果:

>>> import makepass
>>> for i in range(6):
...     print makepass.GenPasswd(12)
... 
4sxiLRE6bqAX
QSP2ZXjSsMxl
DLygnUv5T5qq
skL7pb5Zhhvp
GdJnnJKG5mZN
6gmow2qv47Tg

 

7、查看当前目录是否有(.py) python程序

>>> import os
>>> [x for x in os.listdir('.') if os.path.isfile(x) and os.path.splitext(x)[1]=='.py']

 

posted @ 2016-11-29 11:21  子非木  阅读(836)  评论(0编辑  收藏  举报