首页 技术 正文
技术 2022年11月6日
0 收藏 828 点赞 862 浏览 3255 个字

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实现该功能

   #!/usr/bin/env bash   function uname_func()
{
uname="uname -a"
printf "Gathering system information with the $uname command:\n\n"
$uname
} function disk_func()
{
diskspace="df -h"
printf "Gathering diskspace information with the $diskspace command:\n\n"
$diskspace
} function main()
{
uname_func
disk_func
} 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']
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,489
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,290