pythonnetwork-programmingpython-trio

Python Asks and Trio modules don't work together


I'm new to both Trio and Asks so apologies if this is an obvious mistake.

From the docs of Trio and Asks, I thought the following code would work and allow me to asynchronously make a request

import asks
import trio

async def test():
    a = await asks.get('https://example.com')

trio.run(test)

But instead it gives me many errors and concludes with AttributeError: module 'trio' has no attribute 'MultiError'

From the documentation I assumed that both libraries are compatible, but perhaps this is incorrect?


Solution

  • trio.MultiError has been removed in newer versions.

    import asks
    import trio
    import asyncio
    
    async def test():
        try:
            response = await asks.get('https://google.com')
            print("Response:", response)
            print("Response status code:", response.status_code)
            print("Response text:", response.text)
        except trio.Cancelled as e:
            print(f"An error occurred: {e}")
    
    trio.run(test)
    

    reference

    UPDATE

    uninstall the previous version of anyio using this command pip uninstall anyio

    then install the compatible version with trio pip install anyio[trio]

    then run the script again, you will get an output like this:

    Response: <Response 429 Too Many Requests>
    Response status code: 429
    Response text: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head><meta http-equiv="content-type" content="text/html; charset=utf-8"><meta name="viewport" content="initial-scale=1"><title>https://google.com/</title></head>
    <body style="font-family: arial, sans-serif; background-color: #fff; color: #000; padding:20px; font-size:18px; overscroll-behavior:contain;" onload="e=document.getElementById('captcha');if(e){e.focus();} if(solveSimpleChallenge) {solveSimpleChallenge(,);}">
    <div style="max-width:400px;">
    <hr noshade size="1" style="color:#ccc; background-color:#ccc;"><br>
    <form id="captcha-form" action="index" method="post">
    <noscript>
    <div style="font-size:13px;">
      In order to continue, please enable javascript on your web browser.
    </div>
    </noscript>
    <script src="https://www.google.com/recaptcha/api.js" async defer></script>
    <script>var submitCallback = function(response) {document.getElementById('captcha-form').submit();};</script>
    <div id="recaptcha" class="g-recaptcha" data-sitekey="6LfwuyUTAAAAAOAmoS0fdqijC2PbbdH4kjq62Y1b" data-callback="submitCallback" data-s="_tgCT2yHQMDHpfdZ6VLDfvvGJTkL_TiVH-Nmf9UR0j5cBe60qBkNCjt2WVZsXz1wQQ90wfqcJ6VHBt8LCiCCCfpWawN0o2W0U1QUu_L_ho1fpCA_fmLbeTby5DMCYObVxdW2dzi_KlzKQ0lzhbo4_EVYh6IgAJ3NhlK67C4Q2gn_j47MWYHNZSF0JuPK4d7SctCVLBgr57uePxnZLHUMO2MPhQ5mKy5CZl3Rl8dHQUWbTQxXg0PKyo7-WTrX9avU22nvB0MfJWWRxzKfUL_POwXLrNhjpIA"></div>
    
    <input type='hidden' name='q' value='EgSKxy_DGIrMla4GIjAYHTL1QYtmVFwVUix8upuZjQ3AuJv4FIXnA0sxrqy8hI0cJ2Hyiy4dK12v50Gm8zkyAXJKGVNPUlJZX0FCVVNJVkVfTkVUX01FU1NBR0VaAUM'><input type="hidden" name="continue" value="https://google.com/">
    </form>
    <hr noshade size="1" style="color:#ccc; background-color:#ccc;">
    
    <div style="font-size:13px;">
    <b>About this page</b><br><br>
    This network is blocked due to unaddressed abuse complaints about malicious behavior. This page checks to see if it's really a human sending the requests and not a robot coming from this network. <br><br>
    <div id="infoDiv" style="display:none; background-color:#eee; padding:10px; margin:0 0 15px 0; line-height:1.4em;">
    This page appears when Google automatically detects requests coming from your computer network which appear to be in violation of the <a href="//www.google.com/policies/terms/">Terms of Service</a>. The block will expire shortly after those requests stop.  In the meantime, solving the above CAPTCHA will let you continue to use our services.<br><br>This traffic may have been sent by malicious software, a browser plug-in, or a script that sends automated requests.  If you share your network connection, ask your administrator for help &mdash; a different computer using the same IP address may be responsible.  <a href="//support.google.com/websearch/answer/86640">Learn more</a><br><br>Sometimes you may be asked to solve the CAPTCHA if you are using advanced terms that robots are known to use, or sending requests very quickly.
    </div>
    
    IP address: *.*.*.*<br>Time: 2024-02-08T23:38:51Z<br>URL: https://google.com/<br>
    </div>
    </div>
    </body>
    </html>