本文整理汇总了Python中tornado.httpclient.AsyncHTTPClient类的典型用法代码示例。如果您正苦于以下问题:Python AsyncHTTPClient类的具体用法?Python AsyncHTTPClient怎么用?Python AsyncHTTPClient使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了AsyncHTTPClient类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: es
def es(self, url, query={}, body={}):
http_client = AsyncHTTPClient()
if not url.startswith('/'):
url = '/'+url
for arg in query:
if not isinstance(query[arg], list):
query[arg] = [query[arg]]
try:
response = yield http_client.fetch(
'http://{}{}?{}'.format(
config['api']['elasticsearch'],
url,
utils.url_encode_tornado_arguments(query) \
if query else '',
),
method='POST' if body else 'GET',
body=utils.json_dumps(body) if body else None,
)
return utils.json_loads(response.body)
except HTTPError as e:
try:
extra = utils.json_loads(e.response.body)
except:
extra = {'error': e.response.body.decode('utf-8')}
raise exceptions.Elasticsearch_exception(
e.code,
extra,
)
示例2: ExpandSearch
class ExpandSearch(Spiderman):
"""Expands youtube video network.
"""
functions = 'expand'
def search(self):
self.done = False
self.requests_made = 1
self.network = {}
# callback function parameters
search_key = self.search_key = str(datetime.now())
related_search = self.related_search
client = self.client
cb = lambda x: related_search(x, 0)
video_ids = [str(k) for k in self.input_object['seed']]
global pages
global max_results
self.http_client = AsyncHTTPClient()
for video_id in video_ids:
self.http_client.fetch("http://gdata.youtube.com/feeds/api/videos/{}/related?alt=jsonc&v=2".format(video_id),
callback=cb)
示例3: test_post_participants_valid_request
def test_post_participants_valid_request(self):
db = self.get_db_client().udaan
event = dict(
eventName="dummyEvent",
currentRound=0,
)
body = dict(
names="Dummy Names",
mobileNumber="9123456789"
)
request_body = json.dumps(body)
event_id = yield db.events.insert(event)
str_event_id = str(event_id)
http_client = AsyncHTTPClient()
response = yield http_client.fetch("http://localhost:8000/api/event_management/participants", method="POST",
headers=dict(Authorization=str_event_id), body=request_body)
response_body = json.loads(response.body.decode())
self.assertEqual(response.code, 200)
self.assertEqual(response_body["status"], 200)
participant = yield db.participants.find_one({"_id": ObjectId(response_body["message"])})
yield db.participants.remove({"_id": participant["_id"]})
yield db.events.remove({"_id": event_id})
self.assertEqual(response_body["message"], participant["_id"].__str__())
示例4: test_api
def test_api(self):
print("Running test")
self.mock_requests = {}
# invoke service to be tested
client = AsyncHTTPClient(self.io_loop)
client.fetch(self.test_data['service_endpoint'], self.stop)
mocks_with_assertions = [x for x in self.test_data['mocks'] if 'body' in x['mock']['request']]
for mock in mocks_with_assertions:
self.wait(timeout=30)
self.assertEqual(flatten_text(self.mock_requests[mock['mock']['name']].decode("utf-8")), flatten_text(mock['mock']['request']['body']))
#TODO: Assert request headers
response = self.wait()
# print(response)
# perform assertions
for assertion in self.test_data['assertions']:
if 'http_code' in assertion:
self.assertEqual(response.code, assertion['http_code'])
if 'response' in assertion:
self.assertEqual(flatten_text(response.body.decode("utf-8")), flatten_text(assertion['response']))
if 'content-type' in assertion:
self.assertEqual(response.headers['Content-Type'], assertion['content-type'])
示例5: __sweep
def __sweep(self):
"""
the function called by the cleanup service
"""
#print 'candid: ' + str(len(self.__remove_candidates))
self.__log.info('cleanup service - Sweep started')
for sharing_secret in self.__remove_candidates:
#cleanup all the listeners
candidate = self.__remove_candidates[sharing_secret]
#In case a lot of actions are waiting to be executed
#and are clogged in the space, don't clean it up give it
#a chance for another sweeping period
if not candidate.is_being_processed():
self.__log.info('cleanup service - cleaning candidate for %s' % sharing_secret)
candidate.cleanup()
#notify the load balancer of the cleanup
http = AsyncHTTPClient()
load_balancer = Properties.load_balancer_url
url = '/'.join([load_balancer, 'SharingFactory',sharing_secret])
http.fetch(url, method='DELETE', callback=None)
#yield gen.Task(http.fetch, url, method = 'DELETE')
#remove if from stored sharing spaces
del(self.__sharing_spaces[sharing_secret])
else:
self.__log.info('cleanup service - skipping cleaning candidate for %s is being processed' % sharing_secret)
#now nominate every one
self.__remove_candidates.clear()
for sharing_secret in self.__sharing_spaces:
self.__remove_candidates[sharing_secret] = \
self.__sharing_spaces[sharing_secret]
self.__log.info('cleanup service - Sweep finished')
self.timer = Timer(self.SWEEP_PERIOD, self.__sweep)
self.timer.start()
示例6: __init__
class TaskPool:
def __init__(self, maxClients):
self._ioloop = ioloop.IOLoop()
self._httpClient = AsyncHTTPClient(self._ioloop, maxClients)
self._taskNum = 0
def run(self):
self._check()
self._ioloop.start()
def spawn(self, request, callback, **kwargs):
def wapped(response):
self._taskNum -= 1
try:
callback(response)
except:
print 'spwan error:', traceback.format_exc()
pass
self._taskNum += 1
self._httpClient.fetch(request, wapped, **kwargs)
def _check(self):
def callback():
if self._taskNum == 0:
self._ioloop.stop()
return self._check()
self._ioloop.add_callback(callback)
示例7: access_token_for_id
def access_token_for_id(cls, id, callback):
"""Returns the access token for an id, acquiring a new one if necessary."""
token = Cache.get(cls.auth_cache_key_template % id)
if token:
return IOLoop.instance().add_callback(lambda: callback(token))
# If we don't have an access token cached, see if we have a refresh token
token = TokenIdMapping.lookup_refresh_token(id)
if token:
post_body = urllib.urlencode({
'client_id': Config.get('oauth', 'client-id'),
'client_secret': Config.get('oauth', 'client-secret'),
'refresh_token': token,
'grant_type': 'refresh_token',
})
http_client = AsyncHTTPClient()
return http_client.fetch(
'https://accounts.google.com/o/oauth2/token',
lambda response: cls.on_refresh_complete(response, id, callback),
method='POST',
body=post_body,
request_timeout=20.0,
connect_timeout=15.0,
)
else:
logging.error("Unable to update access token for %s, no refresh token stored.", id)
return IOLoop.instance().add_callback(lambda: callback(None))
示例8: test_with_data
def test_with_data(e, s, a, b):
ss = HTTPScheduler(s)
ss.listen(0)
L = e.map(inc, [1, 2, 3])
L2 = yield e._scatter(['Hello', 'world!'])
yield _wait(L)
client = AsyncHTTPClient()
response = yield client.fetch('http://localhost:%d/memory-load.json' %
ss.port)
out = json.loads(response.body.decode())
assert all(isinstance(v, int) for v in out.values())
assert set(out) == {a.address, b.address}
assert sum(out.values()) == sum(map(getsizeof,
[1, 2, 3, 'Hello', 'world!']))
response = yield client.fetch('http://localhost:%s/memory-load-by-key.json'
% ss.port)
out = json.loads(response.body.decode())
assert set(out) == {a.address, b.address}
assert all(isinstance(v, dict) for v in out.values())
assert all(k in {'inc', 'data'} for d in out.values() for k in d)
assert all(isinstance(v, int) for d in out.values() for v in d.values())
assert sum(v for d in out.values() for v in d.values()) == \
sum(map(getsizeof, [1, 2, 3, 'Hello', 'world!']))
ss.stop()
示例9: test_with_status
def test_with_status(e, s, a, b):
ss = HTTPScheduler(s)
ss.listen(0)
client = AsyncHTTPClient()
response = yield client.fetch('http://localhost:%d/tasks.json' %
ss.port)
out = json.loads(response.body.decode())
assert out['total'] == 0
assert out['processing'] == 0
assert out['failed'] == 0
assert out['in-memory'] == 0
assert out['ready'] == 0
assert out['waiting'] == 0
L = e.map(div, range(10), range(10))
yield _wait(L)
client = AsyncHTTPClient()
response = yield client.fetch('http://localhost:%d/tasks.json' %
ss.port)
out = json.loads(response.body.decode())
assert out['failed'] == 1
assert out['in-memory'] == 9
assert out['ready'] == 0
assert out['total'] == 10
assert out['waiting'] == 0
ss.stop()
示例10: post
def post(self):
json_str = self.get_argument("json_msg")
# print "json_str: ",json_str
value_obj = json.loads(json_str)
com_val = COMMAND_URL_DICT[value_obj["command"]]
com_url = com_val[0]
com_func = com_val[1]
url = "http://115.28.143.67:" + str(PORT) + com_url
print "---------------------------------------"
print "request url: " + url
print "request json: " + json_str
print "---------------------------------------"
if "GET" == com_func:
request = HTTPRequest(url, "GET")
http = AsyncHTTPClient()
response = yield http.fetch(request)
print "---------------------------------------"
print "response json: " + response.body
print "---------------------------------------"
self.write(response.body)
elif "POST" == com_func:
request = HTTPRequest(url, "POST", body=json_str)
http = AsyncHTTPClient()
response = yield http.fetch(request)
print "---------------------------------------"
print "response json: " + response.body
print "---------------------------------------"
self.write(response.body)
else:
pass
示例11: post
def post(self, *args, **kwargs):
# TODO
# save the messages to local database
# file = self.request.files['image'][0]
# file_name = file["filename"]
# image = file['body']
text = self.get_argument("text")
data = {
"from": "Mailgun Sandbox <[email protected]>",
"to": "<[email protected]>",
"subject": "Hello Udaan",
"text": text,
}
data = urlencode(data)
client = AsyncHTTPClient()
headers_object = HTTPHeaders({"X-Mailgun-Variables": dumps({"X-Mailgun-Variables": {"password": "working"}})})
request_object = HTTPRequest("https://api.mailgun.net/v3/sandbox1713f24a60034b5ab5e7fa0ca2faa9b6.mailgun.org"
"/messages",
method="POST",
headers=headers_object,
body=data,
auth_username="api",
auth_password="key-a0bd92feef0ccecb07f199b770449917"
)
print(request_object.headers.get_list("X-Mailgun-Variables"))
response = yield client.fetch(request_object)
client.close()
print(response)
if response.code == 200:
msg = "email send successfully"
self.respond(msg, response.code)
else:
msg = "Please try again"
self.respond(msg, response.code)
示例12: w
def w():
http_client = AsyncHTTPClient()
if req_cnt % 2 == 0:
response = yield http_client.fetch("http://localhost:8889/wait/%s/%s" % (5, req_cnt))
else:
response = yield http_client.fetch("http://localhost:8890/wait/%s/%s" % (1, req_cnt))
print ">>>> response >>>", response, response.body, handler, handler.req_cnt
示例13: tsend_log
def tsend_log(self,
message,
severity,
filename=None,
url=None,
status_code=None,
headers=None,
parameters=None,
stacktrace=False):
from tornado.httpclient import AsyncHTTPClient
d = self._build_message(
message,
severity,
filename,
url,
status_code,
headers,
parameters,
stacktrace)
# want to use the better client here.
AsyncHTTPClient.configure("tornado.curl_httpclient.CurlAsyncHTTPClient")
AsyncHTTPClient().fetch(
self._build_url(),
lambda resp: None,
method="POST",
body=json.dumps(d),
headers=headers)
示例14: get_motion_detection
def get_motion_detection(camera_id, callback):
from tornado.httpclient import HTTPRequest, AsyncHTTPClient
thread_id = camera_id_to_thread_id(camera_id)
if thread_id is None:
error = 'could not find thread id for camera with id %s' % camera_id
logging.error(error)
return callback(error=error)
url = 'http://127.0.0.1:7999/%(id)s/detection/status' % {'id': thread_id}
def on_response(response):
if response.error:
return callback(error=utils.pretty_http_error(response))
enabled = bool(response.body.lower().count('active'))
logging.debug('motion detection is %(what)s for camera with id %(id)s' % {
'what': ['disabled', 'enabled'][enabled],
'id': camera_id})
callback(enabled)
request = HTTPRequest(url, connect_timeout=_MOTION_CONTROL_TIMEOUT, request_timeout=_MOTION_CONTROL_TIMEOUT)
http_client = AsyncHTTPClient()
http_client.fetch(request, callback=on_response)
示例15: test_broadcast
def test_broadcast(s, a, b):
ss = HTTPScheduler(s)
ss.listen(0)
s.services['http'] = ss
aa = HTTPWorker(a)
aa.listen(0)
a.services['http'] = aa
a.service_ports['http'] = aa.port
s.worker_info[a.address]['services']['http'] = aa.port
bb = HTTPWorker(b)
bb.listen(0)
b.services['http'] = bb
b.service_ports['http'] = bb.port
s.worker_info[b.address]['services']['http'] = bb.port
client = AsyncHTTPClient()
a_response = yield client.fetch('http://localhost:%d/info.json' % aa.port)
b_response = yield client.fetch('http://localhost:%d/info.json' % bb.port)
s_response = yield client.fetch('http://localhost:%d/broadcast/info.json'
% ss.port)
assert (json.loads(s_response.body.decode()) ==
{a.address: json.loads(a_response.body.decode()),
b.address: json.loads(b_response.body.decode())})
ss.stop()
aa.stop()
bb.stop()