def __setitem__(self, key, value): pickled = self.encode(value) if key in self: sql_session.execute(self.table.update().values(data=pickled).where(self.table.c.session_id == key)) else: sql_session.execute(self.table.insert().values(session_id=key, data=pickled)) sql_session.commit()
def saveIp(ip): """ ip统计,将ip的地点入库; sina的api """ if web.config.debug: d = {'country': u'书记', 'province': u'书记', 'city': u'书记'} else: url = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=%s' % ip d = json.loads(urllib2.urlopen(url).read()) try: position = d['country'] + '@' + d['province'] + '@' + d['city'] except KeyError: position = '@@' try: city = sql_session.query(Ip).filter_by(ip=ip)[0] city.visit += 1 except IndexError: city = Ip(ip, 1, position) sql_session.add(city) sql_session.commit() return d['city']