Python源码示例:aiohttp.request()

示例1
def make_request(self):
        retries = 0
        while retries < self.max_retries:
            try:
                resp = yield from aiohttp.request(self.method, self.url,
                                                  **self.request_kwargs)
                return (yield from self._handle_response(resp))
            except Exception as exc:
                retries += 1
                error = dict(
                    url=self.url,
                    params=self.request_kwargs.get('params'),
                    message='Request failed, retrying.',
                    retries_left=self.max_retries-retries,
                )
                if self.debug:
                    error['callback'] = repr(self.callback)
                    error['exception'] = repr(exc)
                    error['traceback'] = traceback.format_exc()
                    sys.stderr.write('{}\n'.format(json.dumps(error)))
            yield from asyncio.sleep(1)
        else:
            error['message'] = 'Maximum retries exceeded for url, giving up.'
            sys.stderr.write('{}\n'.format(json.dumps(error)))
            return 
示例2
def _handle_request(self, method, request, requested_url):
        """Handle proxy requests."""
        requested_url = requested_url or "/"
        headers = request.headers.copy()
        headers["Host"] = request.host
        headers["X-Real-Ip"] = request.remote
        headers["X-Forwarded-For"] = request.remote
        headers["X-Forwarded-Proto"] = request.scheme
        post_data = await request.read()
        async with aiohttp.request(
            method,
            self.proxy_url + requested_url,
            params=request.query,
            data=post_data,
            headers=headers,
        ) as resp:
            content = await resp.read()
            headers = resp.headers.copy()
            return aiohttp.web.Response(
                body=content, status=resp.status, headers=headers
            ) 
示例3
def _send_to_external_chat(self, bot, event, config):
        if event.from_bot:
            # don't send my own messages
            return

        event_timestamp = event.timestamp

        conversation_id = event.conv_id
        conversation_text = event.text

        user_full_name = event.user.full_name
        user_id = event.user_id

        url = config["HUBOT_URL"] + conversation_id
        payload = {"from" : str(user_id.chat_id), "message" : conversation_text}
        headers = {'content-type': 'application/json'}

        connector = aiohttp.TCPConnector(verify_ssl=False)
        asyncio.ensure_future(
            aiohttp.request('post', url, data = json.dumps(payload), headers = headers, connector=connector)
        ).add_done_callback(lambda future: future.result()) 
示例4
def _search_comic(bot, event, terms):
    request = yield from aiohttp.request('get', "https://relevantxkcd.appspot.com/process?%s" % urllib.parse.urlencode({
        "action": "xkcd",
        "query": " ".join(terms),
    }))
    raw = yield from request.read()
    values = [row.strip().split(" ")[0] for row in raw.decode().strip().split("\n")]
    
    weight = float(values.pop(0))
    values.pop(0) # selection - ignore?
    comics = [int(i) for i in values]
    num = comics.pop(0)
    
    msg = 'Most relevant xkcd: #%d (relevance: %.2f%%)\nOther relevant comics: %s' % (num, weight*100, ", ".join("#%d" % i for i in comics))
    
    # get info and upload image if necessary
    yield from _get_comic(bot, num)
    
    yield from bot.coro_send_message(event.conv.id_, msg)
    yield from _print_comic(bot, event, num) 
示例5
def get_flag(base_url, cc):
    url = '{}/{cc}/{cc}.gif'.format(base_url, cc=cc.lower())
    resp = yield from aiohttp.request('GET', url)
    with contextlib.closing(resp):
        if resp.status == 200:
            image = yield from resp.read()
            return image
        elif resp.status == 404:
            raise web.HTTPNotFound()
        else:
            raise aiohttp.HttpProcessingError(
                code=resp.status, message=resp.reason,
                headers=resp.headers)


# BEGIN FLAGS2_ASYNCIO_EXECUTOR 
示例6
def http_get(url):
    res = yield from aiohttp.request('GET', url)
    if res.status == 200:
        ctype = res.headers.get('Content-type', '').lower()
        if 'json' in ctype or url.endswith('json'):
            data = yield from res.json()  # <1>
        else:
            data = yield from res.read()  # <2>
        return data

    elif res.status == 404:
        raise web.HTTPNotFound()
    else:
        raise aiohttp.errors.HttpProcessingError(
            code=res.status, message=res.reason,
            headers=res.headers) 
