Python tornado.httpclient.AsyncHTTPClient() Examples

The following are 30 code examples of tornado.httpclient.AsyncHTTPClient(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tornado.httpclient , or try the search function .
Example #1
Source File: okpy.py    From oauthenticator with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def authenticate(self, handler, data=None):
        code = handler.get_argument("code", False)
        if not code:
            raise web.HTTPError(400, "Authentication Cancelled.")
        http_client = AsyncHTTPClient()
        auth_request = self.get_auth_request(code)
        response = await http_client.fetch(auth_request)
        if not response:
            raise web.HTTPError(500, 'Authentication Failed: Token Not Acquired')
        state = json.loads(response.body.decode('utf8', 'replace'))
        access_token = state['access_token']
        info_request = self.get_user_info_request(access_token)
        response = await http_client.fetch(info_request)
        user = json.loads(response.body.decode('utf8', 'replace'))
        # TODO: preserve state in auth_state when JupyterHub supports encrypted auth_state
        return {
            'name': user['email'],
            'auth_state': {'access_token': access_token, 'okpy_user': user},
        } 
Example #2
Source File: globus.py    From oauthenticator with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def revoke_service_tokens(self, services):
        """Revoke live Globus access and refresh tokens. Revoking inert or
        non-existent tokens does nothing. Services are defined by dicts
        returned by tokens.by_resource_server, for example:
        services = { 'transfer.api.globus.org': {'access_token': 'token'}, ...
            <Additional services>...
        }
        """
        access_tokens = [token_dict.get('access_token') for token_dict in services.values()]
        refresh_tokens = [token_dict.get('refresh_token') for token_dict in services.values()]
        all_tokens = [tok for tok in access_tokens + refresh_tokens if tok is not None]
        http_client = AsyncHTTPClient()
        for token in all_tokens:
            req = HTTPRequest(self.revocation_url,
                              method="POST",
                              headers=self.get_client_credential_headers(),
                              body=urllib.parse.urlencode({'token': token}),
                              )
            await http_client.fetch(req) 
Example #3
Source File: crawler.py    From kimsufi-crawler with MIT License 6 votes vote down vote up
def __init__(self, state_change_callback):
        # set properties
        self.state_change_callback = state_change_callback

        # load mappings
        self.SERVER_TYPES = parse_json_file(
            os.path.join(CURRENT_PATH, 'mapping/server_types.json'))
        self.REGIONS = parse_json_file(
            os.path.join(CURRENT_PATH, 'mapping/regions.json'))

        # set private vars
        self.API_URL = ("https://ws.ovh.com/dedicated/r2/ws.dispatcher"
                        "/getAvailability2")
        self.STATES = {}
        self.HTTP_ERRORS = []
        self.interval = 8   # seconds between interations
        self.periodic_cb = None
        self.ioloop = None
        self.http_client = AsyncHTTPClient() 
Example #4
Source File: tornado-crawler-demo1.py    From Python_Master_Courses with GNU General Public License v3.0 6 votes vote down vote up
def get_links_from_url(url):
    """Download the page at `url` and parse it for links.

    Returned links have had the fragment after `#` removed, and have been made
    absolute so, e.g. the URL 'gen.html#tornado.gen.coroutine' becomes
    'http://www.tornadoweb.org/en/stable/gen.html'.
    """
    try:
        response = yield httpclient.AsyncHTTPClient().fetch(url)#获取到
        print('fetched %s' % url)


        html = response.body if isinstance(response.body, str) \
            else response.body.decode()
        urls = [urljoin(url, remove_fragment(new_url))
                for new_url in get_links(html)]
    except Exception as e:
        print('Exception: %s %s' % (e, url))
        raise gen.Return([])

    raise gen.Return(urls) 
Example #5
Source File: tornado-crawler-demo2.py    From Python_Master_Courses with GNU General Public License v3.0 6 votes vote down vote up
def get_request(self, url, headers=None):
        response = None
        http_client = AsyncHTTPClient()
        try:
            # response = yield http_client.fetch(url, method='GET', headers=headers,request_timeout=5)

            # POST
            body = {'a': 4, 'b': 5}
            response: HTTPResponse = yield http_client.fetch(url, method='POST', body=str(body), headers=headers, request_timeout=5)
        except Exception as e:
            print('get_request error:{0}'.format(e))

        #请求
        response.request
        #请求头
        response.headers

        response_body = None
        if response and response.code == 200:
            # print('fetched {0}'.format(url))
            response_body = response.body if isinstance(response.body, str) \
                else response.body.decode()

        return response_body 
Example #6
Source File: webspider.py    From tornado-zh with MIT License 6 votes vote down vote up
def get_links_from_url(url):
    """Download the page at `url` and parse it for links.

    Returned links have had the fragment after `#` removed, and have been made
    absolute so, e.g. the URL 'gen.html#tornado.gen.coroutine' becomes
    'http://www.tornadoweb.org/en/stable/gen.html'.
    """
    try:
        response = yield httpclient.AsyncHTTPClient().fetch(url)
        print('fetched %s' % url)

        html = response.body if isinstance(response.body, str) \
            else response.body.decode()
        urls = [urljoin(url, remove_fragment(new_url))
                for new_url in get_links(html)]
    except Exception as e:
        print('Exception: %s %s' % (e, url))
        raise gen.Return([])

    raise gen.Return(urls) 
Example #7
Source File: auth.py    From teleport with Apache License 2.0 5 votes vote down vote up
def get_auth_http_client(self):
        """Returns the `.AsyncHTTPClient` instance to be used for auth requests.

        May be overridden by subclasses to use an HTTP client other than
        the default.
        """
        return httpclient.AsyncHTTPClient() 
Example #8
Source File: auth.py    From teleport with Apache License 2.0 5 votes vote down vote up
def get_auth_http_client(self) -> httpclient.AsyncHTTPClient:
        """Returns the `.AsyncHTTPClient` instance to be used for auth requests.

        May be overridden by subclasses to use an HTTP client other than
        the default.
        """
        return httpclient.AsyncHTTPClient() 
Example #9
Source File: auth.py    From teleport with Apache License 2.0 5 votes vote down vote up
def get_auth_http_client(self):
        """Returns the `.AsyncHTTPClient` instance to be used for auth requests.

        May be overridden by subclasses to use an HTTP client other than
        the default.

        .. versionadded:: 4.3
        """
        return httpclient.AsyncHTTPClient() 
Example #10
Source File: testing.py    From teleport with Apache License 2.0 5 votes vote down vote up
def get_http_client(self):
        return AsyncHTTPClient() 
Example #11
Source File: testing.py    From teleport with Apache License 2.0 5 votes vote down vote up
def fetch(self, path, raise_error=False, **kwargs):
        """Convenience method to synchronously fetch a URL.

        The given path will be appended to the local server's host and
        port.  Any additional kwargs will be passed directly to
        `.AsyncHTTPClient.fetch` (and so could be used to pass
        ``method="POST"``, ``body="..."``, etc).

        If the path begins with http:// or https://, it will be treated as a
        full URL and will be fetched as-is.

        If ``raise_error`` is True, a `tornado.httpclient.HTTPError` will
        be raised if the response code is not 200. This is the same behavior
        as the ``raise_error`` argument to `.AsyncHTTPClient.fetch`, but
        the default is False here (it's True in `.AsyncHTTPClient`) because
        tests often need to deal with non-200 response codes.

        .. versionchanged:: 5.0
           Added support for absolute URLs.

        .. versionchanged:: 5.1

           Added the ``raise_error`` argument.

        .. deprecated:: 5.1

           This method currently turns any exception into an
           `.HTTPResponse` with status code 599. In Tornado 6.0,
           errors other than `tornado.httpclient.HTTPError` will be
           passed through, and ``raise_error=False`` will only
           suppress errors that would be raised due to non-200
           response codes.

        """
        if path.lower().startswith(('http://', 'https://')):
            url = path
        else:
            url = self.get_url(path)
        return self.io_loop.run_sync(
            lambda: self.http_client.fetch(url, raise_error=raise_error, **kwargs),
            timeout=get_async_test_timeout()) 
Example #12
Source File: gen_test.py    From teleport with Apache License 2.0 5 votes vote down vote up
def get(self):
        client = AsyncHTTPClient()
        with ignore_deprecation():
            response = yield gen.Task(client.fetch, self.get_argument('url'))
        response.rethrow()
        self.finish(b"got response: " + response.body) 
Example #13
Source File: run-dev.py    From zulip with Apache License 2.0 5 votes vote down vote up
def fetch_request(url: str, callback: Any, **kwargs: Any) -> "Generator[Callable[..., Any], Any, None]":
    # use large timeouts to handle polling requests
    req = httpclient.HTTPRequest(
        url,
        connect_timeout=240.0,
        request_timeout=240.0,
        decompress_response=False,
        **kwargs,
    )
    client = httpclient.AsyncHTTPClient()
    # wait for response
    response = yield gen.Task(client.fetch, req)
    callback(response) 
Example #14
Source File: auth.py    From teleport with Apache License 2.0 5 votes vote down vote up
def get_auth_http_client(self) -> httpclient.AsyncHTTPClient:
        """Returns the `.AsyncHTTPClient` instance to be used for auth requests.

        May be overridden by subclasses to use an HTTP client other than
        the default.

        .. versionadded:: 4.3
        """
        return httpclient.AsyncHTTPClient() 
Example #15
Source File: testing.py    From teleport with Apache License 2.0 5 votes vote down vote up
def get_http_client(self) -> AsyncHTTPClient:
        return AsyncHTTPClient(force_instance=True, defaults=dict(validate_cert=False)) 
Example #16
Source File: auth.py    From tornado-zh with MIT License 5 votes vote down vote up
def get_auth_http_client(self):
        """Returns the `.AsyncHTTPClient` instance to be used for auth requests.

        May be overridden by subclasses to use an HTTP client other than
        the default.
        """
        return httpclient.AsyncHTTPClient() 
Example #17
Source File: auth.py    From tornado-zh with MIT License 5 votes vote down vote up
def get_auth_http_client(self):
        """Returns the `.AsyncHTTPClient` instance to be used for auth requests.

        May be overridden by subclasses to use an HTTP client other than
        the default.
        """
        return httpclient.AsyncHTTPClient() 
Example #18
Source File: tornado.py    From python-consul2 with MIT License 5 votes vote down vote up
def __init__(self, *args, **kwargs):
        super(HTTPClient, self).__init__(*args, **kwargs)
        self.client = httpclient.AsyncHTTPClient() 
Example #19
Source File: auth.py    From teleport with Apache License 2.0 5 votes vote down vote up
def get_auth_http_client(self):
        """Returns the `.AsyncHTTPClient` instance to be used for auth requests.

        May be overridden by subclasses to use an HTTP client other than
        the default.
        """
        return httpclient.AsyncHTTPClient() 
Example #20
Source File: email_client.py    From Doodle with MIT License 5 votes vote down vote up
def send_email(to, subject, html):
    if isinstance(to, unicode):
        to = to.encode('utf-8')
    if isinstance(subject, unicode):
        subject = subject.encode('utf-8')
    if isinstance(html, unicode):
        html = html.encode('utf-8')

    data = {
        'from': CONFIG.EMAIL_SENDER,
        'to': to,
        'subject': subject,
        'html': html
    }
    data = urlencode(data)
    request = HTTPRequest(
        url=_MAILGUN_API_URL,
        method='POST',
        auth_username='api',
        auth_password=CONFIG.MAILGUN_API_KEY,
        body=data
    )
    client = AsyncHTTPClient()
    try:
        yield client.fetch(request)
    except HTTPError as e:
        try:
            response = e.response.body
        except AttributeError:
            response = None
        logging.exception('failed to send email:\nto: %s\nsubject: %s\nhtml: %s\nresponse: %s', to, subject, html, response) 
Example #21
Source File: geolookup.py    From viewfinder with Apache License 2.0 5 votes vote down vote up
def __init__(self, io_loop, geoproc):
    self._io_loop = io_loop
    self._geoproc = geoproc
    self._http_client = httpclient.AsyncHTTPClient()
    try:
      with open(options.options.lookupdb, 'r') as rf:
        self._lookup_db = json.load(rf)
    except:
      self._lookup_db = {} 
Example #22
Source File: twisted_test.py    From viewfinder with Apache License 2.0 5 votes vote down vote up
def tornado_fetch(self, url, runner):
        responses = []
        client = AsyncHTTPClient(self.io_loop)

        def callback(response):
            responses.append(response)
            self.stop_loop()
        client.fetch(url, callback=callback)
        runner()
        self.assertEqual(len(responses), 1)
        responses[0].rethrow()
        return responses[0] 
Example #23
Source File: gen_test.py    From viewfinder with Apache License 2.0 5 votes vote down vote up
def get(self):
        io_loop = self.request.connection.stream.io_loop
        client = AsyncHTTPClient(io_loop=io_loop)
        response = yield gen.Task(client.fetch, self.get_argument('url'))
        response.rethrow()
        self.finish(b"got response: " + response.body) 
Example #24
Source File: testing.py    From viewfinder with Apache License 2.0 5 votes vote down vote up
def fetch(self, path, **kwargs):
        """Convenience method to synchronously fetch a url.

        The given path will be appended to the local server's host and
        port.  Any additional kwargs will be passed directly to
        `.AsyncHTTPClient.fetch` (and so could be used to pass
        ``method="POST"``, ``body="..."``, etc).
        """
        self.http_client.fetch(self.get_url(path), self.stop, **kwargs)
        return self.wait() 
Example #25
Source File: testing.py    From tornado-zh with MIT License 5 votes vote down vote up
def get_http_client(self):
        return AsyncHTTPClient(io_loop=self.io_loop, force_instance=True,
                               defaults=dict(validate_cert=False)) 
Example #26
Source File: auth.py    From viewfinder with Apache License 2.0 5 votes vote down vote up
def get_auth_http_client(self):
        """Returns the `.AsyncHTTPClient` instance to be used for auth requests.

        May be overridden by subclasses to use an HTTP client other than
        the default.
        """
        return httpclient.AsyncHTTPClient() 
Example #27
Source File: auth.py    From viewfinder with Apache License 2.0 5 votes vote down vote up
def get_auth_http_client(self):
        """Returns the `.AsyncHTTPClient` instance to be used for auth requests.

        May be overridden by subclasses to use an HTTP client other than
        the default.
        """
        return httpclient.AsyncHTTPClient() 
Example #28
Source File: auth.py    From viewfinder with Apache License 2.0 5 votes vote down vote up
def get_auth_http_client(self):
        """Returns the `.AsyncHTTPClient` instance to be used for auth requests.

        May be overridden by subclasses to use an HTTP client other than
        the default.
        """
        return httpclient.AsyncHTTPClient() 
Example #29
Source File: fogbugz_export.py    From viewfinder with Apache License 2.0 5 votes vote down vote up
def fetch(**kwargs):
  http = AsyncHTTPClient()
  response = yield http.fetch(make_url(**kwargs))
  raise gen.Return(ElementTree.fromstring(response.body)) 
Example #30
Source File: twisted_test.py    From teleport with Apache License 2.0 5 votes vote down vote up
def tornado_fetch(self, url, runner):
        responses = []
        client = AsyncHTTPClient()

        def callback(response):
            responses.append(response)
            self.stop_loop()
        client.fetch(url, callback=callback)
        runner()
        self.assertEqual(len(responses), 1)
        responses[0].rethrow()
        return responses[0]