Python Security 入门

来源:互联网 发布:星际淘宝网 编辑:程序博客网 时间:2024/05/21 05:08

原文地址,我自己摘取修改了部分章节

这篇文章假定你的系统是Linux,python版本是2.*。

你可以使用内置的help()函数去了解一个函数的详细。记住这一点,它可以帮助你在学习语言的时候学习到更多的详细内容.
help(type)

有时你会想把一些变量和字符串连接起来然后通过脚本显示出来。那么你就需要使用str()函数把整型转换成字符串类型

ip = '1.1.1.1'port = 55print 'the ip is:'+ip+'and the port is:'+str(port)

结果:
the ip is:1.1.1.1and the port is:55

Python字符串允许你通过偏移值来获取你想需要的字符串,并且可以通过len()函数来获取字符串的长度,它可以帮助你更方便的操作字符串。

domain = 'primalsecurity.net'print domain[0]print domain[0:3]print domain[1:]print len(domain)

结果:
p
pri
rimalsecurity.net
18

split函数把一个字符串通过”:”切割生成一个新的列表。这是一个非常有用的字符串函数因为你能够把这个字符串里面的有用信息提出出来。例如,你获取到了一个ip列表,你想在这个列表里面添加一个索引值。你也可以删除和添加新的值到这个列表里面通过.append().remove()函数

ip = '1.1.1.1'port = 55string = ip+':'+str(port)print stringstring = string.split(':')print stringstring.append('google')print stringstring.remove('google')print string

结果:
1.1.1.1:55
[‘1.1.1.1’, ‘55’]
[‘1.1.1.1’, ‘55’, ‘google’]
[‘1.1.1.1’, ‘55’]

Python模块

Python有许多有用的内建模块(os,subprocess,socket,urllib,httplib,re,sys等等)和第三方模块(cymruwhois,scapy,dpkt,spider等等).使用Python模块很简单”import ”

OS模块是非常重要的因为你需要在你的Python代码里面调用系统命令
OS模块给你提供了很多可以使用的功能函数,其中我发现我经常使用”os.system”,我可给它传递一个命令,然后通过它去在系统底层执行我们传递的命令.下面我们将会执行一个命令
echo ‘UHJpbWFsIFNlY3VyaXR5Cg==’ | base64 -d
结果:
Primal Security

创建一个文件对象

下面的这个例子演示了如何创建一个文件对象,并且读取/写入数据到这个对象里面,通常你自己读取一个文件的数据,并且做一些逻辑处理然后把输出的写到文件里面

file = open('test.txt', 'w')file.write('Hello World')file.close()    file = open('test.txt', 'r')print file.readlines()

结果:
[‘Hello World’]

最好我们来介绍一下sys模块,它可以让你读取从命令终端输入的值并且帮你引入到脚本里面,它的语法很简单,sys.agrv[0]就是一个实际的脚本名,并在命令行指定的每个参数后面分配一个下标

使用sys处理命令行输入值

import sysscript = sys.argv[0]ip = sys.argv[1]port = sys.argv[2]print "[+] The script name is: "+scriptprint "[+] The IP is: "+ip+" and the port is: "+port

结果:
~$ python sys.py 8.8.8.8 53
[+] The script name is: sys.py
[+] The IP is: 8.8.8.8 and the port is: 53

0 0
原创粉丝点击