这是开源代码,可以在谷歌上下载。 项目名称为PySocks-1.0a4
以下是其中一个文件IPv4_Tools.py :
' ' IP v4 _ tools-IP v4 helperfunctionsmodulewritteninpython
版权(c ) 2001 Xavier语言
see copyright.txtandgpl.txtforcopyrightsinformation。
thismoduleprovidesasmallsetofclassesandshortfunctionstoeasefor
IP v4协议处理:
- is _ routable 3360 checkswhetheranipaddressisroutableornot (RFC 1918 )。
- is _ port : checkswhetheranintegerisavalidportnumber (1- 65535 )。
''''
defis _ routable (地址) :
' ' ' defis _ routable (地址) )。
thisfunctionreturnsifagivenipv4addressisroutableornot。
Parameters:
- address 3360 IP v4地址字符串格式: AAA.BBB.CCC.DDD
Return value:
- 0: address is not routable
- 1:地址is routable
routableaddressesaredefinedasnotpertainingtothefollowing :
127.0.0.0-127.255.255.255 (127/8前缀)。
10.0.0.0-10.255.255.255 (10/8前缀)。
172.16.0.0-172.31.255.255 (172.16/12前缀)。
192.168.0.0-192.168.255.255 (192.168/16 prefix (' '
# splittingtheaddressinits 4组件。
first,second,junk1,junk2=address.split ('.' ) )。
# testingtheaddressagainstthegivenintervals。
if (第一个in (' 10 )、' 127 ) )。
or (first==' 172 ' andsecond=' 16 ' andsecond=' 31 ' )
or () first,second )==('192 ',' 168 ' ) ) ) :
返回0
返回1
EFis_port(port ) :
' ' ' defis_port(port ) )。
thisfunctionsreturnsifagivenportisvalidornot。
Parameters:
- port: integer
Return value:
- 0:端口is a valid端口
- 1:端口is not a valid port
validportsaredefinedasintheinterval1- 65535.' ' '
返回(端口0 )和端口65536 ) )。
两个“”之间的部分是注释,c的/*和*/
删除注释后,此代码将变为以下行:
defis _ routable (地址) :
first,second,junk1,junk2=address.split ('.' ) )。
if (第一个in (' 10 )、' 127 ) )。
or (first==' 172 ' andsecond=' 16 ' andsecond=' 31 ' )
or () first,second )==('192 ',' 168 ' ) ) ) :
返回0
返回1
EFis_port(port ) :
返回(端口0 )和端口65536 ) )。
这几行代码很简单。 函数is_routable主要检测地址是否可路由,声明四个变量分别得到IPv4地址的四个字节,判断每个字节是否满足要求,如果满足要求则为1,如此函数is_port返回端口是否是有效的端口号。
共享到:
2009-05-20 00:29
浏览1277
评论