Answers for "timeout exception requests python"

1

python requests.get timeout

# Here is how to set a time out for requests.get in python
# its simple!
import requests

link = 'https://google.com' 
request_from_link = requests.get(link, timeout=10) 
# this causes the code to call a timeout if the connection or delays in 
# between the reads take more than 10 seconds
print(request_from_link)
Posted by: Guest on May-07-2021
0

python timeout exception

--------------------------------------------------------------
Timeout function
--------------------------------------------------------------
import asyncio
from async_timeout import timeout

class Student:
  	def __init__(self):
      	self.queue = asyncio.Queue()
      	pass
  	
	async def function(self):
      	try:
    		async with timeout(300): # 5 minutes...
              	#source = await self.queue.get()
            	
                #do what u need to do
                pass
                
        except asyncio.TimeoutError as e:
        	print(e)
            
            
--------------------------------------------------------------
Requests
--------------------------------------------------------------
import requests as r

class Student:
	def __init__(self):
      	pass
      
  	def function(url:string):
		try:
    		data = r.get(url, timeout=10.0)
		except requests.Timeout as err:
    		logger.error({"message": err.message})
		except Exception as err:
          	print(err)
        return data # or what ever u need to return
Posted by: Guest on February-19-2021

Python Answers by Framework

Browse Popular Code Answers by Language