本文整理汇总了Python中requests_futures.sessions.FuturesSession方法的典型用法代码示例。如果您正苦于以下问题:Python sessions.FuturesSession方法的具体用法?Python sessions.FuturesSession怎么用?Python sessions.FuturesSession使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类requests_futures.sessions
的用法示例。
在下文中一共展示了sessions.FuturesSession方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from requests_futures import sessions [as 别名]
# 或者: from requests_futures.sessions import FuturesSession [as 别名]
def __init__(self, cache: bool = False, future: bool = True):
if cache:
redis_conn = redis.StrictRedis(host='redis')
self.session = requests_cache.core.CachedSession(
cache_name='api_cache',
backend='redis', expire_after=60 * 60 * 24 * 30,
allowable_codes=(200,),
allowable_methods=('GET',),
old_data_on_error=False,
connection=redis_conn,
)
else:
self.session = session()
if future:
self.future_session = FuturesSession(max_workers=10, session=self.session)
self.url = self.url_template.format(resource='', token=self.token)
示例2: _init_session
# 需要导入模块: from requests_futures import sessions [as 别名]
# 或者: from requests_futures.sessions import FuturesSession [as 别名]
def _init_session(session, **kwargs):
if session is None:
if kwargs.get('asynchronous'):
session = FuturesSession(max_workers=kwargs.get('max_workers', 8))
else:
session = Session()
if kwargs.get('proxies'):
session.proxies = kwargs.get('proxies')
retries = Retry(
total=3,
backoff_factor=1,
status_forcelist=[429, 500, 502, 503, 504],
method_whitelist=["HEAD", "GET", "OPTIONS", "POST", "TRACE"])
session.mount('https://', TimeoutHTTPAdapter(
max_retries=retries,
timeout=kwargs.get('timeout', DEFAULT_TIMEOUT)))
# TODO: Figure out how to utilize this within the validate_response
# TODO: This will be a much better way of handling bad requests than
# TODO: what I'm currently doing.
# session.hooks['response'] = \
# [lambda response, *args, **kwargs: response.raise_for_status()]
session.headers.update({
"User-Agent": random.choice(USER_AGENT_LIST)
})
return session
示例3: hit
# 需要导入模块: from requests_futures import sessions [as 别名]
# 或者: from requests_futures.sessions import FuturesSession [as 别名]
def hit(self, request, title):
params = {
"v": "1",
"tid": request.registry.settings['analytics'],
"cid": "api",
"t": "pageview",
"dt": "API - {}".format(title),
"dp": request.path
}
payload = urlencode(params)
session = FuturesSession()
debug_url = "/debug" if self.debug else ""
r = session.get("https://www.google-analytics.com{}/collect?{}".format(debug_url, payload))
if self.debug:
response = r.result()
print(response.content)
示例4: send_slos
# 需要导入模块: from requests_futures import sessions [as 别名]
# 或者: from requests_futures.sessions import FuturesSession [as 别名]
def send_slos(queryset_list):
"""
Send SLO requests to each ticket of each queryset of ``queryset_list``
:param list queryset_list: A list a :class:`Ticket` queryset
:return: A list of possibly encoutered :class:`Exception`
:rtype: list
"""
# sending SLO to timed-out validated tickets
async_list = []
session = FuturesSession(
executor=ThreadPoolExecutor(max_workers=settings.CAS_SLO_MAX_PARALLEL_REQUESTS)
)
errors = []
for queryset in queryset_list:
for ticket in queryset:
ticket.logout(session, async_list)
queryset.delete()
for future in async_list:
if future: # pragma: no branch (should always be true)
try:
future.result()
except Exception as error:
errors.append(error)
return errors
示例5: send_ga_event
# 需要导入模块: from requests_futures import sessions [as 别名]
# 或者: from requests_futures.sessions import FuturesSession [as 别名]
def send_ga_event(event, user):
session = FuturesSession()
payload = {
"v": 1,
"tid": settings.GOOGLE_TRACKING_ID,
"t": "event",
"ec": "email",
"ea": event.event_type,
"cm": "email",
}
if event.esp_event:
payload["ua"] = event.esp_event.get("user-agent")
payload["dt"] = event.esp_event.get("subject", [None])[0]
payload["cn"] = event.esp_event.get("campaign_name", None)
payload["cs"] = event.esp_event.get("campaign_source", None)
payload["cc"] = payload["el"] = event.esp_event.get("email_id", None)
payload["dp"] = "%s/%s" % (payload["cc"], event.event_type)
else:
logger.warn("No ESP event found for event: %s" % event.__dict__)
logger.info("Sending mail event data Analytics: %s" % payload)
session.post("https://www.google-analytics.com/collect", data=payload)
示例6: test_send_requests_worker
# 需要导入模块: from requests_futures import sessions [as 别名]
# 或者: from requests_futures.sessions import FuturesSession [as 别名]
def test_send_requests_worker(monkeypatch):
load = [
{
"url": "http://shadowreader.example.com",
"req_method": "POST",
"request_uri": "/test",
}
]
headers = {"User-Agent": "sr_pytest"}
fut = FuturesSession().get("http://www.example.com")
monkeypatch.setattr("libs.worker._send_request", lambda a, b, c, d, e, f, g: fut)
futs, timeouts, exceptions = worker.send_requests_worker(
load=load, delay=0.1, random_delay=True, headers=headers
)
assert len(futs) == 1
fut = futs[0]["fut"]
assert fut.result().status_code == 200
示例7: __init__
# 需要导入模块: from requests_futures import sessions [as 别名]
# 或者: from requests_futures.sessions import FuturesSession [as 别名]
def __init__(
self, default_headers=None, max_requests=10, rate_limit=0,
req_timeout=None, max_retry=10, total_retry=100, drop_404s=False,
):
"""Create the MultiRequest.
Args:
default_headers - A dict of headers which will be added to every request
max_requests - Maximum number of requests to issue at once
rate_limit - Maximum number of requests to issue per second
req_timeout - Maximum number of seconds to wait without reading a response byte before deciding an error has occurred
max_retry - The total number of attempts to retry a single batch of requests
total_retry - The total number of request retries that can be made through the entire session
Note there is a difference between `max_retry` and `total_retry`:
- `max_retry` refers to how many times a batch of requests will be re-issued collectively
- `total_retry` refers to a limit on the total number of outstanding requests made
Once the latter is exhausted, no failed request within the whole session will be retried.
"""
self._default_headers = default_headers
self._max_requests = max_requests
self._req_timeout = req_timeout or 25.0
self._max_retry = max_retry
self._drop_404s = drop_404s
self._rate_limiter = RateLimiter(rate_limit) if rate_limit else None
self._availability_limiter = AvailabilityLimiter(total_retry) if total_retry else None
self._session = FuturesSession(max_workers=max_requests)
retries = Retry(total=0, status_forcelist=[500, 502, 503, 504], raise_on_status=True)
self._session.mount(
'https://', SSLAdapter(
max_retries=retries, pool_maxsize=max_requests, pool_connections=max_requests,
),
)
示例8: open
# 需要导入模块: from requests_futures import sessions [as 别名]
# 或者: from requests_futures.sessions import FuturesSession [as 别名]
def open(self):
if self.request == 'hyper':
if self.http2:
self.__http = hyper.HTTP20Connection(self.host, self.port, proxy_host=self.realhost, proxy_port=self.realport, proxy_headers=self.proxy_headers)
else:
self.__http = hyper.HTTPConnection(self.host, self.port, proxy_host=self.realhost, proxy_port=self.realport, proxy_headers=self.proxy_headers)
elif self.request == 'httpx':
if self.http2:
self.__http = httpx.AsyncClient(base_url='%s://%s' % (self.scheme, self.host), http2=self.http2)
else:
self.__http = httpx.Client(base_url='%s://%s' % (self.scheme, self.host))
elif self.request == 'requests':
self.__http = requests.Session()
if self.using_proxy():
self.__http.proxies = urllib.request.getproxies()
elif self.request == 'requests-futures':
self.__http = FuturesSession()
if self.using_proxy():
self.__http.proxies = urllib.request.getproxies()
elif self.request == 'httplib2':
self.__http = httplib2.Http()
else:
if self.scheme == 'http':
self.__http = http_client.HTTPConnection(self.host, self.port)
elif self.scheme == 'https':
self.__http = http_client.HTTPSConnection(self.host, self.port)
if self.using_proxy():
self.__http.set_tunnel(self.realhost, self.realport, self.proxy_headers)
示例9: __init__
# 需要导入模块: from requests_futures import sessions [as 别名]
# 或者: from requests_futures.sessions import FuturesSession [as 别名]
def __init__(self, host='localhost', port=8094, tags=None):
# only import HttpClient's dependencies if using HttpClient
# if they're not found, inform the user how to install them
try:
from requests_futures.sessions import FuturesSession
except ImportError:
raise ImportError('pytelegraf[http] must be installed to use HTTP transport')
super(HttpClient, self).__init__(host, port, tags)
# the default url path for writing metrics to Telegraf is /write
self.url = 'http://{host}:{port}/write'.format(host=self.host, port=self.port)
# create a session to reuse the TCP socket when possible
self.future_session = FuturesSession()
示例10: HTTPInstanceGenerator
# 需要导入模块: from requests_futures import sessions [as 别名]
# 或者: from requests_futures.sessions import FuturesSession [as 别名]
def HTTPInstanceGenerator(action, instance_times, blocking_cli, param_file=None):
if len(instance_times) == 0:
return False
session = FuturesSession(max_workers=15)
url = base_url + action
parameters = {'blocking': blocking_cli, 'result': RESULT}
authentication = (user_pass[0], user_pass[1])
after_time, before_time = 0, 0
if param_file == None:
st = 0
for t in instance_times:
st = st + t - (after_time - before_time)
before_time = time.time()
if st > 0:
time.sleep(st)
future = session.post(url, params=parameters, auth=authentication, verify=False)
after_time = time.time()
else: # if a parameter file is provided
try:
param_file_body = param_file_cache[param_file]
except:
with open(param_file, 'r') as f:
param_file_body = json.load(f)
param_file_cache[param_file] = param_file_body
for t in instance_times:
st = t - (after_time - before_time)
if st > 0:
time.sleep(st)
before_time = time.time()
future = session.post(url, params=parameters, auth=authentication,
json=param_file_body, verify=False)
after_time = time.time()
return True
示例11: BinaryDataHTTPInstanceGenerator
# 需要导入模块: from requests_futures import sessions [as 别名]
# 或者: from requests_futures.sessions import FuturesSession [as 别名]
def BinaryDataHTTPInstanceGenerator(action, instance_times, blocking_cli, data_file):
"""
TODO: Automate content type
"""
url = base_gust_url + action
session = FuturesSession(max_workers=15)
if len(instance_times) == 0:
return False
after_time, before_time = 0, 0
try:
data = binary_data_cache[data_file]
except:
data = open(data_file, 'rb').read()
binary_data_cache[data_file] = data
for t in instance_times:
st = t - (after_time - before_time)
if st > 0:
time.sleep(st)
before_time = time.time()
session.post(url=url, headers={'Content-Type': 'image/jpeg'},
params={'blocking': blocking_cli, 'result': RESULT},
data=data, auth=(user_pass[0], user_pass[1]), verify=False)
after_time = time.time()
return True
示例12: _async_requests
# 需要导入模块: from requests_futures import sessions [as 别名]
# 或者: from requests_futures.sessions import FuturesSession [as 别名]
def _async_requests(urls):
"""
Sends multiple non-blocking requests. Returns
a list of responses.
:param urls:
List of urls
"""
session = FuturesSession(max_workers=30)
futures = [
session.get(url)
for url in urls
]
return [ future.result() for future in futures ]
示例13: _get_data
# 需要导入模块: from requests_futures import sessions [as 别名]
# 或者: from requests_futures.sessions import FuturesSession [as 别名]
def _get_data(self, key, params={}, **kwargs):
config = self._CONFIG[key]
params = self._construct_params(config, params)
urls = self._construct_urls(config, params, **kwargs)
response_field = config['response_field']
try:
if isinstance(self.session, FuturesSession):
data = self._async_requests(
response_field, urls, params, **kwargs)
else:
data = self._sync_requests(
response_field, urls, params, **kwargs)
return data
except ValueError:
return {'error': 'HTTP 404 Not Found. Please try again'}
示例14: multi_futures_app_request
# 需要导入模块: from requests_futures import sessions [as 别名]
# 或者: from requests_futures.sessions import FuturesSession [as 别名]
def multi_futures_app_request(
app_ids, headers=None, verify=True, params=None, workers=s.CONCURRENT_REQUESTS
):
"""
:param app_ids: a list of app IDs.
:param headers: a dictionary of custom headers to use.
:param verify: bool for requesting SSL verification.
:return: a list of all apps' detail data
"""
session = FuturesSession(max_workers=workers)
headers = default_headers() if headers is None else headers
responses = [
session.get(
build_url("details", app_id),
headers=headers,
verify=verify,
params=params,
hooks={"response": parse_app_details_response_hook},
)
for app_id in app_ids
]
apps = []
for i, response in enumerate(responses):
try:
result = response.result()
app_json = result.app_details_data
app_json.update({"app_id": app_ids[i], "url": result.url})
apps.append(response.result().app_details_data)
except requests.exceptions.RequestException as e:
log.error(
"Error occurred fetching {app}: {err}".format(
app=app_ids[i], err=str(e)
)
)
return apps
示例15: get_async
# 需要导入模块: from requests_futures import sessions [as 别名]
# 或者: from requests_futures.sessions import FuturesSession [as 别名]
def get_async():
# [START requests_get]
session = FuturesSession()
url = 'http://www.google.com/humans.txt'
rpc = session.get(url)
# ... do other things ...
resp = make_response(rpc.result().text)
resp.headers['Content-type'] = 'text/plain'
return resp
# [END requests_get]