示例7
def __process(self,request):
        for i in request['d']['results']:
            url = i['Url'].encode('ascii','ignore').decode()
            self.uniq_urls.add(url)
            up = urlparse(url)
            x = up.netloc
            if not x.count(':'):
                if up.scheme == "https":
                    x+=":443"
                else:
                    x+=":80"

            self.uniq_hosts.add(x)
        if len(request['d']['results']) < self.parameters['$top']:
            return False
        else:
            return True 
示例8
def search(self,query,page):
        params = {
            "Query":query,
            "$skip": self.parameters["$top"] * page
        }
        params.update(self.parameters)
        try:
            r = yield from aiohttp.request(
                    'get',
                    self.url,
                    params=params,
                    headers=self.headers
                    )
            results = yield from r.json()
            yield from self.__process(results)
        except aiohttp.ClientError as client_error:
            print("Error: {emsg}".format(emsg=client_error)) 
示例9
def get_balance(session, account):
        """
        Get balance.

        Args:
            session (TastyAPISession): An active and logged-in session object against which to query.
            account (TradingAccount): The account_id to get balance on.
        Returns:
            dict: account attributes
        """
        url = '{}/accounts/{}/balances'.format(
            session.API_url,
            account.account_number
        )

        async with aiohttp.request('GET', url, headers=session.get_request_headers()) as response:
            if response.status != 200:
                raise Exception('Could not get trading account balance info from Tastyworks...')
            data = (await response.json())['data']
        return data 
示例10
def get_positions(session, account):
        """
        Get Open Positions.

        Args:
            session (TastyAPISession): An active and logged-in session object against which to query.
            account (TradingAccount): The account_id to get positions on.
        Returns:
            dict: account attributes
        """
        url = '{}/accounts/{}/positions'.format(
            session.API_url,
            account.account_number
        )

        async with aiohttp.request('GET', url, headers=session.get_request_headers()) as response:
            if response.status != 200:
                raise Exception('Could not get open positions info from Tastyworks...')
            data = (await response.json())['data']['items']
        return data 
示例11
def get_history(session, account):
        """
        Get live Orders.

        Args:
            session (TastyAPISession): An active and logged-in session object against which to query.
            account (TradingAccount): The account_id to get history on.
        Returns:
            dict: account attributes
        """
        url = '{}/accounts/{}/transactions'.format(
            session.API_url,
            account.account_number
        )

        async with aiohttp.request('GET', url, headers=session.get_request_headers()) as response:
            if response.status != 200:
                raise Exception('Could not get history info from Tastyworks...')
            data = (await response.json())['data']
        return data 
示例12
def load_watchlists(self):
        request_url = '{}/public_watchlists?include_synthetic=true'.format(
            BASE_URL
        )

        async with aiohttp.request('GET', request_url) as resp:
            if resp.status != 200:
                raise Exception('Could not get public asset watchlists')
            data = await resp.json()

        data = data['public_watchlists']
        for entry in data:
            list_data = entry['entries']
            wlist = Watchlist.from_list(list_data)
            wlist.name = entry['name']
            wlist.slug = entry['slug']
            self.watchlists[wlist.slug] = wlist

        return self 
示例13
def qks_rex(bot, ev):
    match = ev.match
    msg = f'骑空士爪巴远点\n{qksimg}'
    res = 'http://'+match.group(0)
    async with aiohttp.TCPConnector(verify_ssl=False) as connector:
        async with aiohttp.request(
            'GET',
            url=res,
            allow_redirects=False,
            connector=connector,
        ) as resp:
            h = resp.headers
            s = resp.status
    if s == 301 or s == 302:
        if 'granbluefantasy.jp' in h['Location']:
            await bot.send(ev, msg, at_sender=True)
            await util.silence(ev, 60) 
