コード例 #1
0
 def on_message(self, msg):
     #pdb.set_trace()
     if hasattr(msg, "kind"):
         if msg.kind == 'message':
             self.write_message(str(msg.body))
         if msg.kind == 'disconnect':
             self.write_message('The connection terminated '
                                'due to a Redis server error.')
             self.close()
     else:
         msg = json.loads(msg)
         if msg.get("type", None) == "auth" and not self.uid:
             uid = msg.get("uid", None)
             if uid:
                 with LogicContext():
                     r = get_context().get_redis()
                     res = r.sadd("curusrs", uid)
                     if res:
                         self.uid = uid
                         self.listen(uid)
                         bc(
                             r,
                             json.dumps({
                                 "type": "contactref",
                                 "uid": uid,
                                 "action": "add"
                             }))
                         self.write_message(
                             json.dumps({
                                 "type": "authstat",
                                 "stat": 1
                             }))
                         bcm(r, self)
                         return
             self.write_message(json.dumps({"type": "authstat", "stat": 0}))
         elif msg.get("type", None) == "msg" and self.uid:
             tousr = msg.get("to", None)
             msgtxt = msg.get("msg", None)
             if tousr and msgtxt:
                 with LogicContext():
                     r = get_context().get_redis()
                     res = r.sismember("curusrs", tousr)
                     if res:
                         c.publish(
                             tousr,
                             json.dumps({
                                 "type": "msg",
                                 "from": self.uid,
                                 "msg": msgtxt
                             }))
                         self.write_message(json.dumps({"stat": 1}))
                         return
             self.write_message(json.dumps({"stat": 0}))
コード例 #2
0
 def on_close(self):
     if self.client.subscribed:
         with LogicContext():
             r = get_context().get_redis()
             r.srem("curusrs", self.uid)
             bc(
                 r,
                 json.dumps({
                     "type": "contactref",
                     "uid": self.uid,
                     "action": "rm"
                 }))
             self.client.unsubscribe(self.uid)
             self.client.disconnect()
コード例 #3
0
                    rr, vv = add(c['name'], v['_id'], 1)
                    xs = self.get_citys(c['id'])
                    for x in xs:
                        print '****', x['name']
                        rrr, vvv = add(x['name'], vv['_id'], 2)

    def get_citys(self, fid=0):
        url = self._url.format(fid)
        doc = pyq(url=url)
        text = doc.text()[21:-1]
        try:
            return json.loads(text)
        except:
            print text
            return []


class DemoCase(TestCase):
    def setUp(self):
        pass

    def testprint(self):
        print m_page('city')


if __name__ == '__main__':
    case = CityCate()
    reflesh_config('setting.py')
    with LogicContext():
        case.test_add()