此为开源代码,可以google上下载,项目名为:PySocks-1.0a4
下面为其中文件之一的IPv4_Tools.py:
"""IPv4_Tools - IPv4 helper functions module written in Python
Copyright (C) 2001 Xavier Lagraula
See COPYRIGHT.txt and GPL.txt for copyrights information.
This module provides a small set of classes and short functions to ease for
IPv4 protocols handling:
- is_routable: checks whether an IP address is routable or not (RFC 1918).
- is_port: checks whether an integer is a valid port number (1-65535)
"""
def is_routable(address):
"""def is_routable(address)
This function returns if a given IPv4 address is routable or not.
Parameters:
- address: IPv4 address - string - format: aaa.bbb.ccc.ddd
Return value:
- 0: address is not routable
- 1: address is routable
Routable addresses are defined as not pertaining to the following:
127.0.0.0 - 127.255.255.255 (127/8 prefix)
10.0.0.0 - 10.255.255.255 (10/8 prefix)
172.16.0.0 - 172.31.255.255 (172.16/12 prefix)
192.168.0.0 - 192.168.255.255 (192.168/16 prefix)"""
# Splitting the address in its 4 components.
first, second, junk1, junk2 = address.split('.')
# Testing the address against the given intervals.
if (first in ['10', '127']
or (first == '172' and second >= '16' and second <= '31')
or ((first, second) == ('192', '168'))):
return 0
return 1
def is_port(port):
"""def is_port(port)
This functions returns if a given port is valid or not.
Parameters:
- port: integer
Return value:
- 0: port is a valid port
- 1: port is not a valid port
Valid ports are defined as in the interval 1-65535."""
return (port > 0) and (port < 65536)
注意两个"""之间的部分为注释,类似与C++中的/*和*/
除掉注释,此代码只有如下几行:
def is_routable(address):
first, second, junk1, junk2 = address.split('.')
if (first in ['10', '127']
or (first == '172' and second >= '16' and second <= '31')
or ((first, second) == ('192', '168'))):
return 0
return 1
def is_port(port):
return (port > 0) and (port < 65536)
这几行代码很简单:函数is_routable主要是检测address是否可路由,声明四个变量分别得到IPv4地址的四个byte,然后判断各个byte是否符合要求,符合要求就返回1,否则返回0;函数is_port返回port是否为合格的端口号。