示例#1
0
 def _callback_receive(self, resp, previous):
     tag = 'Listener'
     try:
         resp = json.loads(resp.decode('utf-8'))
         if resp.get('retcode', 0) != 0:
             # something is wrong
             log.e(tag, 'error retcode %d errmsg %s' % (
                 resp.get('retcode', 0), resp.get('errmsg', 'none')))
             if resp.get('retcode', 0) == 103:
                 # Connection failed, log in again.
                 log.w(tag, 'Meet with error 103.')
                 log.w(tag, 'Please log in again')
                 exit()
         else:
             if resp.get('errmsg') is not None:
                 # {errmsg: "error!!!", retcode: 0}
                 # time exceeds every minute, just ignore it.
                 log.v(tag, 'listening...')
                 return
             for c in resp['result']:
                 num = c['value']['from_uin']
                 msg = c['value']['content'][1]
                 if c['poll_type'] == 'message':
                     log.v(tag, 'got message from friend ' + str(num))
                     for h in self.handlers:
                         h.on_buddy_message(num, msg)
                 elif c['poll_type'] == 'group_message':
                     log.v(tag, 'got message from group ' + str(num))
                     uin = c['value']['send_uin']
                     for h in self.handlers:
                         h.on_group_message(num, uin, msg)
     except Exception:
         log.e(tag, 'Fatal error parsing messages.')
         log.e(tag, resp)
         traceback.print_exc()
示例#2
0
 def _callback_receive(self, resp, previous):
     tag = 'Listener'
     try:
         resp = json.loads(resp.decode('utf-8'))
         if resp.get('retcode', 0) != 0 or resp.get('result') is None:
             # something is wrong
             log.e(tag, 'error retcode %d errmsg %s' % (
                 resp.get('retcode', 0), resp.get('errmsg', 'none')))
             if resp.get('retcode', 0) == 103:
                 log.w(tag, 'Meet with error 103.')
                 log.w(tag, 'Please login @ w.qq.com manually first.')
                 exit()
         else:
             uin = resp['result'][0]['value']['from_uin']
             msg = resp['result'][0]['value']['content'][1]
             if resp['result'][0]['poll_type'] == 'message':
                 log.v(tag, 'got message from friend ' + str(uin))
                 for h in self.handlers:
                     h.on_buddy_message(uin, msg)
             if resp['result'][0]['poll_type'] == 'group_message':
                 log.v(tag, 'got message from group ' + str(uin))
                 for h in self.handlers:
                     h.on_group_message(uin, msg)
     except Exception:
         log.e(tag, 'Fatal error parsing messages.')
         log.e(tag, resp)
         traceback.print_exc()
示例#3
0
 def req(self, url, *, data=None, headers={}):
     r = request.Request(url)
     if data is not None:
         r.data = parse.urlencode(data).encode('utf-8')
     r.headers.update(headers)
     try:
         with self.opener.open(r) as f:
             return f.read()
     except Exception:
         log.w('http_client', 'got an HTTP error.')  # ignored
示例#4
0
 def req(self, url, *, data=None, headers={}):
     r = request.Request(url)
     if data is not None:
         r.data = parse.urlencode(data).encode('utf-8')
     r.headers.update(headers)
     try:
         f = self.opener.open(r)
         return f.read()
     except Exception:
         log.w('http', 'HTTP error @ ' + url)
         traceback.print_exc()
示例#5
0
 def req(self, url, *, data=None, headers={}):
     r = request.Request(url)
     if data is not None:
         r.data = parse.urlencode(data).encode('utf-8')
     r.headers.update(headers)
     try:
         f = self.opener.open(r)
         return f.read()
     except Exception:
         log.w('http', 'HTTP error @ ' + url)
         traceback.print_exc()
示例#6
0
 def _gevent_req(self, url, data, headers):
     r = request.Request(url)
     if data is not None:
         r.data = parse.urlencode(data).encode('utf-8')
     r.headers.update(headers)
     try:
         f = self.opener.open(r)
         # hang up here
         # gevent will automatically switch to different jobs
         return f.read()
     except Exception:
         log.w('http', 'HTTP error @ ' + url)
         traceback.print_exc()