示例14
def test_request_hook(app: Flask, aio: AioHTTP):
    """Test for Flask request hook"""
    @app.before_request
    def before_request():
        request.foo = []
        request.foo.append('a')

    @app.after_request
    def after_request(response):
        request.foo.append('c')
        return response

    @app.teardown_request
    def teardown_request(exc):
        request.foo.append('d')

    @app.route('/hook')
    @async
    def hook():
        request.foo.append('b')

        return ''.join(request.foo)

    with Server(app, aio) as server:
        assert 'ab' == server.get('/hook') 
示例15
def get_flag(base_url, cc):
    url = '{}/{cc}/{cc}.gif'.format(base_url, cc=cc.lower())
    resp = yield from aiohttp.request('GET', url)
    with contextlib.closing(resp):
        if resp.status == 200:
            image = yield from resp.read()
            return image
        elif resp.status == 404:
            raise web.HTTPNotFound()
        else:
            raise aiohttp.HttpProcessingError(
                code=resp.status, message=resp.reason,
                headers=resp.headers)


# BEGIN FLAGS2_ASYNCIO_EXECUTOR 
示例16
def http_get(url):
    res = yield from aiohttp.request('GET', url)
    if res.status == 200:
        ctype = res.headers.get('Content-type', '').lower()
        if 'json' in ctype or url.endswith('json'):
            data = yield from res.json()  # <1>
        else:
            data = yield from res.read()  # <2>
        return data

    elif res.status == 404:
        raise web.HTTPNotFound()
    else:
        raise aiohttp.errors.HttpProcessingError(
            code=res.status, message=res.reason,
            headers=res.headers) 
示例17
def update_nicknames(self):
        nickfile = os.path.join(self.setting["dirname"], "nickname3.csv")
        try:
            async with aiohttp.request('GET', self.Nicknames_csv) as resp:
                if resp.status != 200:
                    raise ServerError(
                        "bad server response. code: "+str(resp.status))
                restxt = await resp.text()
                with open(nickfile, "w", encoding="utf-8-sig") as f:
                    f.write(restxt)
        except aiohttp.ClientError as e:
            raise RuntimeError('错误'+str(e))
        with open(nickfile, encoding="utf-8-sig") as f:
            csv = f.read()
            for line in csv.split("\n")[1:]:
                row = line.split(",")
                for col in row:
                    self.nickname_dict[col] = (row[0], row[1]) 
示例18
def test_aiohttp(self):
        try:
            import aiohttp
        except ImportError:
            raise SkipTest("Requires aiohttp")
        from aiohttp import web

        @asyncio.coroutine
        def echo(request):
            print(request.path)
            return web.Response(body=str(request).encode('utf8'))
        
        @asyncio.coroutine
        def server(loop):
            app = web.Application(loop=loop)
            app.router.add_route('GET', '/', echo)

            srv = yield from loop.create_server(app.make_handler(),
                                                '127.0.0.1', 8080)
            print("Server started at http://127.0.0.1:8080")
            return srv

        @asyncio.coroutine
        def client():
            push, pull = self.create_bound_pair(zmq.PUSH, zmq.PULL)
            
            res = yield from aiohttp.request('GET', 'http://127.0.0.1:8080/')
            text = yield from res.text()
            yield from push.send(text.encode('utf8'))
            rcvd = yield from pull.recv()
            self.assertEqual(rcvd.decode('utf8'), text)

        loop = asyncio.get_event_loop()
        loop.run_until_complete(server(loop))
        print("servered")
        loop.run_until_complete(client()) 
示例19
def test_httpstan_port_conflict():
    s = socket.socket()
    try:
        s.bind(("", 8080))
        async with stan.common.httpstan_server() as (host, port):
            async with aiohttp.request("GET", f"http://{host}:{port}/v1/health") as resp:
                assert resp.status == 200
                assert port != 8080
    finally:
        s.close() 
示例20
def test_httpstan_health():
    async with stan.common.httpstan_server() as (host, port):
        async with aiohttp.request("GET", f"http://{host}:{port}/v1/health") as resp:
            assert resp.status == 200 
示例21
def handle(request):
    coroutines = [aiohttp.request('get', url) for url in REQEUST_URLS]

    results = await asyncio.gather(*coroutines, return_exceptions=True)

    response_data = {
        url: not isinstance(result, Exception) and result.status == 200
        for url, result in zip(REQEUST_URLS, results)
    }

    body = json.dumps(response_data).encode('utf-8')
    return web.Response(body=body, content_type="application/json") 
