예제 #1
0
    def __init__(self, agent=None, timeout=60, keep_alive=1,
            default_headers={}, max_redirect=7, with_exceptions=False):

        self.timeout = timeout
        self.max_redirect = max_redirect
        self._handlers = Handlers()
        self._with_exceptions = with_exceptions

        if agent is None:
            self.agent = "python-httpclient/{version}".format(
                version=__version__)
        else:
            self.agent = agent

        if len(default_headers) == 0:
            default_headers = [(
                'Connection', 'keep-alive',
            )]

        self._default_headers = Headers(default_headers)

        if 'User-Agent' not in default_headers:
            self._default_headers.add('User-Agent', self.agent)
            self._default_headers.add('User-Agent', 'foooo')

        self._poolmanager = PoolManager(
            maxsize=keep_alive
        )
예제 #2
0
 def test_unexisting_phase(self):
     handler = Handlers()
     with self.assertRaises(Exception) as cm:
         handler.handlers('foo')
     exception = cm.exception
     self.assertTrue(exception)
     pass
예제 #3
0
    def test_add_remove_handler(self):
        handlers = Handlers()
        handlers.add_handler('request_send', _cb_request_send)
        self.assertTrue(handlers.handlers('request_send'))

        handlers.remove_handler('request_send')
        self.assertFalse(handlers.handlers('request_send'))
예제 #4
0
    def test_overwrite_handlers(self):
        request = Request('GET', 'http')
        handlers = Handlers()
        handlers.add_handler('request_send', lambda r: 1)
        resp = handlers.dispatch('request_send', request)
        self.assertEqual(resp, 1)

        handlers.add_handler('request_send', lambda r: 2)
        resp = handlers.dispatch('request_send', request)
        self.assertEqual(resp, 2)
예제 #5
0
    def test_prepare(self):
        request = Request('FOO', 'http')
        client = HTTPClient()
        client.add_handler('request_prepare', _cb_request_prepare)

        resp = client.request(request)
        self.assertEqual(resp.status, 400)

        handlers = Handlers()
        handlers.add_handler('request_prepare',
                             _cb_request_prepare_change_request)
        req = handlers.dispatch('request_prepare', request)
        self.assertEqual(req.method, 'PUT')

        handlers = Handlers()
        handlers.add_handler('request_prepare', _cb_request_prepare_do_nothing)
        req = handlers.dispatch('request_prepare', request)
        self.assertEqual(req.method, 'FOO')
        self.assertEqual(req.url, 'http')
예제 #6
0
class HTTPClient(object):

    def __init__(self, agent=None, timeout=60, keep_alive=1,
            default_headers={}, max_redirect=7, with_exceptions=False):

        self.timeout = timeout
        self.max_redirect = max_redirect
        self._handlers = Handlers()
        self._with_exceptions = with_exceptions

        if agent is None:
            self.agent = "python-httpclient/{version}".format(
                version=__version__)
        else:
            self.agent = agent

        if len(default_headers) == 0:
            default_headers = [(
                'Connection', 'keep-alive',
            )]

        self._default_headers = Headers(default_headers)

        if 'User-Agent' not in default_headers:
            self._default_headers.add('User-Agent', self.agent)
            self._default_headers.add('User-Agent', 'foooo')

        self._poolmanager = PoolManager(
            maxsize=keep_alive
        )

    def HTTPException(fn):
        def wrapper(*args):
            res = fn(*args)
            if args[0]._with_exceptions:
                if res.is_error:
                    raise http_exception(res)
            return res
        return wrapper

    def add_handler(self, position, cb):
        self._handlers.add_handler(position, cb)

    def remove_handler(self, position):
        self._handler.remove_handler(position)

    def default_header(self, key):
        return self.default_headers.get('key')

    @property
    def default_headers(self):
        return self._default_headers

    @HTTPException
    def request(self, request):
        return self._request(request)

    @HTTPException
    def head(self, url, headers={}):
        request = Request('HEAD', url, headers=headers)
        return self._request(request)

    @HTTPException
    def get(self, url, headers={}):
        request = Request('GET', url, headers=headers)
        return self._request(request)

    @HTTPException
    def put(self, url, headers={}, content=None):
        request = Request('PUT', url, headers=headers, content=content)
        return self._request(request)

    @HTTPException
    def post(self, url, headers={}, content=None):
        request = Request('POST', url, headers=headers, content=content)
        return self._request(request)

    @HTTPException
    def delete(self, url, headers={}, content=None):
        request = Request('DELETE', url, headers=headers)
        return self._request(request)

    @HTTPException
    def mirror(self, url, filename):
        req = Request('GET', url)
        res = self.request(req)
        if res.is_success:
            f = open(filename, 'w')
            f.write(res.content)
            f.close()
            last_modified = Date.time2epoch(res.last_modified)
            if last_modified:
                os.utime(file, (last_modified, last_modified))

    def _request(self, request):

        try:
            resp = self._make_request(request)
        except Exception, e:
            print e
            raise e

        if resp.is_redirect and len(resp.redirects) < self.max_redirect:
            try:
                return self._follow_redirect(resp, request)
            except Exception, e:
                raise e
예제 #7
0
 def test_execute_handler(self):
     request = Request('GET', 'http')
     handlers = Handlers()
     handlers.add_handler('request_send', _cb_request_send)
     resp = handlers.dispatch('request_send', request)
     self.assertIsInstance(resp, Response)
예제 #8
0
 def test_simple_handler(self):
     handlers = Handlers()
     self.assertTrue(handlers)