本文整理汇总了Python中requests_futures.sessions.FuturesSession.post方法的典型用法代码示例。如果您正苦于以下问题:Python FuturesSession.post方法的具体用法?Python FuturesSession.post怎么用?Python FuturesSession.post使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类requests_futures.sessions.FuturesSession
的用法示例。
在下文中一共展示了FuturesSession.post方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: CustomStreamListener
# 需要导入模块: from requests_futures.sessions import FuturesSession [as 别名]
# 或者: from requests_futures.sessions.FuturesSession import post [as 别名]
class CustomStreamListener(tweepy.StreamListener):
def __init__(self, socketio, track):
super(CustomStreamListener, self).__init__()
self.socketio = socketio
self.room = track
self.session = FuturesSession()
def get_geonames_username(self):
return "yasyf{}".format(random.randint(1,5))
def on_status(self, status):
if status.coordinates or status.author.location:
data = {'text': status.text.encode('utf-8')}
data.update({k:getattr(status.author, k) for k in ['time_zone', 'location']})
data.update({k:getattr(status, k) for k in ['lang', 'coordinates']})
def add_sentiment(session, response):
data['sentiment'] = response.json()['results']
self.socketio.emit('status', data, room=self.room)
def add_country_code(session, response):
try:
json = response.json()
if json['totalResultsCount'] > 0:
result = json['geonames'][0]
data['country'] = result['countryCode']
data['coordinates'] = {'coordinates': [float(result['lng']), float(result['lat'])]}
else:
return
except:
data['country'] = response.text.strip()
if TEST_MODE:
data['sentiment'] = random.random()
self.socketio.emit('status', data, room=self.room)
else:
url = "http://apiv2.indico.io/sentiment"
args = {'key': os.getenv('INDICOIO_API_KEY')}
self.session.post(url, data={'data': data['text']}, params=args, background_callback=add_sentiment)
if status.coordinates:
url = "http://ws.geonames.org/countryCode"
args = {'lat': status.coordinates['coordinates'][1], 'lng': status.coordinates['coordinates'][0],
'username': self.get_geonames_username()}
self.session.get(url, params=args, background_callback=add_country_code)
else:
url = "http://api.geonames.org/search"
args = {'q': status.author.location, 'username': self.get_geonames_username(),
'maxRows': 1, 'type': 'json'}
self.session.get(url, params=args, background_callback=add_country_code)
return True
def on_error(self, status_code):
print 'Encountered error with status code:', status_code
self.socketio.emit('error', {'status_code': status_code}, room=self.room)
return True
def on_timeout(self):
print 'Timeout...'
return True
示例2: APNsClient
# 需要导入模块: from requests_futures.sessions import FuturesSession [as 别名]
# 或者: from requests_futures.sessions.FuturesSession import post [as 别名]
class APNsClient(object):
def __init__(self, cert_file, use_sandbox=False, use_alternative_port=False):
server = 'api.development.push.apple.com' if use_sandbox else 'api.push.apple.com'
port = 2197 if use_alternative_port else 443
self.cert = cert_file
self.server = "https://{}:{}".format(server, port)
self.__connection = FuturesSession()
self.__connection.mount('https://', HTTP20Adapter())
def send_notification(self, tokens, notification, priority=NotificationPriority.Immediate, topic=None):
# print(notification.dict())
json_payload = json.dumps(notification.dict(), ensure_ascii=False, separators=(',', ':')).encode('utf-8')
headers = {
'apns-priority': priority.value
}
if topic:
headers['apns-topic'] = topic
if not isinstance(tokens, list):
tokens = [tokens]
for token in tokens:
url = '{}/3/device/{}'.format(self.server, token)
self.__connection.post(url, json_payload, headers=headers, cert=self.cert, background_callback=req_callback)
示例3: HttpClient
# 需要导入模块: from requests_futures.sessions import FuturesSession [as 别名]
# 或者: from requests_futures.sessions.FuturesSession import post [as 别名]
class HttpClient(ClientBase):
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()
def send(self, data):
"""
Send the data in a separate thread via HTTP POST.
HTTP introduces some overhead, so to avoid blocking the main thread,
this issues the request in the background.
"""
self.future_session.post(url=self.url, data=data)
示例4: crowdsource_undetected
# 需要导入模块: from requests_futures.sessions import FuturesSession [as 别名]
# 或者: from requests_futures.sessions.FuturesSession import post [as 别名]
def crowdsource_undetected(related_list, files_path, instructions, data_for):
# if no files found then return zero
if not os.listdir(files_path):
return 0
# Remove trailing slashes
files_path = os.path.normpath(files_path)
# Get an api crowd user
api_user = get_api_user()
crowd_user_id = 0
if api_user and 'Id' in api_user:
crowd_user_id = api_user['Id']
# get a crowd job
crowd_job_id = 0
if crowd_user_id > 0:
crowd_job_id = create_api_job(crowd_user_id, os.path.basename(files_path), instructions)
zip_path = None
if crowd_job_id > 0:
# save json object to json file
if related_list is not None and len(related_list) > 0:
sio = StringIO()
json.dump(related_list, sio)
with open(os.path.join(files_path,'%s.json'%data_for), "w") as fjson:
fjson.write(sio.getvalue())
# compress all files in files_path directory
zip_path = os.path.join(files_path, '%s.zip'%data_for)
buff = StringIO()
with zipfile.ZipFile(buff, 'w', zipfile.ZIP_DEFLATED) as zipf:
print 'zipping ' + zip_path
zipdir(files_path, zipf)
print 'zipped ' + zip_path
session = FuturesSession()
# api_uri = 'http://api.opescode.com/api/UserData?id=%s' %str(job_api_id)
api_uri = '{0}/api/UserData?id={1}'.format(service_base_uri, str(crowd_job_id))
logger.info('Calling web api {0} for {1}'.format(api_uri, zip_path))
def bg_cb(sess, resp):
print zip_path, resp.status_code
# if failed then save the files to the recording physical folder
if resp.status_code != 200:
print 'Post file {0} failed with stc={1}'.format(zip_path, str(resp.status_code))
# For now, I will not log this until I find a better way to pass logger to the callback method. Note: callback method has no access to self
logger.error('Post file {0} failed with stc={1}'.format(zip_path, str(resp.status_code)))
else:
logger.info('%s posted successfully'%zip_path)
try:
with open(zip_path, "wb") as f: # use `wb` mode
print 'saving zip ' + zip_path
f.write(buff.getvalue())
print 'zip saved ' + zip_path
if not archive_only:
print 'posting ' + zip_path
session.post(api_uri, files={"archive": buff.getvalue()}, background_callback=bg_cb)
print 'posted ' + zip_path
logger.info('posted %s and awaiting api response.'%zip_path)
except Exception as ex:
logger.error('Exception occured while calling web api.')
return crowd_job_id
示例5: send_ga_event
# 需要导入模块: from requests_futures.sessions import FuturesSession [as 别名]
# 或者: from requests_futures.sessions.FuturesSession import post [as 别名]
def send_ga_event(event, user):
session = FuturesSession()
payload = {
'v': 1,
'tid': settings.GOOGLE_TRACKING_ID,
'uid': google_user_id(user),
'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: check_result
# 需要导入模块: from requests_futures.sessions import FuturesSession [as 别名]
# 或者: from requests_futures.sessions.FuturesSession import post [as 别名]
def check_result(request):
"""
This is NOT A VIEW.
Returns the job status after querying asynchronously. If finished, returns result.
"""
API_KEY = gimmeapikey(request)
jobid = request.session['jobid']
payload = {'apikey':API_KEY}
session = FuturesSession()
try:
future = session.post('https://api.havenondemand.com/1/job/status/'+jobid, data = payload)
r = future.result()
except Exception as e: # This is the correct syntax
return 0
# check if response if valid, else return error.
# r.content is a byte array. To cure that, decode utf-8 is used.
response = r.content.decode('utf-8')
json_data = json.loads(response)
if 'status' in json_data:
if json_data['status'] == 'finished':
request.session['extracted_text'] = json_data['actions'][0]['result']['document'][0]['content']
return json_data['status']
else:
return 0
示例7: send_single
# 需要导入模块: from requests_futures.sessions import FuturesSession [as 别名]
# 或者: from requests_futures.sessions.FuturesSession import post [as 别名]
def send_single(receiver, body):
self = agent_repo.get_byname('~self')
url = 'http://{0}:8888/incoming'.format(receiver.hostname)
session = FuturesSession()
response_future = session.post(url, data={'sender': self.hostname, 'content': body})
# wait for the response to come in
response_future.result()
示例8: testMakePosts
# 需要导入模块: from requests_futures.sessions import FuturesSession [as 别名]
# 或者: from requests_futures.sessions.FuturesSession import post [as 别名]
def testMakePosts(self):
s = FuturesSession()
new_post = json.dumps({'content': 'testing'})
print new_post
p = s.post('https://cs242project.herokuapp.com/submitPost', data=new_post)
res = p.result()
print res
print res.content
r = s.get('https://cs242project.herokuapp.com/getPosts')
res2 = r.result()
print res2.content
self.assertEqual("test", "test")
示例9: LogglyHandler
# 需要导入模块: from requests_futures.sessions import FuturesSession [as 别名]
# 或者: from requests_futures.sessions.FuturesSession import post [as 别名]
class LogglyHandler(logging.Handler):
def __init__(self, token, host, tags=None, fmt=None,
resp_callback=None, exc_callback=None):
logging.Handler.__init__(self)
tags = tags or 'pyloggly'
fmt = fmt or DEFAULT_MESSAGE_FORMAT
self.url = INPUT_URL_FORMAT.format(
host=host,
token=token,
tags=quote(tags, safe=',')
)
self.session = FuturesSession()
self.formatter = jsonlogger.JsonFormatter(fmt)
self.setFormatter(self.formatter)
if resp_callback is not None:
self.resp_callback = resp_callback
if exc_callback is not None:
self.exc_callback = exc_callback
def resp_callback(self, session, resp):
pass
def exc_callback(self, exc):
raise exc
def emit(self, record):
try:
self.session.post(self.url,
data=self.format(record),
background_callback=self.resp_callback)
except RequestException, e:
self.exc_callback(e)
示例10: call_webhook
# 需要导入模块: from requests_futures.sessions import FuturesSession [as 别名]
# 或者: from requests_futures.sessions.FuturesSession import post [as 别名]
def call_webhook(self, original_validated_data, instance=None):
user = self.context.get('request').user
# Get the webhook of the user.
webhook = user.webhooks.first()
# For now we only want to send a POST request for the following events.
events = ['account', 'case', 'contact', 'deal']
# Get the class name of the instance.
model = self.Meta.model._meta.model_name
if webhook and model in events:
if instance:
# Create, so we don't have the instance ID available in the data.
original_validated_data.update({
'id': instance.id,
})
data = {
'type': model,
'data': original_validated_data,
'object': self.data,
'event': 'create',
}
else:
data = {
'type': model,
'data': original_validated_data,
'object': self.data,
'event': 'update',
}
data = json.dumps(data, sort_keys=True, default=lambda x: str(x))
headers = {
'Content-Type': 'application/json',
}
session = FuturesSession()
session.post(webhook.url, data=data, headers=headers)
示例11: av_processing_view
# 需要导入模块: from requests_futures.sessions import FuturesSession [as 别名]
# 或者: from requests_futures.sessions.FuturesSession import post [as 别名]
def av_processing_view(request):
"""
Inputs a audio link, stores jobid to dict, redirects to processing page.
"""
ip = gimmeip(request)
# no need to store linkid when jobid is required in every step. store only jobid instead.
#latest_link_id = Link.objects.filter(linkip=ip, linktype='a').order_by('-created')[0].id
latestlink = Link.objects.filter(linkip=ip).exclude(linktype='i').order_by('-created')[0].linktext
#check if latest_link exists
if latestlink:
API_KEY = gimmeapikey(request)
latest_link = latestlink.strip()
payload = {'apikey':API_KEY, 'url':latest_link}
session = FuturesSession()
try:
future = session.post('https://api.havenondemand.com/1/api/async/recognizespeech/v1', data = payload)
r = future.result()
except Exception as e: # This is the correct syntax
return 0
# r.content is a byte array. To cure that, decode utf-8 is used.
response = r.content.decode('utf-8')
#this works too, to get jobID string
#response = str(r.result().content)[2:-1]
json_data = json.loads(response)
if 'jobID' in json_data:
jobid = json_data['jobID']
# setting jobid and linktext as session variables.
request.session['jobid'] = jobid
request.session['linktext'] = latest_link
return render(request, 'apicall/av_processing.html', { 'link' : latest_link, 'status': 'Processing...'})
else:
return HttpResponseRedirect(reverse('error'))
示例12: connect
# 需要导入模块: from requests_futures.sessions import FuturesSession [as 别名]
# 或者: from requests_futures.sessions.FuturesSession import post [as 别名]
def connect(self):
"""
Connect to the G-Node REST API via HTTP. Note: almost all methods throw an HTTPError or
URLError if the communication fails.
"""
url = urlparse.urljoin(self.location, RestStore.URL_LOGIN)
session = FuturesSession(max_workers=20)
future = session.post(url, {'username': self.user, 'password': self.password})
response = future.result()
self.raise_for_status(response)
if not session.cookies:
raise RuntimeError("Unable to authenticate for user '%s' (status: %d)!"
% (self.user, response.status_code))
self.__session = session
示例13: create_api_job
# 需要导入模块: from requests_futures.sessions import FuturesSession [as 别名]
# 或者: from requests_futures.sessions.FuturesSession import post [as 别名]
def create_api_job(user_id, title, instructions):
session = FuturesSession()
member_job = {'UserId': user_id, 'Id': 0, 'Title': title, 'Instructions': instructions}
headers = {'Content-Type': 'application/json'}
# future = session.post('http://api.opescode.com/api/MemberJobs', json=member_job, headers=headers)
future = session.post('{0}/api/MemberJobs'.format(service_base_uri), json=member_job, headers=headers)
response = future.result()
stc = response.status_code
id = 0
if stc != 200:
print 'get_api_job -> Job not created - Request failed with stc=%s'%str(stc)
logger.error('get_api_job -> Job not created - Request failed with stc=%s'%str(stc))
else:
jsn = response.json()
print 'get_api_job -> created job: ', jsn
id = jsn # this api method returns integer job id
logger.info('get_api_job -> Job created - id=%s'%str(id))
return id
示例14: __init__
# 需要导入模块: from requests_futures.sessions import FuturesSession [as 别名]
# 或者: from requests_futures.sessions.FuturesSession import post [as 别名]
class Tracker:
def __init__(self, app_id: str) -> None:
self._app_id = app_id
self._session = FuturesSession()
def send_async(self, teacher: Teacher) -> Future:
payload = {
"v": 1,
"tid": "UA-2241989-17",
"cid": 555,
"t": "pageview",
"dh": self._app_id,
"dp": teacher.id,
"dt": teacher.name,
}
return self._session.post("http://www.google-analytics.com/collect", payload)
def close(self) -> None:
self._session.close()
示例15: postLogs
# 需要导入模块: from requests_futures.sessions import FuturesSession [as 别名]
# 或者: from requests_futures.sessions.FuturesSession import post [as 别名]
def postLogs(logcache):
#post logs asynchronously with requests workers and check on the results
#expects a queue object from the multiprocessing library
#setting this within the processs to allow requests to establish a keep alive session with async workers.
httpsession = FuturesSession(max_workers=2)
httpsession.trust_env=False #turns off needless and repetitive .netrc check for creds
canQuit=False
logger.info('started posting process')
def backgroundcallback(session, response):
#release the connection back to the pool
try:
r=response.result()
response.close()
except Exception as e:
logger.error('Exception while posting message: %r'%e)
while True:
try:
#see if we have anything to post
#waiting a bit to not end until we are told we can stop.
postdata=logcache.get(False,30)
if postdata is None:
#signalled from parent process that it's ok to stop.
logcache.task_done()
canQuit=True
elif len(postdata)>0:
url=random.choice(options.urls)
r=httpsession.post(url,data=postdata,stream=False)
logcache.task_done()
except Empty as e:
if canQuit:
logger.info('signaling shutdown for threadpool executor')
httpsession.executor.shutdown(wait=True)
break
logger.info('{0} done'.format('log posting task'))