本文整理汇总了Python中treq.post函数的典型用法代码示例。如果您正苦于以下问题:Python post函数的具体用法?Python post怎么用?Python post使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了post函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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."
示例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
示例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
示例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
示例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
示例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)
示例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()
示例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)
示例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)
示例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
示例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)
示例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)
示例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)
示例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
示例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(),))