Python aiohttp.request() Examples

The following are 30 code examples of aiohttp.request(). 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 aiohttp , or try the search function .
Example #1
Source File: watchlists.py    From tastyworks_api with Apache License 2.0 6 votes vote down vote up
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 
Example #2
Source File: jjc_consult.py    From yobot with GNU General Public License v3.0 6 votes vote down vote up
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]) 
Example #3
Source File: adminpanel.py    From homeassistant-zigate with MIT License 6 votes vote down vote up
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
            ) 
Example #4
Source File: hubot.py    From hangoutsbot with GNU Affero General Public License v3.0 6 votes vote down vote up
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()) 
Example #5
Source File: flags3_asyncio.py    From example-code with MIT License 6 votes vote down vote up
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) 
Example #6
Source File: flags2_asyncio_executor.py    From example-code with MIT License 6 votes vote down vote up
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 
Example #7
Source File: xkcd.py    From hangoutsbot with GNU Affero General Public License v3.0 6 votes vote down vote up
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) 
Example #8
Source File: test_aiowebsocket.py    From Flask-aiohttp with MIT License 6 votes vote down vote up
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') 
Example #9
Source File: Bing.py    From webbies with MIT License 6 votes vote down vote up
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 
Example #10
Source File: Bing.py    From webbies with MIT License 6 votes vote down vote up
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)) 
Example #11
Source File: antiqks.py    From HoshinoBot with GNU General Public License v3.0 6 votes vote down vote up
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) 
Example #12
Source File: trading_account.py    From tastyworks_api with Apache License 2.0 6 votes vote down vote up
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 
Example #13
Source File: flags3_asyncio.py    From notebooks with MIT License 6 votes vote down vote up
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) 
Example #14
Source File: trading_account.py    From tastyworks_api with Apache License 2.0 6 votes vote down vote up
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 
Example #15
Source File: trading_account.py    From tastyworks_api with Apache License 2.0 6 votes vote down vote up
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 
Example #16
Source File: flags2_asyncio_executor.py    From notebooks with MIT License 6 votes vote down vote up
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 
Example #17
Source File: test_aiowebsocket.py    From Flask-aiohttp with MIT License 5 votes vote down vote up
def request(self, method, path, params=None):
        r = urllib.request.Request(self.url(path, **params),
                                   method=method.upper())
        with urllib.request.urlopen(r) as response:
            return response.readall().decode('utf-8') 
Example #18
Source File: jjc_consult.py    From yobot with GNU General Public License v3.0 5 votes vote down vote up
def search_pcrdfans_async(self, def_lst: list, region: int) -> List[Solution]:
        authorization = self.setting['jjc_auth_key']
        if not authorization:
            raise RuntimeError('未授权,无法查询')
        id_list = [int(char_id) * 100 + 1 for (char_id, _) in def_lst]
        headers = {
            'user-agent': 'yobot',
            'authorization': authorization,
        }
        payload = {"_sign": "a", "def": id_list, "nonce": "a",
                   "page": 1, "sort": 1, "ts": int(time.time()), "region": region}
        try:
            async with aiohttp.request(
                'POST',
                'https://api.pcrdfans.com/x/v1/search',
                headers=headers,
                json=payload,
            ) as resp:
                restxt = await resp.text()
        except aiohttp.ClientError as e:
            raise RuntimeError('错误'+str(e))
        try:
            search = json.loads(restxt)
        except json.JSONDecodeError:
            raise RuntimeError('服务器错误,请稍后再试')
        if search['code']:
            raise RuntimeError(f'查询请求被拒绝,返回值{search["code"]}')
        result = search['data']['result']
        return list(map(self._parse_pcrdfans_team, result)) 
Example #19
Source File: scraper_thread.py    From mp with Apache License 2.0 5 votes vote down vote up
def fetch_async(a):
    async with aiohttp.request('GET', URL.format(a)) as r:
        data = await r.json()
    return data['args']['a'] 
Example #20
Source File: scraper_thread.py    From mp with Apache License 2.0 5 votes vote down vote up
def fetch_async(a):
    async with aiohttp.request('GET', URL.format(a)) as r:
        data = await r.json()
    return data['args']['a'] 
Example #21
Source File: semaphore.py    From mp with Apache License 2.0 5 votes vote down vote up
def fetch_async(a):
    async with aiohttp.request('GET', URL.format(a)) as r:
        data = await r.json()
    return data['args']['a'] 
Example #22
Source File: test_lib_async.py    From fhir-py with MIT License 5 votes vote down vote up
def test_create_bundle(self):
        bundle = {
            'resourceType':
                'bundle',
            'type':
                'transaction',
            'entry':
                [
                    {
                        'request': {
                            'method': 'POST',
                            'url': '/Patient'
                        },
                        'resource':
                            {
                                'id': 'bundle_patient_1',
                                'identifier': self.identifier,
                            }
                    },
                    {
                        'request': {
                            'method': 'POST',
                            'url': '/Patient'
                        },
                        'resource':
                            {
                                'id': 'bundle_patient_2',
                                'identifier': self.identifier,
                            }
                    },
                ],
        }
        await self.create_resource('Bundle', **bundle)
        await self.client.resources('Patient').search(
            _id='bundle_patient_1'
        ).get()
        await self.client.resources('Patient').search(
            _id='bundle_patient_2'
        ).get() 
