python调用linux
import os
os.system("命令")
2. 如何用Python写Linux命令
有其中一种方法,可以调用os库
import os
os.system("ls")
这样就执行了 linux ‘ls’ 命令了
3. python中,调用linux执行命令的方法中有break,需要用到返回值 继续自己的程序,结果执
摘要 您好,我这边正在为您查询,请稍等片刻,我这边马上回复您~关于python中,调用linux执行命令的方法中有break,需要用到返回值 继续自己的程序,结果执行调用方法就退出程序了,怎么办?
4. python 怎么调用linux下的tcpmp
之前在linux用python脚本写一个抓包分析小工具,实在不想用什么libpcap、pypcap所以,简单来了个tcpmp加grep搞定。基本思路是分别起tcpmp和grep两个进程,进程直接通过pipe交换数据,简单代码如下:
[python] view plain
#! /usr/bin/python
def tcpmp():
import subprocess, fcntl, os
# sudo tcpmp -i eth0 -n -s 0 -w - | grep -a -o -E "Host: .*|GET /.*"
cmd1 = ['tcpmp', '-i', 'eth0', '-n','-B', '4096','-s', '0', '-w', '-']
cmd2 = ['grep', '--line-buffered', '-a', '-o', '-E', 'Host: .*|GET /.*']
p1 = subprocess.Popen(cmd1, stdout=subprocess.PIPE)
p2 = subprocess.Popen(cmd2, stdout=subprocess.PIPE, stdin=p1.stdout)
flags = fcntl.fcntl(p2.stdout.fileno(), fcntl.F_GETFL)
fcntl.fcntl(p2.stdout.fileno(), fcntl.F_SETFL, (flags | os.O_NDELAY | os.O_NONBLOCK))
return p2
def poll_tcpmp(proc):
#print 'poll_tcpmp....'
import select
txt = None
while True:
# wait 1/10 second
readReady, _, _ = select.select([proc.stdout.fileno()], [], [], 0.1)
if not len(readReady):
break
try:
for line in iter(proc.stdout.readline, ""):
if txt is None:
txt = ''
txt += line
except IOError:
print 'data empty...'
pass
break
return txt
proc = tcpmp()
while True:
text = poll_tcpmp(proc)
if text:
print '>>>> ' + text
5. python脚本怎么执行linux系统命令
打开apache服务其实就一条,就是执行{type httpd}所得到的执行文件。
#!/usr/bin/python
#!coding=<utf-8>
import os
os.system("service httpd start")
#这样就执行完了,os模块还有其他可用方法,比如popen(),请参考标准模块手册。
6. python linux怎么操作
1. os 模块
1.1. os模块的exec方法族
Python的exec系统方法同Unix的exec系统调用是一致的。这些方法适用
于在子进程中调用外部程序的情况,因为外部程序会替换当前进程的代码,不会返回。( 这个看了点 help(os) --> search
"exec" 的相关介绍,但是没太搞明白咋使用)
1.2. os模块的system方法
system方法会创建子进程运行外部程序,方法只返回外部程序的运行结果。这个方法比较适用于外部程序没有输出结果的情况。
[python]
view plain
>>> import os
>>> os.system("echo \"Hello World\"") # 直接使用os.system调用一个echo命令
Hello World ——————> 打印命令结果
0 ——————> What's this ? 返回值?
>>> val = os.system("ls -al | grep \"log\" ") # 使用val接收返回值
-rw-r--r-- 1 root root 6030829 Dec 31 15:14 log ——————> 此时只打印了命令结果
>>> print val
0 ——————> 注意,此时命令正常运行时,返回值是0
>>> val = os.system("ls -al | grep \"log1\" ")
>>> print val
256 ——————> 使用os.system调用一个没有返回结果的命令,返回值为256~
>>>
注意:上面说了,此方法脂肪会外部程序的结果,也就是os.system的结果,所以如果你想接收命令的返回值,接着向下看~
1.3. os模块的popen方法
当需要得到外部程序的输出结果时,本方法非常有用,返回一个类文件对象,调用该对象的read()或readlines()方法可以读取输出内容。比如使用urllib调用Web API时,需要对得到的数据进行处理。os.popen(cmd) 要得到命令的输出内容,只需再调用下read()或readlines()等 如a=os.popen(cmd).read()
[python]
view plain
>>> os.popen('ls -lt') # 调用os.popen(cmd)并不能得到我们想要的结果
<open file 'ls -lt ', mode 'r' at 0xb7585ee8>
>>> print os.popen('ls -lt').read() # 调用read()方法可以得到命令的结果
total 6064
-rwxr-xr-x 1 long long 23 Jan 5 21:00 hello.sh
-rw-r--r-- 1 long long 147 Jan 5 20:26 Makefile
drwxr-xr-x 3 long long 4096 Jan 2 19:37 test
-rw-r--r-- 1 root root 6030829 Dec 31 15:14 log
drwxr-xr-x 2 long long 4096 Dec 28 09:36 pip_build_long
drwx------ 2 Debian-gdm Debian-gdm 4096 Dec 23 19:08 pulse-gylJ5EL24GU9
drwx------ 2 long long 4096 Jan 1 1970 orbit-long
>>> val = os.popen('ls -lt').read() # 使用变量可以接收命令返回值
>>> if "log" in val: # 我们可以使用in来判断返回值中有木有一个字符串
... print "Haha,there is the log"
... else:
... print "No,not happy"
...
Haha,there is the log
2. commands 模块
使用commands模块的getoutput方法,这种方法同popend的区别在于popen返回的是一个类文件对象,而本方法将外部程序的输出结果当作字符串返回,很多情况下用起来要更方便些。
主要方法:
* commands.getstatusoutput(cmd) 返回(status, output)
* commands.getoutput(cmd) 只返回输出结果
* commands.getstatus(file) 返回ls -ld file的执行结果字符串,调用了getoutput,不建议使用此方法
[python]
view plain
long@zhouyl:/tmp/tests$ python
Python 2.7.3 (default, Jan 2 2013, 16:53:07)
[GCC 4.7.2] on linux2
Type "help", "right", "credits" or "license" for more information.
>>> import commands
>>> commands.getstatusoutput('ls -lt') # 返回(status, output)
(0, 'total 5900\n-rwxr-xr-x 1 long long 23 Jan 5 21:34 hello.sh\n-rw-r--r-- 1 long long 147 Jan 5 21:34 Makefile\n-rw-r--r-- 1 long long 6030829 Jan 5 21:34 log')
>>> commands.getoutput('ls -lt') # 返回命令的输出结果(貌似和Shell命令的输出格式不同哈~)
'total 5900\n-rwxr-xr-x 1 long long 23 Jan 5 21:34 hello.sh\n-rw-r--r-- 1 long long 147 Jan 5 21:34 Makefile\n-rw-r--r-- 1 long long 6030829 Jan 5 21:34 log'
>>> commands.getstatus('log') # 调用commands.getoutput中的命令对'log'文件进行相同的操作
'-rw-r--r-- 1 long long 6030829 Jan 5 21:34 log'
>>>
3. subprocess模块
根据Python官方文档说明,subprocess模块用于取代上面这些模块。有一个用Python实现的并行ssh工具—mssh,代码很简短,不过很有意思,它在线程中调用subprocess启动子进程来干活。
[python]
view plain
>>> from subprocess import call
>>> call(["ls", "-l"])
subprocess与system相比的优势是它更灵活(你可以得到标准输出,标准错误,“真正”的状态代码,更好的错误处理,等..)。我认为使用os.system已过时,或即将过时。
7. Python脚本在Linux上怎么运行
一、首先下载安装python,建议安装2.7版本以上,3.0版本以下,由于3.0版本以上不向下兼容,体验较差。