示例22
def report_to_slack(output, webhook):
    payload = {
        "text": f"lbrynet {__version__} ({system_info.get_platform()['platform']}) time to first byte:\n{output}"
    }
    async with aiohttp.request('post', webhook, data=json.dumps(payload)):
        pass 
示例23
def _get(self, url):
        response = yield from aiohttp.request('GET', self._base_url + url, auth=self._auth, loop=self._loop)
        return (yield from response.read()) 
示例24
def get(self, request, requested_url):
        """Handle GET proxy requests."""
        return await self._handle_request("GET", request, requested_url) 
示例25
def post(self, request, requested_url):
        """Handle POST proxy requests."""
        return await self._handle_request("POST", request, requested_url) 
示例26
def sendSource(bot, event, name, imgLink):
    logger.info("Getting {}".format(imgLink))
    r = yield from aiohttp.request("get", imgLink)
    raw = yield from r.read()
    contentType = r.headers['Content-Type']
    logger.info("\tContent-type: {}".format(contentType))
    ext = contentType.split('/')[1]
    image_data = io.BytesIO(raw)
    filename = "{}_{}.{}".format(name, int(time.time()), ext)
    try:
        image_id = yield from bot._client.upload_image(image_data, filename=filename)
    except:
        yield from bot.coro_send_message(event.conv, _("I'm sorry, I couldn't upload a {} image".format(ext)))
    else:
        yield from bot.coro_send_message(event.conv.id_, None, image_id=image_id) 
示例27
def telegram_api_request(self, configuration, method, data):
        connector = aiohttp.TCPConnector(verify_ssl=True)
        headers = {'content-type': 'application/x-www-form-urlencoded'}

        BOT_API_KEY = configuration["bot_api_key"]

        url = "https://api.telegram.org/bot{}/{}".format(BOT_API_KEY, method)

        response = yield from aiohttp.request('post', url, data=data, headers=headers, connector=connector)
        results = yield from response.text()

        return results 
示例28
def image_upload_single(image_uri, bot):
    logger.info("getting {}".format(image_uri))
    filename = os.path.basename(image_uri)
    r = yield from aiohttp.request('get', image_uri)
    raw = yield from r.read()
    image_data = io.BytesIO(raw)
    image_id = yield from bot._client.upload_image(image_data, filename=filename)
    return image_id 
示例29
def _scan_for_triggers(bot, event, command):
    limit = 3
    count = 0
    lctext = event.text.lower()
    image_links = []
    for trigger in _lookup:
        pattern = '\\b' + trigger + '\.(jpg|png|gif|bmp)\\b'
        if re.search(pattern, lctext):
            image_links.append(_get_a_link(trigger))
            count = count + 1
            if count >= limit:
                break

    image_links = list(set(image_links)) # make unique

    if len(image_links) > 0:
        for image_link in image_links:
            try:
                image_id = yield from bot.call_shared('image_validate_and_upload_single', image_link)
            except KeyError:
                logger.warning('image plugin not loaded - using legacy code')
                if re.match(r'^https?://gfycat.com', image_link):
                    image_link = re.sub(r'^https?://gfycat.com/', 'https://thumbs.gfycat.com/', image_link) + '-size_restricted.gif'
                elif "imgur.com" in image_link:
                    image_link = image_link.replace(".gifv",".gif")
                    image_link = image_link.replace(".webm",".gif")
                filename = os.path.basename(image_link)
                r = yield from aiohttp.request('get', image_link)
                raw = yield from r.read()
                image_data = io.BytesIO(raw)
                logger.debug("uploading: {}".format(filename))
                image_id = yield from bot._client.upload_image(image_data, filename=filename)
            yield from bot.coro_send_message(event.conv.id_, "", image_id=image_id) 
示例30
def image_upload_single(image_uri, bot):
    logger.info("getting {}".format(image_uri))
    filename = os.path.basename(image_uri)
    r = yield from aiohttp.request('get', image_uri)
    raw = yield from r.read()
    image_data = io.BytesIO(raw)
    image_id = yield from bot._client.upload_image(image_data, filename=filename)
    return image_id