Category Archives: Python

peewee解决问题"OperationalError: (2006, 'MySQL server has gone away')"

用过MySQL的应该都知道,MySQL默认长链接只能保持8小时,超过后就会自动断开。

在peewee中如何维持长连接呢?

解决方法比较晦涩,需要自定义一个支持重试的mixin,然后自定义一种RetryMySQLDatabase混入mixin
from peewee import *
from peewee import __exception_wrapper__

class RetryOperationalError(object):

def execute[......]

继续阅读

Python获取外网ip地址

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('8.8.8.8', 80))
print(s.getsockname()[0])
s.close()
备注:这个IP有可能被墙,可以换一个国内的,呵呵。。

如果是NAT出口的,显示的是NAT的IP地址。[......]

继续阅读