牛掰的python与unix
python的中心哲学
Python 2.7.5 (default, Nov 6 2016, 00:28:07)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import this
The Zen of Python, by Tim Peters Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
IPython和Bash
IPython与Bash输出hello pyyu
[root@py_unix ~]# ipython
Python 2.7. (default, Nov , ::)
Type "copyright", "credits" or "license" for more information. IPython 3.2. -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details. In []: print "hello pyyu"
hello pyyu
[root@py_unix ~]# echo "Hello pyyu"
Hello pyyu
IPython与Bash执行命令
Bash查看当前目录下的文件
[root@py_unix opt]# ls -l ./
总用量
-rw-r--r-- root root 6月 : access.log
-rw-r--r-- root root 6月 : get_ip.py
drwxr-xr-x root root 6月 : mysql
-rw-r--r-- root root 8月 mysql-5.5.-linux2.-x86_64.tar.gz
drwxr-xr-x root root 7月 : redis
In []: import subprocess In []: subprocess.c
subprocess.call subprocess.check_call subprocess.check_output In []: subprocess.call(["ls","-l","/opt"])
总用量
-rw-r--r-- root root 6月 : access.log
-rw-r--r-- root root 6月 : get_ip.py
drwxr-xr-x root root 6月 : mysql
-rw-r--r-- root root 8月 mysql-5.5.-linux2.-x86_64.tar.gz
drwxr-xr-x root root 7月 : redis
Out[]:
Subprocess与import的过程
In [5]: subprocess.call(["some_command","some_argument","another_argument_or_path"])
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
<ipython-input-5-541ed8cab1e8> in <module>()
----> 1 subprocess.call(["some_command","some_argument","another_argument_or_path"]) /usr/lib64/python2.7/subprocess.pyc in call(*popenargs, **kwargs)
522 retcode = call(["ls", "-l"])
523 """
--> 524 return Popen(*popenargs, **kwargs).wait()
525
526 /usr/lib64/python2.7/subprocess.pyc in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags)
709 p2cread, p2cwrite,
710 c2pread, c2pwrite,
--> 711 errread, errwrite)
712 except Exception:
713 # Preserve original exception in case os.close raises. /usr/lib64/python2.7/subprocess.pyc in _execute_child(self, args, executable, preexec_fn, close_fds, cwd, env, universal_newlines, startupinfo, creationflags, shell, to_close, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite)
1325 raise
1326 child_exception = pickle.loads(data)
-> 1327 raise child_exception
1328
1329 OSError: [Errno 2] No such file or directory
python脚本的运行
[root@py_unix home]# cat ls.py
#!/usr/bin/env python
#python wrapper for ther ls command
import subprocess
subprocess.call(["ls","-l"]) [root@py_unix home]# python ls.py
总用量 4
-rw-r--r-- 1 root root 106 8月 20 09:17 ls.py
[root@py_unix home]# cat osinfo.py
#!/usr/bin/env python
# A System Information Gathering Script
import subprocess
#Command 1
uname = "uname"
uname_arg = "-a"
print "Gathering system information with %s command:\n" % uname
subprocess.call([uname,uname_arg])
#command 2
diskspace = "df"
diskspace_arg = "-h"
print "Gathering diskspace information %s command:\n" %diskspace
subprocess.call([diskspace,diskspace_arg])
输出结果:
[root@py_unix home]# python osinfo.py
Gathering system information with uname command: Linux py_unix 3.10.0-514.21.1.el7.x86_64 #1 SMP Thu May 25 17:04:51 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
Gathering diskspace information df command: 文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/cl-root 17G 3.8G 14G 22% /
devtmpfs 478M 0 478M 0% /dev
tmpfs 489M 0 489M 0% /dev/shm
tmpfs 489M 6.8M 482M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda1 1014M 184M 831M 19% /boot
tmpfs 98M 0 98M 0% /run/user/0
python和bash查看内核信息和内存:
[root@py_unix home]# cat osinfo.sh
#!/usr/bin/env bash
#A system Information Gathering Script
#Command 1
UNAME="uname -a"
printf "内核信息$UNAME \n\n"
$UNAME #Command 2
DISKSPACE="df -h"
printf "内存信息$DISKSPACE \n\n"
$DISKSPACE
#############
注意Bash的空格
subprocess模块
加载subprocess模块仅仅是将可以使用的代码文件加载进来。也可以创建自己的模块或文件,拱以后重复使用,这与加载subprocess模块的方法相同。IPython shell的一个非常好的优点就是可以对模块或者文件检查,查看其内部可用的属性。
使用Tab自动完成查看subprocess中可用的属性:
In []: subprocess.
subprocess.CalledProcessError subprocess.STDOUT subprocess.errno subprocess.mswindows subprocess.signal
subprocess.MAXFD subprocess.call subprocess.fcntl subprocess.os subprocess.sys
subprocess.PIPE subprocess.check_call subprocess.gc subprocess.pickle subprocess.traceback
subprocess.Popen subprocess.check_output subprocess.list2cmdline subprocess.select subprocess.types
查看subprocess.call的信息:
In []: subprocess.call?
Signature: subprocess.call(*popenargs, **kwargs)
Docstring:
Run command with arguments. Wait for command to complete, then
return the returncode attribute. The arguments are the same as for the Popen constructor. Example: retcode = call(["ls", "-l"])
File: /usr/lib64/python2./subprocess.py
Type: function
Python的优点之一是其交互式解释器,也称为shell。shell提供了一种能快速实现灵感、检验特性的方法,以及交互式的模块界面,能够将一些需要两三行脚本才能完成的任务一次性完成。
通常我们编写代码时,会采用同时运行文本编辑器和python的方式(稍后会有介绍,这实际上运行的就是Ipython),通过交互式的使用编辑器和shell,也就是在两者之间切换来完成程序的编写。
IPython集成了交互式Python的诸多优点。IPython具有卓越的Python shell,其性能远远优于标准Python的shell。IPython同时提供了基于控制台命令环境的定制功能,可以十分轻松的将交互式Python shell包含在各种Python应用中,甚至当作shell使用
Ipython提供了两类自动完成:完成(complete)与菜单完成(menu)。两者的差别在于'完成' 尽可能扩展当前的主题词,并提供一个可能的替换列表,而“菜单完成”会扩展主题词,直接匹配可以替换列表中的一个,并且如果连续按Tab键时,每一次都会切换到下一个可能的替换。IPython的默认自动完成是‘完成’。也可以通过设置修改。
强大的魔力函数
IPython有强大的功能。原因之一是它具有非常多的,内建的built-in魔力函数。
输入
按下Tab可以找出所有魔力函数.魔力函数的名字magic本身就具有魔力。运行magic可以打开一个分页的帮助文档,其中记录了所有IPython内建函数的用法。这个帮助文档包括函数名,函数的用法(用于何处),以及函数工作方式的描述。
UNIX Shell
UNIX shell提供了一个处理问题的统一方法,具有丰富的工具集,相当简练容易的语法、标准I/O流、管道、以及重定向等功能。
alias
In []: %alias nss netstat -tunlp
nss |grep 22
牛掰的python与unix的更多相关文章
- RNG牛掰!
2018-05-21 RNG牛掰!Uzi圆梦! 不说了,先去哭了! 2018-07-08 洲际赛后更新,RNG依然牛逼! 2018-08-30 亚运后后更新,UZI加油! 2018-10-22 继续加 ...
- Jackson:我是最牛掰的 Java JSON 解析器(有点虚)
在当今的编程世界里,JSON 已经成为将信息从客户端传输到服务器端的首选协议,可以好不夸张的说,XML 就是那个被拍死在沙滩上的前浪. 很不幸的是,JDK 没有 JSON 库,不知道为什么不搞一下.L ...
- python写unix口令破解器
看了python绝技做出来的unix口令破解器 首先需要crypt. python并不自带!! windows下pip安装失败= = 后来直接去kali敲了 附件:jiami.txt #假设是unix ...
- python datetime unix时间戳以及字符串时间戳转换
将python的datetime转换为unix时间戳 import time import datetime dtime = datetime.datetime.now() ans_time = ti ...
- python程序unix密码破解
# qianxiao996精心制作 #博客地址:https://blog.csdn.net/qq_36374896 #!/usr/bin/env python #指定这是一个python文件,使用这个 ...
- html5 canvas(小树姐的牛掰到爆了的作品)
自从小树嫁了个牛逼的前端之后,canvas的境界超过我了!!! 小树demo 小编表示:这个境界,这个几何,让我有种跪舔的感觉... http://www.wow-trend.com/brand/in ...
- 牛人总结python中string模块各属性以及函数的用法,果断转了,好东西
http://blog.chinaunix.net/uid-25992400-id-3283846.html http://blog.csdn.net/xiaoxiaoniaoer1/article/ ...
- X86-64寄存器和栈帧--牛掰降解汇编函数寄存器相关操作
X86-64寄存器和栈帧 概要 说到x86-64,总不免要说说AMD的牛逼,x86-64是x86系列中集大成者,继承了向后兼容的优良传统,最早由AMD公司提出,代号AMD64:正是由于能向后兼容,AM ...
- 小事牛刀之——python做文件对比
使用python对比filename1和filenam2的差异,并将差异写入到filename3中. #!/usr/bin/env python # -*- coding: utf-8 -*- # @ ...
随机推荐
- HDU2870 最大窗口面积(单调队列优化)
HDU1506 HDU2870 HDU4328 以前做的是单调队列的方法,现在试一试DP 注意的是对于i,向左延伸的L[i]到i不一定是单调的,比如1 3 2 1 2,对于i=4,L[i]=1而是2所 ...
- 51Nod 1006:最长公共子序列Lcs(打印LCS)
1006 最长公共子序列Lcs 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). ...
- (译)KVO的内部实现
09年的一篇文章,比较深入地阐述了KVO的内部实现. KVO是实现Cocoa Bindings的基础,它提供了一种方法,当某个属性改变时,相应的objects会被通知到.在其他语言中,这种观察者模 ...
- Spring Could 问题
作为流行的微服务框架,Spring Could实用但不完美,比如说它只针对Java语言,坚持REST协议做微服务间的通讯等. Spring Cloud虽然集成了众多组件,可以构建一个完整的微服务应用, ...
- Mysql root 用户密码忘记后重置root密码
[windows] 1.停止mysql服务:打开命令行窗口CMD,Net stop mysql 2.用另外一种方式启动Mysql:在命令行进入到mysql的安装路径下的bin目录下使用 mysqld- ...
- [正经分析] DAG上dp两种做法的区别——拓扑序与SPFA
在下最近刷了几道DAG图上dp的题目. 要提到的第一道是NOIP原题<最优贸易>.这是一个缩点后带点权的DAG上dp,它同时规定了起点和终点. 第二道是洛谷上的NOI导刊题目<最长路 ...
- (转)2009-05-25 22:12 Outlook2007选择发送帐号
本文转载自:http://hi.baidu.com/vugwggogodaenqe/item/c95c6d019457a2d873e676ec outlook2007可以用程序选择发送帐号,其他的版本 ...
- 事务的ACID和四个隔离级别
在实际的业务场景中,并发读写引出了和事务控制的需求.优秀的事务处理能力是关系型数据库(特别是oracle等商用RDBMS)相对于正当风口的NoSQL数据库的一大亮点.但这也从另一方面说明了事务控制的复 ...
- 【UVA】10891 Game of Sum(区间dp)
题目 传送门:QWQ 分析 大力dp.用$ dp[i][j] $表示$ [i,j] $A能得到的最高分 我看到博弈论就怂... 代码 #include <bits/stdc++.h> us ...
- jquery使用js的一些疼处
使用javascript的一些疼处 书写繁琐,代码量大 代码复杂 动画效果,很难实现.使用定时器 各种操作和处理 HTML <button id="btn">按钮< ...