Read timeout using either urllib2 or any other http library

后端 未结 8 945
花落未央
花落未央 2020-11-29 05:36

I have code for reading an url like this:

from urllib2 import Request, urlopen
req = Request(url)
for key, val in headers.items():
    req.add_header(key, va         


        
8条回答
  •  隐瞒了意图╮
    2020-11-29 06:00

    Any asynchronous network library should allow to enforce the total timeout on any I/O operation e.g., here's gevent code example:

    #!/usr/bin/env python2
    import gevent
    import gevent.monkey # $ pip install gevent
    gevent.monkey.patch_all()
    
    import urllib2
    
    with gevent.Timeout(2): # enforce total timeout
        response = urllib2.urlopen('http://localhost:8000')
        encoding = response.headers.getparam('charset')
        print response.read().decode(encoding)
    

    And here's asyncio equivalent:

    #!/usr/bin/env python3.5
    import asyncio
    import aiohttp # $ pip install aiohttp
    
    async def fetch_text(url):
        response = await aiohttp.get(url)
        return await response.text()
    
    text = asyncio.get_event_loop().run_until_complete(
        asyncio.wait_for(fetch_text('http://localhost:8000'), timeout=2))
    print(text)
    

    The test http server is defined here.

提交回复
热议问题