本文整理汇总了Python中treq.post函数的典型用法代码示例。如果您正苦于以下问题:Python post函数的具体用法?Python post怎么用?Python post使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了post函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: setup_nodes
def setup_nodes(request, node_id):
"""The 'setup_nodes' endpoint is called by the controller node and
sets up the attributes for all the main nodes
"""
servicemap = node.servicemap
print "Servicemap is: "
pprint(servicemap)
if node_id == 'all' or node_id in node.inv_table.keys():
for route in servicemap:
node_id = route['id']
addresses = node.inv_table[node_id]
for addr in addresses:
# Do the initial setup
url = 'http://' + addr + '/setup'
node_routes_json = json.dumps(route['next_hops'])
print "NODE ROUTES JSON: ", str(node_routes_json)
d = treq.post(url, data=node_routes_json)
d.addCallback(node.ack_response)
# Set attributes
attributes = route['attr']
for stat_key, stat_val in attributes.iteritems():
attr_url = 'http://' + addr + '/' + str(stat_key) + '/' + str(stat_val)
d = treq.post(attr_url)
d.addCallback(node.ack_response)
# TODO: Logic is a bit broken here because if we're only setting up one node_id, we still iterate through the entire list
return "OK"
else:
print "Skipping non-matched URL."
开发者ID:luhkevin,项目名称:servicesim,代码行数:29,代码来源:simserver.py
示例2: make_request
def make_request():
if ssl:
response = yield treq.post(url, agent=self.agent, **kwargs)
else:
response = yield treq.post(url, **kwargs)
try:
content = yield treq.content(response)
except:
log.msg("Error while reading body in HTTP response",
level=logger.WARN)
self.stats_client.incr('http.requests.errors.read_body_error')
err = RuntimeError("Error while reading body in HTTP response (Response code: %d)." % response.code)
err.status_code = response.code
raise err
else:
if response.code == 200:
defer.returnValue(content)
else:
log.msg("Error: Bad status code in HTTP response",
http_response_code=response.code,
http_content=content,
level=logger.WARN)
self.stats_client.incr('http.requests.errors.bad_status_code.%d' % response.code)
err = RuntimeError('Error: Bad status code in HTTP response: %d' % response.code)
err.status_code = response.code
raise err
开发者ID:Seekscale,项目名称:smbproxy,代码行数:27,代码来源:fs_cache.py
示例3: _make_request
def _make_request(self, method, payload):
"""
Actually make the HTTP request.
:rtype : twisted.internet.defer.Deferred
"""
# user.getRecentTracks works
# user.getrecenttracks works
# User.getRecentTracks doesn't work, but doesn't error sensibly
# So fuck that, send it lower() to make sure
method = method.lower()
final_payload = {
"api_key": self._api_key,
"format": "json",
"method": method
}
final_payload.update(payload)
# Convert unicode strings to utf8-encoded bytestrings (treq doesn't
# seem to be able to encode unicode strings properly)
for k, v in final_payload.iteritems():
if isinstance(v, unicode):
final_payload[k] = v.encode("utf8")
deferred = treq.post(self.API_URL,
final_payload,
headers={"User-Agent": "Ultros-contrib/LastFM"})
deferred.addCallback(self._handle_response)
return deferred
开发者ID:domainr,项目名称:Ultros-contrib,代码行数:26,代码来源:__init__.py
示例4: insertMultipleLogEntries
def insertMultipleLogEntries(self, entries):
d = treq.post(self.path + "v1/insertmultipleentries",
params={"entries": utils.arbJSONDump(entries)})
d.addCallback(treq.json_content)
d.addCallback(self._verify)
return d
开发者ID:hawkowl,项目名称:edmlogger,代码行数:7,代码来源:apiclient.py
示例5: insertSingleLogEntry
def insertSingleLogEntry(self, entry):
d = treq.post(self.path + "v1/insertentry",
params={"entry": utils.arbJSONDump(entry)})
d.addCallback(treq.json_content)
d.addCallback(self._verify)
return d
开发者ID:hawkowl,项目名称:edmlogger,代码行数:7,代码来源:apiclient.py
示例6: post
def post(self, call, data={}):
url = self.endpoint + call
headers = self.get_auth(call, data)
response = yield treq.post(url)
content = yield response.content()
result = json.loads(content)
returnValue(result)
开发者ID:Mrkebubun,项目名称:sputnik-old,代码行数:7,代码来源:bitfinex.py
示例7: _send_request
def _send_request(self):
if not self.queue:
if self.request_loop.running:
self.request_loop.stop()
return
now = time.time() - 1 # 1 second buffer
if (self.rate_remaining < 1+1 and self.rate_reset > now or
DiscordRestApiLoop.global_wait > now):
self.log.warn("Rate limited: {}".format(self.channel_id))
return
payload = self.queue.pop()
method = payload['method']
url = payload['url']
content = payload['content']
# url = '{}/channels/{}/messages'.format(HOST, self.channel_id)
content = json.dumps({"content": content})
self.log.debug('at _send_request: {} url {}'.format(self.channel_id,
url))
if method == 'post':
d = treq.post(url, content, headers=HEADERS)
elif method == 'patch':
d = treq.patch(url, content, headers=HEADERS)
elif method == 'delete':
d = treq.delete(url, headers=HEADERS)
elif method == 'get':
d = treq.get(url, headers=HEADERS)
d.addCallback(self.update_rate_limits)
if not self.queue:
self.request_loop.stop()
开发者ID:d-dd,项目名称:Yukari,代码行数:29,代码来源:dcrestclient.py
示例8: delete
def delete(self, key, range_end=None, prev_kv=False):
"""
Delete value(s) from etcd.
URL: /v3alpha/kv/deleterange
:param key: key is the first key to delete in the range.
:type key: bytes
:param range_end: range_end is the key following the last key to delete
for the range [key, range_end).\nIf range_end is not given, the range
is defined to contain only the key argument.\nIf range_end is one bit
larger than the given key, then the range is all keys with the prefix
(the given key).\nIf range_end is '\\0', the range is all keys greater
than or equal to the key argument.
:key range_end: bytes
:param prev_kv: If prev_kv is set, etcd gets the previous key-value pairs
before deleting it.\nThe previous key-value pairs will be returned in the
delete response.
:key prev_kv: bool
"""
url = u'{}/v3alpha/kv/deleterange'.format(self._url).encode()
obj = {
u'key': binascii.b2a_base64(key).decode(),
u'range_end': binascii.b2a_base64(range_end).decode() if range_end else None,
u'prev_kv': prev_kv
}
data = json.dumps(obj).encode('utf8')
response = yield treq.post(url, data, headers=self.REQ_HEADERS)
obj = yield treq.json_content(response)
res = Deleted.parse(obj)
returnValue(res)
开发者ID:oberstet,项目名称:scratchbox,代码行数:33,代码来源:etcd.py
示例9: send_notification
def send_notification(self, processor, url, data, ttl, claims=None):
"""Send out a notification to a url for a processor"""
url = url.encode("utf-8")
headers = {"TTL": str(ttl)}
crypto_key = self._crypto_key
claims = claims or self._claims
if self._vapid and claims:
headers.update(self._vapid.sign(claims))
crypto_key = "{};p256ecdsa={}".format(
crypto_key,
self._vapid.public_key_urlsafe_base64
)
if data:
headers.update({
"Content-Type": "application/octet-stream",
"Content-Encoding": "aesgcm",
"Crypto-key": crypto_key,
"Encryption": self._encryption,
})
d = treq.post(url,
data=data,
headers=headers,
allow_redirects=False,
agent=self._agent)
d.addCallback(self._sent_notification, processor)
d.addErrback(self._error_notif, processor)
开发者ID:mozilla-services,项目名称:ap-loadtester,代码行数:26,代码来源:runner.py
示例10: verify
def verify(self, remote_ip, challenge, response):
"""
:param remote_ip:
:param challenge:
:param response:
:returns: Deferred
:raises: Exception
"""
def handle_response(response):
def parse_content(content):
if response.code != 200:
log.msg('Received code: %d from Google for recaptcha' % response.code)
raise Exception("Recaptacha returned code: %d" % response.code)
c = content.splitlines().append('') #being cheeky here
if len(c) < 2:
log.err("Received unexpected response from recaptcha: %s" % content)
raise Exception("Received unexpected response from recaptcha: %s" % content)
return [c[0] == "true", c[1]] #eheh c[1] always exist
return response.content().addCallback(parse_content)
d = treq.post(self.url, data={
'privatekey': self.private_key,
'remoteip': remote_ip,
'challenge': challenge,
'response': response}, timeout=5)
d.addCallback(handle_response)
return d
开发者ID:Mrkebubun,项目名称:sputnik,代码行数:32,代码来源:recaptcha.py
示例11: callRemote
def callRemote(self, method, *args):
scheme = "https://" if self.secure else "http://"
netloc = self.host
if self.port:
netloc += ":%s" % self.port
if self.user:
auth = self.user
if self.password:
auth += ":%s" % self.password
netloc = "%[email protected]%s" % (auth, netloc)
url = scheme + netloc + self.path
data = self.payloadTemplate % (
method,
xmlrpclib.dumps(args, allow_none=self.allowNone),
)
headers = {"User-Agent": "Twisted/XMLRPC", "Content-Type": "text/xml"}
response = yield treq.post(
url,
data,
headers=headers,
timeout=self.connectTimeout,
reactor=self._reactor,
)
body = yield treq.content(response)
parsed = xmlrpclib.loads(body, use_datetime=self.useDateTime)[0][0]
returnValue(parsed)
开发者ID:Fugiman,项目名称:Servrhe,代码行数:28,代码来源:blog.py
示例12: process_item
def process_item(self, item, spider):
# If no processing is made, translation will
# be N/A
item['translation'] = "N/A"
if self.blocking_delay > 0.001:
# This is a bad bad thing
time.sleep(self.blocking_delay)
if self.async_delay > 0.001:
# Emulate an asynchronous call to a translation function
delay = self.async_delay
translate = lambda: "calculated-%s" % item['info']
translation = yield deferLater(reactor, delay, translate)
item['translation'] = translation
if self.downloader_api:
# Do an API call using Scrapy's downloader
formdata = dict(text=item['info'])
request = FormRequest(self.base + "api", formdata=formdata)
response = yield self.crawler.engine.download(request, spider)
item['translation'] = json.loads(response.body)['translation']
if self.treq_api:
# Do an API call using treq
response = yield post(self.base + "api", {"text": item['info']})
json_response = yield response.json()
item['translation'] = json_response['translation']
defer.returnValue(item)
开发者ID:Fighting-Toghter,项目名称:scrapybook,代码行数:30,代码来源:speed.py
示例13: call
def call(self, url, params=None):
"""Override Mandrill's call method to return a deferred."""
params = params or {}
params['key'] = self.apikey
self.log('POST to {}{}.json: {}'.format(ROOT, url, params))
start = time.time()
full_url = '{}{}.json'.format(ROOT, url)
response = yield treq.post(
full_url, data=json.dumps(params), headers=HEADERS)
result = yield response.json()
complete_time = time.time() - start
self.log('Received {} in {}ms: {}'.format(
response.code, complete_time * 1000, result))
self.last_request = dict(
url=url, request_body=params, response_body=result,
response=response, time=complete_time)
if response.code >= 400:
raise self.cast_error(result)
returnValue(result)
开发者ID:lextoumbourou,项目名称:txmandrill-api,代码行数:28,代码来源:txmandrill.py
示例14: watch
def watch(self, group_id, config_key, index=None):
def handle_response(response):
response.addCallback(key_changed)
def key_changed(result):
if group_id in self.groups:
key = result['key'].lstrip(config_key)
if key != 'lb' and key not in self.blocked_instances:
if result.get('newKey'):
# add endpoint
endpoint = json.loads(result['value'])
self.add_endpoint(group_id, endpoint)
elif result['action'] == 'DELETE':
# remove endpoint
endpoint = json.loads(result['prevValue'])
self.remove_endpoint(group_id, endpoint)
self.watch(group_id, config_key, result['index'])
url = 'http://127.0.0.1:4001/v1/watch%s' % config_key
if index:
deferred = post(url, data={'index': index})
else:
deferred = get(url)
deferred.addCallback(handle_response)
return True
开发者ID:jsdir,项目名称:stretch,代码行数:28,代码来源:supervisors.py
示例15: login
def login(self, email, password, accountType = ACCOUNT_TYPE_HOSTED_OR_GOOGLE):
params = {"Email": email, "Passwd": password, "service": self.SERVICE,
"accountType": accountType, "has_permission": "1",
"source": "android", "androidId": self.androidId,
"app": "com.android.vending", "sdk_version": "16" }
resp = yield treq.post(self.URL_LOGIN, params)
if resp.code == http.OK:
data = yield treq.content(resp)
data = data.split()
params = {}
for d in data:
k, v = d.split("=")
params[k.strip()] = v.strip()
if "Auth" in params:
self.setAuthSubToken(params["Auth"])
self.loggedIn = True
else:
raise LoginError("Auth token not found.")
else:
if resp.code == http.FORBIDDEN:
data = yield treq.content(resp)
params = {}
for d in data.split('\n'):
d = d.strip()
if d:
k, v = d.split("=", 1)
params[k.strip()] = v.strip()
if "error" in params:
raise LoginError(params["error"])
else:
raise LoginError("Login failed.")
else:
data = yield treq.content(resp)
raise LoginError("Login failed: error %d <%s>" % (resp.code, data.rstrip(),))
开发者ID:3cky,项目名称:google-play-review-notify,代码行数:34,代码来源:market.py
示例16: main
def main(self, reactor, options):
"""
See :py:meth:`ICommandLineScript.main` for parameter documentation.
:return: A ``Deferred`` which fires when the deployment is complete or
has encountered an error.
"""
body = dumps({"applications": options["application_config"],
"deployment": options["deployment_config"]})
posted = post(options["url"], data=body,
headers={b"content-type": b"application/json"},
persistent=False)
def fail(msg):
raise SystemExit(msg)
def got_response(response):
if response.code != OK:
d = json_content(response)
def got_error(error):
if isinstance(error, dict):
error = error[u"description"] + u"\n"
else:
error = u"Unknown error: " + unicode(error) + "\n"
fail(error)
d.addCallback(got_error)
return d
else:
sys.stdout.write(_OK_MESSAGE)
posted.addCallback(got_response)
return posted
开发者ID:rowhit,项目名称:flocker,代码行数:32,代码来源:script.py
示例17: execute
def execute(self, rcs, success_codes=None):
"""Executes the scaling policy.
:param TestResources rcs: The integration test resources instance.
This provides useful information to complete the request, like
which endpoint to use to make the API request.
:param iterable success_codes: An iterable of HTTP status codes to
expect in the success case. Defaults to 202.
:return: A :class:`Deferred` which, when triggered, removes the scaling
policy. It returns the test resources supplied, easing continuity
of integration test code.
"""
return (
treq.post(
"%sexecute" % self.link,
headers=headers(str(rcs.token)),
pool=self.scaling_group.pool,
).addCallback(check_success,
[202] if success_codes is None else success_codes)
# Policy execution does not return anything meaningful,
# per http://tinyurl.com/ndds6ap (link to docs.rackspace).
# So, we forcefully return our resources here.
.addCallback(lambda _, x: x, rcs)
)
开发者ID:meker12,项目名称:otter,代码行数:26,代码来源:autoscale.py
示例18: get_access_token
def get_access_token(self, code):
""" Gets the access token for the app given the code
Parameters:
- code - the response code
"""
payload = {'redirect_uri': self.redirect_uri,
'code': code,
'grant_type': 'authorization_code'}
if self.scope:
payload['scope'] = self.scope
if self.state:
payload['state'] = self.state
auth_header = base64.b64encode(self.client_id + ':' + self.client_secret)
headers = {'Authorization': ['Basic %s' % auth_header]}
d = treq.post(self.OAUTH_TOKEN_URL, data=payload,
headers=Headers(headers))
response = yield d
if response.status_code is not 200:
raise SpotifyOauthError(response.reason)
token_info = response.json()
token_info = self._add_custom_values_to_token_info(token_info)
self._save_token_info(token_info)
defer.returnValue(token_info)
开发者ID:jimcortez,项目名称:spotipy_twisted,代码行数:27,代码来源:oauth2.py
示例19: set
def set(self, key, value, lease=None, prev_kv=None):
"""
Put puts the given key into the key-value store.
A put request increments the revision of the key-value
store and generates one event in the event history.
URL: /v3alpha/kv/put
:param key: key is the key, in bytes, to put into the key-value store.
:type key: bytes
:param lease: lease is the lease ID to associate with the key in the key-value store. A lease\nvalue of 0 indicates no lease.
:type lease: int
:param prev_kv: If prev_kv is set, etcd gets the previous key-value pair before changing it.\nThe previous key-value pair will be returned in the put response.
:type prev_kv: bool
:param value: value is the value, in bytes, to associate with the key in the key-value store.
:key value: bytes
"""
url = u'{}/v3alpha/kv/put'.format(self._url).encode()
obj = {
u'key': binascii.b2a_base64(key).decode(),
u'value': binascii.b2a_base64(value).decode()
}
data = json.dumps(obj).encode('utf8')
response = yield treq.post(url, data, headers=self.REQ_HEADERS)
obj = yield treq.json_content(response)
revision = obj[u'header'][u'revision']
returnValue(revision)
开发者ID:oberstet,项目名称:scratchbox,代码行数:30,代码来源:etcd.py
示例20: add_to_load_balancer
def add_to_load_balancer(endpoint, auth_token, lb_config, ip_address):
"""
Add an IP addressed to a load balancer based on the lb_config.
TODO: Handle load balancer node metadata.
:param str endpoint: Load balancer endpoint URI.
:param str auth_token: Keystone Auth Token.
:param str lb_config: An lb_config dictionary.
:param str ip_address: The IP Address of the node to add to the load
balancer.
:return: Deferred that fires with the Add Node to load balancer response
as a dict.
"""
lb_id = lb_config['loadBalancerId']
port = lb_config['port']
path = append_segments(endpoint, 'loadbalancers', str(lb_id), 'nodes')
d = treq.post(path,
headers=headers(auth_token),
data=json.dumps({"nodes": [{"address": ip_address,
"port": port,
"condition": "ENABLED",
"type": "PRIMARY"}]}))
d.addCallback(check_success, [200, 202])
d.addErrback(wrap_request_error, endpoint, 'add')
return d.addCallback(treq.json_content)
开发者ID:apurvvkumaria,项目名称:otter,代码行数:28,代码来源:launch_server_v1.py
注:本文中的treq.post函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论