Example #23
Source File: scraper_thread.py    From mp with Apache License 2.0 5 votes vote down vote up
def fetch_async(a):
    async with aiohttp.request('GET', URL.format(a)) as r:
        data = await r.json()
    return a, data['args']['a'] 
Example #24
Source File: scraper_process.py    From mp with Apache License 2.0 5 votes vote down vote up
def fetch_async(a):
    async with aiohttp.request('GET', URL.format(a)) as r:
        data = await r.json()
    return data['args']['a'] 
Example #25
Source File: scraper_process.py    From mp with Apache License 2.0 5 votes vote down vote up
def fetch_async(a):
    async with aiohttp.request('GET', URL.format(a)) as r:
        data = await r.json()
    return a, data['args']['a'] 
Example #26
Source File: observer_async.py    From Software-Architecture-with-Python with MIT License 5 votes vote down vote up
def callback(self, channel, data):
        """ Callback method """

        # The data is a URL
        url = data
        # We return the response immediately
        print('Fetching URL',url,'...')
        future = aiohttp.request('GET', url)
        self.futures.append(future)
            
        return future 
Example #27
Source File: updater.py    From yobot with GNU General Public License v3.0 5 votes vote down vote up
def linux_update_async(self, force: bool = False, test_ver: int = 0):
        if self.evn == "linux-exe":
            return "Linux 便携版暂时无法自动更新"
        test_version = ["stable", "beta", "alpha"][test_ver]
        pullcheck = self.check_commit(force)
        if pullcheck is not None:
            return pullcheck
        for url in self.ver["check_url"]:
            try:
                async with aiohttp.request('GET', url=url) as response:
                    if response.status == 200:
                        res = await response.text()
                        server_available = True
                        break
            except:
                continue
        if not server_available:
            return "无法连接服务器"
        verinfo = json.loads(res)
        verinfo = verinfo[test_version]
        if not (force or verinfo["version"] > self.ver["ver_id"]):
            return "已经是最新版本"
        git_dir = os.path.dirname(os.path.dirname(self.working_path))
        os.system(f'cd "{git_dir}" ; git pull')
        open('.YOBOT_RESTART', 'w').close()
        sys.exit(10) 
Example #28
Source File: app.py    From Flask-aiohttp with MIT License 5 votes vote down vote up
def api():
    response = yield from aiohttp.request(
        'GET', 'https://graph.facebook.com/zuck')
    data = yield from response.read()
    return data 
Example #29
Source File: updater.py    From yobot with GNU General Public License v3.0 5 votes vote down vote up
def windows_update_git_async(self, force: bool = False, test_ver: int = 0):
        test_version = ["stable", "beta", "alpha"][test_ver]
        pullcheck = self.check_commit(force)
        if pullcheck is not None:
            return pullcheck
        server_available = False
        for url in self.ver["check_url"]:
            try:
                async with aiohttp.request('GET', url=url) as response:
                    if response.status == 200:
                        res = await response.text()
                        server_available = True
                        break
            except:
                continue
        if not server_available:
            return "无法连接服务器"
        verinfo = json.loads(res)
        verinfo = verinfo[test_version]
        if not (force or verinfo["version"] > self.ver["ver_id"]):
            return "已经是最新版本"
        git_dir = os.path.dirname(os.path.dirname(self.working_path))
        cmd = '''
        cd '{}'
        taskkill /pid {} /f
        git pull
        ping 127.0.0.1 -n 3 >nul
        powershell Start-Process -FilePath "python.exe" -ArgumentList '{}'
        '''.format(self.path, os.getpid(), os.path.join(self.working_path, "main.py"))
        with open(os.path.join(git_dir, "update.bat"), "w") as f:
            f.write(cmd)
        os.system("powershell Start-Process -FilePath '{}'".format(
            os.path.join(git_dir, "update.bat")))
        sys.exit() 
Example #30
Source File: jjc_consult.py    From yobot with GNU General Public License v3.0 5 votes vote down vote up
def search_nomae_async(self, def_lst: list, region: int) -> List[Solution]:
        if region == 2 or region == 3:
            raise RuntimeError('当前搜索模式下无法执行此类查询')
        headers = {
            'User-Agent': ('Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
                           'AppleWebKit/537.36 (KHTML, like Gecko) '
                           'Chrome/78.0.3904.87 Safari/537.36'),
            'X-From': 'https://nomae.net/arenadb/',
            'Authority': 'nomae.net',
        }
        req = aiohttp.FormData()
        req.add_field('type', 'search')
        req.add_field('userid', 0)
        req.add_field('public', 1)
        for _, jpname in def_lst:
            req.add_field('def[]', jpname)
        req.add_field('page', 0)
        req.add_field('sort', 0)
        retry = 2
        while retry >= 0:
            retry -= 1
            try:
                async with aiohttp.request(
                    'POST',
                    'https://nomae.net/princess_connect/public/_arenadb/receive.php',
                    headers=headers,
                        data=req) as resp:
                    restxt = await resp.text()
            except aiohttp.ClientError as e:
                raise RuntimeError('错误'+str(e))
            try:
                receive = json.loads(restxt)
            except json.JSONDecodeError:
                continue
            return list(map(self._parse_nomae_team, receive))
        raise RuntimeError('服务器错误,请稍后再试')