Here are the examples of the python api aiohttp.Timeout taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
19 Examples
3
Example 1
async def _get(self, url, data=None, headers=None, method='GET'):
page = ''
try:
with (await self._sem_provider),\
aiohttp.Timeout(self._timeout, loop=self._loop):
async with self._session.request(method, url, data=data,
headers=headers) as resp:
if resp.status == 200:
page = await resp.text()
else:
error_page = await resp.text()
log.error('url: %s\nheaders: %s\ncookies: %s\npage:\n%s' % (
url, resp.headers, resp.cookies, error_page))
raise BadStatusError('Status: %s' % resp.status)
except (UnicodeDecodeError, BadStatusError, asyncio.TimeoutError,
aiohttp.ClientOSError, aiohttp.ClientResponseError,
aiohttp.ServerDisconnectedError) as e:
log.error('%s is failed. Error: %r;' % (url, e))
return page
3
Example 2
async def get_real_ext_ip(self):
"""Return real external IP address."""
try:
with aiohttp.Timeout(self._timeout, loop=self._loop),\
aiohttp.ClientSession(loop=self._loop) as session:
async with session.get('http://httpbin.org/ip') as resp:
data = await resp.json()
except asyncio.TimeoutError as e:
raise RuntimeError('Could not get a external IP. Error: %s' % e)
else:
ip = data['origin'].split(', ')[0]
log.debug('Real external IP: %s' % ip)
return ip
3
Example 3
@asyncio.coroutine
def get_dorks(self):
dorks = None
try:
with aiohttp.Timeout(10.0):
with aiohttp.ClientSession() as session:
r = yield from session.get(
'http://{0}:8090/dorks'.format(self.run_args.tanner)
)
try:
dorks = yield from r.json()
except json.decoder.JSONDecodeError as e:
print(e)
finally:
r.release()
except:
print('Dorks timeout')
return dorks['response']['dorks'] if dorks else []
3
Example 4
@asyncio.coroutine
def submit_slurp(self, data):
try:
with aiohttp.Timeout(10.0):
with aiohttp.ClientSession(connector=aiohttp.TCPConnector(verify_ssl=False)) as session:
r = yield from session.post(
'https://{0}:8080/api?auth={1}&chan=snare_test&msg={2}'.format(
self.run_args.slurp_host, self.run_args.slurp_auth, data
), data=json.dumps(data)
)
assert r.status == 200
r.close()
except Exception as e:
print(e)
3
Example 5
@asyncio.coroutine
def submit_data(self, data):
event_result = None
try:
with aiohttp.Timeout(10.0):
with aiohttp.ClientSession() as session:
r = yield from session.post(
'http://{0}:8090/event'.format(self.run_args.tanner), data=json.dumps(data)
)
try:
event_result = yield from r.json()
except json.decoder.JSONDecodeError as e:
print(e, data)
finally:
r.release()
except Exception as e:
raise e
return event_result
3
Example 6
async def request(req, **user_kw):
fn, args, kwargs, timeout = _transform(req, **user_kw)
if timeout is None:
async with fn(*args, **kwargs) as r:
return await _parse(r)
else:
with aiohttp.Timeout(timeout):
async with fn(*args, **kwargs) as r:
return await _parse(r)
0
Example 7
async def fetch(self, session, room, id_blacklist):
url = self._stream_api.format(room=room)
while True:
# print("polling on url %s" % url)
try:
with aiohttp.Timeout(300):
async with session.get(url, headers=self.headers) as resp:
while True:
line = await resp.content.readline()
line = bytes.decode(line, 'utf-8').strip()
if not line:
continue
msg = self.parse_jmsg(room, json.loads(line))
if msg.sender in id_blacklist:
continue
self.send_to_bus(msg)
except asyncio.TimeoutError:
pass
except:
raise
0
Example 8
async def call(self, endpoint, method='POST', raw=False, *args, **kwargs):
if 'headers' not in kwargs:
kwargs['headers'] = await self.get_headers()
uri = self.uri(endpoint)
logger.debug('Fetching: %s', uri)
logger.debug('Headers: %s', kwargs['headers'])
logger.debug('Cookies: %s', self.session.cookies)
with aiohttp.Timeout(self.request_timeout):
async with self.session.request(
method, uri, *args, **kwargs) as response:
body = await response.read()
if not response.status == 200:
try:
json = await response.json()
except Exception: # TODO: narrow exception
json = None
ex = BadRequest if response.status == 400 else HTTPError
raise ex(response.status, body, kwargs.get('data'), json)
if raw:
return body
json = await response.json()
if json.get('error'):
raise ResponseError(response.status, body, kwargs.get('data'), json)
return json
0
Example 9
async def _collect(self):
with aiohttp.Timeout(10):
async with aiohttp.ClientSession() as session:
async with session.get(self.url) as response:
await self._parse(response)
0
Example 10
async def get_last_version(self):
if self.preferences['enable_proxy'] is True:
connector = ProxyConnector("http://{0}:{1}".format(
self.preferences['proxy_address'],
self.preferences['proxy_port']))
else:
connector = None
try:
with aiohttp.Timeout(15):
response = await aiohttp.get("https://api.github.com/repos/duniter/sakia/releases", connector=connector)
if response.status == 200:
releases = await response.json()
latest = None
for r in releases:
if not latest:
latest = r
else:
latest_date = datetime.datetime.strptime(latest['published_at'], "%Y-%m-%dT%H:%M:%SZ")
date = datetime.datetime.strptime(r['published_at'], "%Y-%m-%dT%H:%M:%SZ")
if latest_date < date:
latest = r
latest_version = latest["tag_name"]
version = (__version__ == latest_version,
latest_version,
latest["html_url"])
logging.debug("Found version : {0}".format(latest_version))
logging.debug("Current version : {0}".format(__version__))
self.available_version = version
self.version_requested.emit()
except (aiohttp.errors.ClientError, aiohttp.errors.TimeoutError) as e:
logging.debug("Could not connect to github : {0}".format(str(e)))
except Exception as e:
pass
0
Example 11
@asyncio.coroutine
def perform_request(self, method, url, params=None, body=None, timeout=None, ignore=()):
url_path = url
if params:
url_path = '%s?%s' % (url, urlencode(params or {}))
url = self.base_url + url_path
start = self.loop.time()
response = None
try:
with aiohttp.Timeout(timeout or self.timeout):
response = yield from self.session.request(method, url, data=body)
raw_data = yield from response.text()
duration = self.loop.time() - start
except asyncio.TimeoutError as e:
self.log_request_fail(method, url, body, self.loop.time() - start, exception=e)
raise ConnectionTimeout('TIMEOUT', str(e), e)
except FingerprintMismatch as e:
self.log_request_fail(method, url, body, self.loop.time() - start, exception=e)
raise SSLError('N/A', str(e), e)
except ClientError as e:
self.log_request_fail(method, url, body, self.loop.time() - start, exception=e)
raise ConnectionError('N/A', str(e), e)
finally:
if response is not None:
yield from response.release()
# raise errors based on http status codes, let the client handle those if needed
if not (200 <= response.status < 300) and response.status not in ignore:
self.log_request_fail(method, url, body, duration, response.status, raw_data)
self._raise_error(response.status, raw_data)
self.log_request_success(method, url, url_path, body, response.status, raw_data, duration)
return response.status, response.headers, raw_data
0
Example 12
async def json(self, url, params, timeout=None):
with aiohttp.Timeout(timeout or self.timeout):
async with self.session.get(url, params=params) as response:
return await response.json()
0
Example 13
async def get_text(self, url, params, timeout=None):
with aiohttp.Timeout(timeout or self.timeout):
response = await self.session.get(url, params=params)
return response.status, await response.text()
0
Example 14
async def get_bin(self, url, params, timeout=None):
with aiohttp.Timeout(timeout or self.timeout):
response = await self.session.get(url, params=params)
return await response.read()
0
Example 15
async def post_text(self, url, data, timeout=None):
with aiohttp.Timeout(timeout or self.timeout):
response = await self.session.post(url, data=data)
return response.url, await response.text()
0
Example 16
@asyncio.coroutine
def test_recv_timeout(loop, test_client):
@asyncio.coroutine
def handler(request):
ws = web.WebSocketResponse()
yield from ws.prepare(request)
yield from ws.receive_str()
yield from asyncio.sleep(0.1, loop=request.app.loop)
yield from ws.close()
return ws
app = web.Application(loop=loop)
app.router.add_route('GET', '/', handler)
client = yield from test_client(app)
resp = yield from client.ws_connect('/')
resp.send_str('ask')
with pytest.raises(asyncio.TimeoutError):
with aiohttp.Timeout(0.01, loop=app.loop):
yield from resp.receive()
yield from resp.close()
0
Example 17
@asyncio.coroutine
def get_body(self, root_url, urls, visited_urls):
if not root_url.startswith("http"):
root_url = 'http://' + root_url
visited_urls.append(root_url)
parsed_url = urlparse(root_url)
if parsed_url.fragment:
return
domain = parsed_url.netloc
if not domain.endswith('/'):
domain += '/'
file_name = self.make_new_link(root_url)
file_path = ''
patt = '/.*/.*\.'
if re.match(patt, file_name):
file_path, file_name = file_name.rsplit('/', 1)
file_path += '/'
print('path: ', file_path, 'name: ', file_name)
if len(domain) < 4:
sys.exit('invalid taget {}'.format(root_url))
page_path = '/opt/snare/pages/{}'.format(domain)
if not os.path.exists(page_path):
os.mkdir(page_path)
if file_path and not os.path.exists(page_path + file_path):
os.makedirs(page_path + file_path)
data = None
try:
with aiohttp.Timeout(10.0):
with aiohttp.ClientSession() as session:
response = yield from session.get(root_url)
data = yield from response.read()
except Exception as e:
print(e)
else:
response.release()
session.close()
if data is not None:
if re.match(re.compile('.*\.(html|php)'), file_name):
soup = self.replace_links(data, domain, urls)
data = str(soup).encode()
with open(page_path + file_path + file_name, 'wb') as index_fh:
index_fh.write(data)
if '.css' in file_name:
css = cssutils.parseString(data)
for carved_url in cssutils.getUrls(css):
if carved_url.startswith('data'):
continue
carved_url = os.path.normpath(os.path.join(domain, carved_url))
if not carved_url.startswith('http'):
if carved_url.startswith('..') or carved_url.startswith('/'):
carved_url = 'http://' + domain + carved_url
else:
carved_url = 'http://' + carved_url
if carved_url not in visited_urls:
urls.insert(0, carved_url)
for url in urls:
urls.remove(url)
if url in visited_urls:
continue
yield from self.get_body(url, urls, visited_urls)
0
Example 18
def download(req):
with aiohttp.Timeout(_timeout):
return aiohttp.get(_fileurl(req))
0
Example 19
async def send(self, data, headers, timeout=None):
"""Use synchronous interface, because this is a coroutine."""
if timeout is None:
timeout = defaults.TIMEOUT
try:
with aiohttp.Timeout(timeout):
async with self.client.post(self._url,
data=data,
headers=headers) as response:
assert response.status == 202
except asyncio.TimeoutError as e:
print_trace = True
message = ("Connection to Opbeat server timed out "
"(url: %s, timeout: %d seconds)" % (self._url, timeout))
raise TransportException(message, data,
print_trace=print_trace) from e
except AssertionError as e:
print_trace = True
body = await response.read()
if response.status == 429:
message = 'Temporarily rate limited: '
print_trace = False
else:
message = 'Unable to reach Opbeat server: '
message += '%s (url: %s, body: %s)' % (e, self._url, body)
raise TransportException(message, data,
print_trace=print_trace) from e
except Exception as e:
message = 'Unable to reach Opbeat server: %s (url: %s)' % (
e, self._url)
raise TransportException(message, data,
print_trace=print_trace) from e
else:
return response.headers['Location']