示例#7
0
    def _callback_receive(self, resp, previous):
        tag = 'listener'
        try:
            resp = json.loads(resp.decode('utf-8'))
            if resp.get('retcode', 0) != 0:
                # something is wrong
                log.e(
                    tag, 'error retcode %d errmsg %s' %
                    (resp.get('retcode', 0), resp.get('errmsg', 'none')))
                if resp.get('retcode', 0) == 103:
                    # Connection failed, log in again.
                    log.w(tag, 'Meet with error 103.')
                    log.w(tag, 'You\'ll have to log in again.')
                    exit()
            else:
                if resp.get('retcode') != 0:
                    # something is really wrong
                    log.e(
                        tag, 'retcode %s errmsg %s' %
                        (resp.get('errmsg'), resp.get('retcode')))
                    return

                if resp.get('errmsg') == 'error!!!':
                    # {errmsg: "error!!!", retcode: 0}
                    # http connection time out
                    # ignore it and continue listening
                    return

                # parse result
                for c in resp['result']:
                    num = c['value']['from_uin']  # uin or gid
                    msg = c['value']['content'][1]  # content
                    if c['poll_type'] == 'message':
                        for h in self.handlers:
                            h.on_buddy_message(num, msg)
                    elif c['poll_type'] == 'group_message':
                        uin = c['value']['send_uin']  # uin
                        for h in self.handlers:
                            h.on_group_message(num, uin, msg)
        except Exception:
            log.e(tag, 'Fatal error parsing messages.')
            log.e(tag, 'Response: ' + str(resp))
            traceback.print_exc()
示例#8
0
    def _callback_receive(self, resp, previous):
        tag = 'listener'
        try:
            resp = json.loads(resp.decode('utf-8'))
            if resp.get('retcode', 0) != 0:
                # something is wrong
                log.e(tag, 'error retcode %d errmsg %s' % (
                    resp.get('retcode', 0), resp.get('errmsg', 'none')))
                if resp.get('retcode', 0) == 103:
                    # Connection failed, log in again.
                    log.w(tag, 'Meet with error 103.')
                    log.w(tag, 'You\'ll have to log in again.')
                    exit()
            else:
                if resp.get('retcode') != 0:
                    # something is really wrong
                    log.e(tag, 'retcode %s errmsg %s' % (
                        resp.get('errmsg'),resp.get('retcode')))
                    return

                if resp.get('errmsg') == 'error!!!':
                    # {errmsg: "error!!!", retcode: 0}
                    # http connection time out
                    # ignore it and continue listening
                    return

                # parse result
                for c in resp['result']:
                    num = c['value']['from_uin'] # uin or gid
                    msg = c['value']['content'][1] # content
                    if c['poll_type'] == 'message':
                        for h in self.handlers:
                            h.on_buddy_message(num, msg)
                    elif c['poll_type'] == 'group_message':
                        uin = c['value']['send_uin'] # uin
                        for h in self.handlers:
                            h.on_group_message(num, uin, msg)
        except Exception:
            log.e(tag, 'Fatal error parsing messages.')
            log.e(tag, 'Response: ' + str(resp))
            traceback.print_exc()
示例#9
0
 def _callback_send(self, resp, previous):
     tag = 'sender'
     resp = json.loads(resp.decode('utf-8'))
     if resp.get('errCode', 0) != 0 or resp.get('retcode', 0) != 0:
         log.w(tag, 'The following error occurred when sending last message:')
         if resp.get('retcode', 0) == 1202:
             # retcode 1202: ignored, as WebQQ itself has ignored it too.
             log.w(tag, '\tretcode 1202')
             log.w(tag, '\tMessage could be lost, but usually not big deal.')
示例#10
0
 def _callback_send(self, resp, previous):
     tag = 'sender'
     resp = json.loads(resp.decode('utf-8'))
     if resp.get('errCode', 0) != 0 or resp.get('retcode', 0) != 0:
         log.w(tag,
               'The following error occurred when sending last message:')
         if resp.get('retcode', 0) == 1202:
             # retcode 1202: ignored, as WebQQ itself has ignored it too.
             log.w(tag, '\tretcode 1202')
             log.w(tag,
                   '\tMessage could be lost, but usually not big deal.')
示例#11
0
 def __init__(self):
     # mHTTPClient running gevent
     # during test
     log.w('http', "You're using gevent based http client.")
     super(mHTTPClient_gevent, self).__init__()