예제 #1
0
    def request(self, method, url, params=None):
        body = None
        if params:
            if method == 'GET':
                url = '{path}?{params}'.format(path=url,
                                               params=urlencode(params))
            else:
                body = urlencode(params)
        debug('GET {0} HTTP/1.1'.format(url))

        def _request():
            self.http.request(method, url, body, headers=self._headers)
            return self.http.getresponse()

        try:
            try:
                r = _request()
            except (CannotSendRequest, BadStatusLine), e:
                debug('exception {0}, reconnecting...'.format(e))
                self.http.close()
                self.http.connect()
                r = _request()
        except HTTPException, e:
            self.http.close()
            raise NetworkError('HTTP request error: {0}'.format(
                type(e).__name__))
예제 #2
0
파일: convore.py 프로젝트: mrj10/convoread
    def request(self, method, url, params={}):
        body = None
        if params:
            if method == 'GET':
                url = '{path}?{params}'.format(path=url,
                                               params=urlencode(params))
            else:
                body = urlencode(params)
        debug('GET {0} HTTP/1.1'.format(url))

        def _request():
            self.http.request(method, url, body, headers=self._headers)
            return self.http.getresponse()

        try:
            try:
                r = _request()
            except (CannotSendRequest, BadStatusLine), e:
                debug('exception {0}, reconnecting...'.format(e))
                self.http.close()
                self.http.connect()
                r = _request()
        except HTTPException, e:
            self.http.close()
            raise NetworkError('HTTP request error: {0}'.format(
                    type(e).__name__))
예제 #3
0
파일: console.py 프로젝트: mrj10/convoread
 def sendmsg(self, msg):
     if not self.topic:
         error('no topic set, type /help for more info')
         return
     msg = msg.strip()
     if not msg:
         return
     debug('sending "{0}"...'.format(msg))
     try:
         self.convore.send_message(self.topic, msg)
     except NetworkError, e:
         error(unicode(e))
예제 #4
0
 def sendmsg(self, msg):
     if not self.topic:
         error('no topic set, type /help for more info')
         return
     msg = msg.strip()
     if not msg:
         return
     debug('sending "{0}"...'.format(msg))
     try:
         self.convore.send_message(self.topic, msg)
     except NetworkError, e:
         error(unicode(e))
예제 #5
0
파일: notify.py 프로젝트: ptone/convoread
    def imgpath(self, user):
        if not self.Image:
            return

        try:
            img = user['img']
        except KeyError:
            return None

        filename = '{hash}.jpg'.format(hash=sha1(img).hexdigest())
        path = os.path.join(self._tmpdir, filename)

        if not os.path.exists(path):
            debug('GET {url} HTTP/1.1'.format(url=img))
            with closing(urlopen(img)) as src:
                blocks = iter(lambda: src.read(4096), b'')
                with closing(open(path, 'wb')) as dst:
                    for block in blocks:
                        dst.write(block)
            img = self.Image.open(path)
            img.thumbnail((64, 64), self.Image.ANTIALIAS)
            img.save(path)

        return path
예제 #6
0
파일: notify.py 프로젝트: ptone/convoread
    def imgpath(self, user):
        if not self.Image:
            return

        try:
            img = user['img']
        except KeyError:
            return None

        filename = '{hash}.jpg'.format(hash=sha1(img).hexdigest())
        path = os.path.join(self._tmpdir, filename)

        if not os.path.exists(path):
            debug('GET {url} HTTP/1.1'.format(url=img))
            with closing(urlopen(img)) as src:
                blocks = iter(lambda: src.read(4096), b'')
                with closing(open(path, 'wb')) as dst:
                    for block in blocks:
                        dst.write(block)
            img = self.Image.open(path)
            img.thumbnail((64, 64), self.Image.ANTIALIAS)
            img.save(path)

        return path
예제 #7
0
                r = _request()
        except HTTPException, e:
            self.http.close()
            raise NetworkError('HTTP request error: {0}'.format(
                type(e).__name__))
        except socket.gaierror:
            msg = 'cannot get network address for "{host}"'.format(
                host=self.http.host)
            raise NetworkError(msg)
        except socket.error, e:
            self.http.close()
            raise NetworkError(e.args[1])

        status_msg = '{status} {reason}'.format(status=r.status,
                                                reason=r.reason)
        debug('HTTP/1.1 {0}'.format(status_msg))
        if r.status // 100 != 2:
            self.http.close()
            raise NetworkError('server error: {0}'.format(status_msg))

        try:
            data = r.read().decode(NETWORK_ENCODING)
            res = json.loads(data)
            debug('response in JSON\n{msg}'.format(
                msg=json.dumps(res, ensure_ascii=False, indent=4)))
            return res
        except ValueError:
            raise NetworkError('bad server response: {0}'.format(data))

    def close(self):
        self.http.close()
예제 #8
0
파일: convore.py 프로젝트: mrj10/convoread
                r = _request()
        except HTTPException, e:
            self.http.close()
            raise NetworkError('HTTP request error: {0}'.format(
                    type(e).__name__))
        except socket.gaierror:
            msg = 'cannot get network address for "{host}"'.format(
                    host=self.http.host)
            raise NetworkError(msg)
        except socket.error, e:
            self.http.close()
            raise NetworkError(e.args[1])

        status_msg = '{status} {reason}'.format(status=r.status,
                                                reason=r.reason)
        debug('HTTP/1.1 {0}'.format(status_msg))
        if r.status // 100 != 2:
            self.http.close()
            raise NetworkError('server error: {0}'.format(status_msg))

        try:
            data = r.read().decode(config['NETWORK_ENCODING'])
            res = json.loads(data)
            debug('response in JSON\n{msg}'.format(
                msg=json.dumps(res, ensure_ascii=False, indent=4)))
            return res
        except ValueError:
            raise NetworkError('bad server response: {0}'.format(data))


    def close(self):