Python从入门到编写POC系列文章是i春秋论坛作家「Exp1ore」表哥原创的一套完整教程,想系统学习Python技能的小伙伴,不要错过哦!
常用的标准库
安装完Python之后,我们也同时获得了强大的Python标准库,通过使用这些标准库可以为我们节省大量的时间,这里是一些常用标准库的简单说明。
Python的标准库包括了很多模块,从Python语言自身特定的类型和声明,到一些只用于少数程序的不著名模块,任何大型Python程序都有可能直接或间接地使用到这类模块的大部分内容。
sys模块
咱们之前已经接触过了sys模块,sys.path.append( ),可以通过help命令查看文档,然后不断的回车查看。
或者用这个命令:
>>> print sys.__doc__
This module provides access to some objects used or maintained by the
interpreter and to functions that interact strongly with the interpreter.
Dynamic objects:
argv -- command line arguments; argv[0] is the script pathname if known
path -- module search path; path[0] is the script directory, else ''
modules -- dictionary of loaded modules
displayhook -- called to show results in an interactive session
excepthook -- called to handle any uncaught exception other than SystemExit
To customize printing in an interactive session or to install a custom
top-level exception handler, assign other functions to replace these.
exitfunc -- if sys.exitfunc exists, this routine is called when Python exits
Assigning to sys.exitfunc is deprecated; use the atexit module instead.
stdin -- standard input file object; used by raw_input() and input()
stdout -- standard output file object; used by the print statement
stderr -- standard error object; used for error messages
By assigning other file objects (or objects that behave like files)
to these, it is possible to redirect all of the interpreter's I/O.
last_type -- type of last uncaught exception
last_value -- value of last uncaught exception
last_traceback -- traceback of last uncaught exception
These three are only available in an interactive session after a
traceback has been printed.
exc_type -- type of exception currently being handled
exc_value -- value of exception currently being handled
exc_traceback -- traceback of exception currently being handled
The function exc_info() should be used instead of these three,
because it is thread-safe.
Static objects:
float_info -- a dict with information about the float inplementation.
long_info -- a struct sequence with information about the long implementation.
maxint -- the largest supported integer (the smallest is -maxint-1)
maxsize -- the largest supported length of containers.
maxunicode -- the largest supported character
builtin_module_names -- tuple of module names built into this interpreter
version -- the version of this interpreter as a string
version_info -- version information as a named tuple
hexversion -- version information encoded as a single integer
copyright -- copyright notice pertaining to this interpreter
platform -- platform identifier
executable -- absolute path of the executable binary of the Python interpreter
prefix -- prefix used to find the Python library
exec_prefix -- prefix used to find the machine-specific Python library
float_repr_style -- string indicating the style of repr() output for floats
dllhandle -- [Windows only] integer handle of the Python DLL
winver -- [Windows only] version number of the Python DLL
__stdin__ -- the original stdin; don't touch!
__stdout__ -- the original stdout; don't touch!
__stderr__ -- the original stderr; don't touch!
__displayhook__ -- the original displayhook; don't touch!
__excepthook__ -- the original excepthook; don't touch!
Functions:
displayhook() -- print an object to the screen, and save it in __builtin__._
excepthook() -- print an exception and its traceback to sys.stderr
exc_info() -- return thread-safe information about the current exception
exc_clear() -- clear the exception state for the current thread
exit() -- exit the interpreter by raising SystemExit
getdlopenflags() -- returns flags to be used for dlopen() calls
getprofile() -- get the global profiling function
getrefcount() -- return the reference count for an object (plus one
getrecursionlimit() -- return the max recursion depth for the interpreter
getsizeof() -- return the size of an object in bytes
gettrace() -- get the global debug tracing function
setcheckinterval() -- control how often the interpreter checks for events
setdlopenflags() -- set the flags to be used for dlopen() calls
setprofile() -- set the global profiling function
setrecursionlimit() -- set the max recursion depth for the interpreter
settrace() -- set the global debug tracing function
sys.argv是变量,命令行参数,专门向Python解释器传递参数他的功能是获取程序外部向程序传递的参数。
举个例子:
为了更好的理解,再写一个程序:
#coding = utf-8
import sys
def demo():
if len(sys.argv) < 2:
print 'no action'
sys.exit() #退出当前程序
for i in range(len(sys.argv)):
print 'arg['+str(i)+']',sys.argv
if __name__ == '__main__':
demo()
sys.stdin,sys.stdout,sys.stderr,处理标准输入,标准输出,标准错误。
输出和错误是内建在每个unix系统中的管道,print的本质就是sys.stdout.write。
stdout是一个类文件对象,调用了其write函数就可以打印任何的字符串了,它们不会添加回车,要自己添加\n,但是只有write的办法,没有read的方法。
还是由于是类文件对象,因此可以讲任何类文件赋值,然后重定向输出,那可能会问,啥是重定向输出,简而言之,就是将输出内容从"控制台"转到"文件"。
举个例子,进一步理解:
最后为啥多了一个\n?
是因为print会在即将要打印的字符串后面加一个硬回车。
os模块
先看一下os模块里有什么内容:
>>> import os >>> dir(os) ['F_OK', 'O_APPEND', 'O_BINARY', 'O_CREAT', 'O_EXCL', 'O_NOINHERIT', 'O_RANDOM', 'O_RDONLY', 'O_RDWR', 'O_SEQUENTIAL', 'O_SHORT_LIVED', 'O_TEMPORARY', 'O_TEXT', 'O_TRUNC', 'O_WRONLY', 'P_DETACH', 'P_NOWAIT', 'P_NOWAITO', 'P_OVERLAY', 'P_WAIT', 'R_OK', 'SEEK_CUR', 'SEEK_END', 'SEEK_SET', 'TMP_MAX', 'UserDict', 'W_OK', 'X_OK', '_Environ', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_copy_reg', '_execvpe', '_exists', '_exit', '_get_exports_list', '_make_stat_result', '_make_statvfs_result', '_pickle_stat_result', '_pickle_statvfs_result', 'abort', 'access', 'altsep', 'chdir', 'chmod', 'close', 'closerange', 'curdir', 'defpath', 'devnull', 'dup', 'dup2', 'environ', 'errno', 'error', 'execl', 'execle', 'execlp', 'execlpe', 'execv', 'execve', 'execvp', 'execvpe', 'extsep', 'fdopen', 'fstat', 'fsync', 'getcwd', 'getcwdu', 'getenv', 'getpid', 'isatty', 'kill', 'linesep', 'listdir', 'lseek', 'lstat', 'makedirs', 'mkdir', 'name', 'open', 'pardir', 'path', 'pathsep', 'pipe', 'popen', 'popen2', 'popen3', 'popen4', 'putenv', 'read', 'remove', 'removedirs', 'rename', 'renames', 'rmdir', 'sep', 'spawnl', 'spawnle', 'spawnv', 'spawnve', 'startfile', 'stat', 'stat_float_times', 'stat_result', 'statvfs_result', 'strerror', 'sys', 'system', 'tempnam', 'times', 'tmpfile', 'tmpnam', 'umask', 'unlink', 'unsetenv', 'urandom', 'utime', 'waitpid', 'walk', 'write']
然后用help( )命令,这里介绍几个常用的:
os库提供了在Python中使用操作系统的命令的方法就是用os.system()。
以下是Winods系统:
>>> command = 'dir'
>>> import os
>>> os.system(command)
驱动器 D 中的卷没有标签。
卷的序列号是 626B-88AA
D:\ichunqiu\items 的目录
2017/08/23 10:34 <DIR> .
2017/08/23 10:34 <DIR> ..
2017/08/15 21:42 7 cmd.bat
2017/08/21 21:22 521 ctf.py
2017/08/23 10:34 7,446 demo.py
2017/08/21 12:05 23 flag.txt
2017/08/20 18:08 <DIR> ichunqiu
2017/08/22 22:38 16 test.txt
5 个文件 8,013 字节
3 个目录 353,137,557,504 可用字节
0
time模块
time模块很常用的,比如说在延时注入中,就要用到它,可以精确的知道程序的运行长短。
>>> import time >>> time.time() #获取当前时间的时间戳 1503480040.985 >>> time.clock() #获取进程的时间 60.641674890547975 >>> time.localtime() #时间戳转换成当地的时间 time.struct_time(tm_year=2017, tm_mon=8, tm_mday=23, tm_hour=17, tm_min=20, tm_sec=48, tm_wday=2, tm_yday=235, tm_isdst=0) >>> time.asctime() #将元祖表示为'Wed Aug 23 17:24:07 2017'这种形式' Wed Aug 23 17:24:27 2017'
json模块
JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式。它基于ECMAScript (w3c制定的js规范)的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。
简洁和清晰的层次结构使得JSON成为理想的数据交换语言,易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。
Python标准库中有JSON模块,主要是两个功能,序列化(encoding)与反序列化(decoding)。
encoding操作:dumps( )
>>> import json >>> data = [{"username":"ichunqiu","password":"BaZong","content":("BaZong","handsome")}] >>> print data [{'username': 'ichunqiu', 'content': ('BaZong', 'handsome'), 'password': 'BaZong'}] >>> data_json = json.dumps(data) #将data进行格式的编码转换 >>> print data_json [{"username": "ichunqiu", "content": ["BaZong", "handsome"], "password": "BaZong"}]
这里的data_json是str类型,data是list类型。
decoding操作:loads( )
>>> demo_data = json.loads(data_json) #将data_json解码 >>> print demo_data [{u'username': u'ichunqiu', u'content': [u'BaZong', u'handsome'], u'password': u'BaZong'}]
hashlib模块
Python中的hashlib库提供了大量的摘要算法,又叫散列算法,哈希算法。
举个例子,计算一个md5值:
random模块
生成随机数的
>>> import random >>> dir(random) ['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST', 'SystemRandom', 'TWOPI', 'WichmannHill', '_BuiltinMethodType', '_MethodType', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_acos', '_ceil', '_cos', '_e', '_exp', '_hashlib', '_hexlify', '_inst', '_log', '_pi', '_random', '_sin', '_sqrt', '_test', '_test_generator', '_urandom', '_warn', 'betavariate', 'choice', 'division', 'expovariate', 'gammavariate', 'gauss', 'getrandbits', 'getstate', 'jumpahead', 'lognormvariate', 'normalvariate', 'paretovariate', 'randint', 'random', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 'triangular', 'uniform', 'vonmisesvariate', 'weibullvariate']
urllib/urlib2这两个模块,感兴趣的小伙伴可以在网上自学哦。
以上是今天要分享的内容,大家看懂了吗?
猜你还喜欢
- 随机文章
-
- 让你的SQL盲注快起来
- Goby——新一代安全工具
- 对一次钓鱼事件的分析
- 任意文件读取漏洞的曲折历程
- SSH暴力破解姿势总结
- Shiro权限绕过漏洞分析(CVE-2020-1957)
- SMBGhost漏洞允许Windows系统上的提权;Holy Water黑客组织瞄准亚洲的宗教人物和慈善机构;伊朗非官方版本Telegram应用用户的4200万记录在线泄漏
- HackingTeam新活动,Scout/Soldier重回视野
- 聊一聊二维码扫描登录原理
- WAF机制及绕过方法总结:注入篇
- Syborg:一款带有断路躲避系统的DNS子域名递归枚举工具
- JMX远程代码漏洞研究
- 玩家与黑客:对游戏公司和游戏玩家的网络威胁
- 网络犯罪天堂Deer.io之死
- PrivescCheck:一款针对Windows系统的提权枚举脚本
- 实战黑客入侵网站教程轻松拿下1000个网站
- 实战案例:精准入侵号码交易网与黑客远程定位
- 文件解压引发的Getshell
- 如何解密AWVS?15行代码就够了!
- NSA披露Web Shell漏洞列表,警惕黑客部署后门
- 热门文章
-
- 八年专业安全团队承接渗透入侵维护服务
- Emlog黑客站模板“Milw0rm”发布
- Stuxnet纪录片-零日 Zero.Days (2016)【中文字幕】
- SQLMAP的注入命令以及使用方法
- 白帽故事汇:网络安全战士从来不是「男生」的专利
- 编辑器漏洞手册
- web安全之如何全面发现系统后台
- 常见Web源码泄露总结
- 渗透测试培训(第五期)
- 深入理解JAVA反序列化漏洞
- cmseasy前台无需登录直接获取敏感数据的SQL注入(有POC证明)
- 网站后台登陆万能密码
- 黑麒麟2016渗透培训系列教程
- 破解emlog收费模板“Begin”
- 那些强悍的PHP一句话后门
- Android平台渗透测试套件zANTI v2.5发布(含详细说明)
- 渗透工具BackTrack与KaliLinux全套视频教程
- Python列为黑客应该学的四种编程语言之一 初学者该怎么学
- CVE-2017-11882漏洞复现和利用
- 恶意程序报告在线查询工具
文章存档
- 2021年3月(4)
- 2020年12月(4)
- 2020年11月(5)
- 2020年10月(8)
- 2020年9月(8)
- 2020年8月(20)
- 2020年7月(47)
- 2020年6月(70)
- 2020年5月(41)
- 2020年4月(21)
- 2020年3月(120)
- 2020年2月(26)
- 2019年12月(12)
- 2019年11月(13)
- 2019年10月(17)
- 2019年9月(15)
- 2019年8月(13)
- 2019年7月(15)
- 2019年6月(15)
- 2019年5月(19)
- 2019年4月(23)
- 2019年3月(19)
- 2019年2月(11)
- 2019年1月(29)
- 2018年12月(24)
- 2018年11月(56)
- 2018年10月(79)
- 2018年9月(20)
- 2018年8月(17)
- 2018年7月(16)
- 2018年6月(7)
- 2018年5月(10)
- 2018年3月(6)
- 2018年2月(2)
- 2018年1月(11)
- 2017年11月(18)
- 2017年10月(6)
- 2017年9月(8)
- 2017年8月(7)
- 2017年7月(7)
- 2017年6月(15)
- 2017年5月(30)
- 2017年4月(7)
- 2017年3月(1)
- 2017年2月(4)
- 2017年1月(1)
- 2016年12月(3)
- 2016年11月(7)
- 2016年10月(6)
- 2016年9月(6)
- 2016年8月(102)
- 2016年7月(24)
- 2013年7月(1)
- 文章标签
-