Python IP转数字,数字转IP

def IntToDottedIP( intip ):
    octet = ''
    for exp in [3,2,1,0]:
        octet = octet + str(intip / ( 256 ** exp )) + "."
        intip = intip % ( 256 ** exp )
    return(octet.rstrip('.'))
 
def DottedIPToInt( dotted_ip ):
    exp = 3
    intip = 0
    for quad in dotted_ip.split('.'):
        intip = intip + (int(quad) * (256 ** exp))
        exp = exp - 1
    return(intip)
Via:Chairo@2011年09月30日-EOF-

tornado获取客户端IP

网上搜到的用tornado获取客户端IP都使用self.request.headers['X-Real-Ip'],但实际翻tornado源代码发现只需要获取self.request.remote_ip即可取到客户端IP

见引用tornado源代码(对应文件为httpserver.py中HTTPRequest属性remote_ip)

if connection and connection.xheaders:
            # Squid uses X-Forwarded-For, others use X-Real-Ip
            self.remote_ip = self.headers.get(
                "X-Real-Ip", self.headers.get("X-Forwarded-For", remote_ip))
            # AWS uses X-Forwarded-Proto
            self.protocol = self.headers.get(
                "X-Scheme", self.headers.get("X-Forwarded-Proto", protocol))
            if self.protocol not in ("http", "https"):
                self.protocol = "http"
        else:
            self.remote_ip = remote_ip
            if protocol:
                self.protocol = protocol
            elif connection and isinstance(connection.stream, 
                                           iostream.SSLIOStream):
                self.protocol = "https"
            else:
                self.protocol = "http"

Via:Chairo@2011年09月29日-EOF-

python中百分号替换参数

"%"用于格式化字符串时指定一些替换参数。

字符串格式化字符:


字符

说明

d或l

十进制整数

f

浮点数

s

字符串或对象的.str()返回串

c

单个字符

u

无符号十进制整数

X或x

十六进制整数

o

八进制整数

e或E

指数格式的浮点数

g或G

按照较短的格式自选%f或%e

r

对象的repr()版本

%

使用%%打印百分号

Via:Chairo@2011年09月29日-EOF-

配置MySQL-Python的时候系统报错mysql_config not found

配置MySQL-Python的时候系统报错,提示:

EnvironmentError: mysql_config not found

Google后得知mysql_config是属于MySQL开发用的文件,而使用apt-get安装的MySQL是没有这个文件的,于是在包安装器里面寻找

libmysqld-dev

libmysqlclient-dev

这两个包安装后问题即可解决

Via:Chairo@2011年09月27日-EOF-

python内建函数id()

python的内建函数id()返回的是对象的内存地址.

Via:Chairo@2011年09月21日-EOF-