Skip to content

Commit c1092cb

Browse files
committed
py-linux-weihu
1 parent eb8ca13 commit c1092cb

7 files changed

Lines changed: 149 additions & 0 deletions

File tree

1.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
str = input()
2+
for i in range(len(str)):
3+
if (str[i] == '+'):
4+
break
5+
print(str[i])

\

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
str = input()
2+
l = len(str)
3+
for i in range(l):
4+
print(str[i],end=" ")
5+
if (str[i] == '+'):
6+
break
7+
print (i)
8+
a = int(str[:i:])
9+
b = int(str[i+1::])
10+
print (a+b)
11+

py_linux_jiankong/1.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env Python
2+
from __future__ import print_function
3+
from collections import OrderedDict
4+
import pprint
5+
6+
def CPUinfo():
7+
''' Return the information in /proc/CPUinfo
8+
as a dictionary in the following format:
9+
CPU_info['proc0']={...}
10+
CPU_info['proc1']={...}
11+
'''
12+
CPUinfo=OrderedDict()
13+
procinfo=OrderedDict()
14+
15+
nprocs = 0
16+
with open('/proc/cpuinfo') as f:
17+
for line in f:
18+
if not line.strip():
19+
# end of one processor
20+
CPUinfo['proc%s' % nprocs] = procinfo
21+
nprocs=nprocs+1
22+
# Reset
23+
procinfo=OrderedDict()
24+
else:
25+
if len(line.split(':')) == 2:
26+
procinfo[line.split(':')[0].strip()] = line.split(':')[1].strip()
27+
else:
28+
procinfo[line.split(':')[0].strip()] = ''
29+
30+
return CPUinfo
31+
32+
if __name__=='__main__':
33+
CPUinfo = CPUinfo()
34+
for processor in CPUinfo.keys():
35+
print(CPUinfo[processor]['model name'])
36+

py_linux_jiankong/2.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env Python
2+
import os
3+
def load_stat():
4+
loadavg = {}
5+
f = open("/proc/loadavg")
6+
con = f.read().split()
7+
f.close()
8+
loadavg['lavg_1']=con[0]
9+
loadavg['lavg_5']=con[1]
10+
loadavg['lavg_15']=con[2]
11+
loadavg['nr']=con[3]
12+
loadavg['last_pid']=con[4]
13+
return loadavg
14+
print "loadavg",load_stat()['lavg_15']
15+

py_linux_jiankong/3.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env Python
2+
3+
from __future__ import print_function
4+
from collections import OrderedDict
5+
6+
def meminfo():
7+
''' Return the information in /proc/meminfo
8+
as a dictionary '''
9+
meminfo=OrderedDict()
10+
11+
with open('/proc/meminfo') as f:
12+
for line in f:
13+
meminfo[line.split(':')[0]] = line.split(':')[1].strip()
14+
return meminfo
15+
16+
if __name__=='__main__':
17+
#print(meminfo())
18+
19+
meminfo = meminfo()
20+
print('Total memory: {0}'.format(meminfo['MemTotal']))
21+
print('Free memory: {0}'.format(meminfo['MemFree']))
22+

py_linux_jiankong/4.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env Python
2+
import time
3+
import sys
4+
5+
if len(sys.argv) > 1:
6+
INTERFACE = sys.argv[1]
7+
else:
8+
INTERFACE = 'eth0'
9+
STATS = []
10+
print 'Interface:',INTERFACE
11+
12+
def rx():
13+
ifstat = open('/proc/net/dev').readlines()
14+
for interface in ifstat:
15+
if INTERFACE in interface:
16+
stat = float(interface.split()[1])
17+
STATS[0:] = [stat]
18+
19+
def tx():
20+
ifstat = open('/proc/net/dev').readlines()
21+
for interface in ifstat:
22+
if INTERFACE in interface:
23+
stat = float(interface.split()[9])
24+
STATS[1:] = [stat]
25+
26+
print 'In Out'
27+
rx()
28+
tx()
29+
30+
while True:
31+
time.sleep(1)
32+
rxstat_o = list(STATS)
33+
rx()
34+
tx()
35+
RX = float(STATS[0])
36+
RX_O = rxstat_o[0]
37+
TX = float(STATS[1])
38+
TX_O = rxstat_o[1]
39+
RX_RATE = round((RX - RX_O)/1024/1024,3)
40+
TX_RATE = round((TX - TX_O)/1024/1024,3)
41+
print RX_RATE ,'MB ',TX_RATE ,'MB'
42+

py_linux_jiankong/5.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env Python
2+
# coding:utf-8
3+
import os, sys, time
4+
5+
while True:
6+
time.sleep(4)
7+
try:
8+
ret = os.popen('ps -C apache2 -o pid,cmd').readlines()
9+
if len(ret) < 2:
10+
print "apache 进程异常退出, 4 秒后重新启动"
11+
time.sleep(3)
12+
os.system("service apache2 restart")
13+
print("service apache2 restart")
14+
ret2 = os.popen('ps -C apache2 -o pid,cmd').readlines()
15+
print(ret2)
16+
except:
17+
print "Error", sys.exc_info()[1]
18+

0 commit comments

Comments
 (0)