api.py 文件源码

python
阅读 25 收藏 0 点赞 0 评论 0

项目:fanpy 作者: mookrs 项目源码 文件源码
def _handle_response(self, req, uri, arg_data, _timeout=None):
        kwargs = {}
        if _timeout:
            kwargs['timeout'] = _timeout
        try:
            handle = urllib_request.urlopen(req, **kwargs)
            if handle.headers['Content-Type'] in ['image/jpeg', 'image/png', 'image/gif']:
                print(handle.headers['Content-Type'])
                return handle
            try:
                data = handle.read()
            except http_client.IncompleteRead as e:
                # Even if we don't get all the bytes we should have there
                # may be a complete response in e.partial
                data = e.partial
            if handle.info().get('Content-Encoding') == 'gzip':
                # Handle gzip decompression.
                buf = BytesIO(data)
                f = gzip.GzipFile(fileobj=buf)
                data = f.read()
            if len(data) == 0:
                return wrap_response({}, handle.headers)
            elif 'json' == self.format:
                res = json.loads(data.decode('utf-8'))
                return wrap_response(res, handle.headers)
            else:
                return wrap_response(
                    data.decode('utf-8'), handle.headers)
        except urllib_error.HTTPError as e:
            if (e.code == 304):
                return []
            else:
                raise FanfouHTTPError(e, uri, self.format, arg_data)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号