本文整理汇总了Python中cachecontrol.CacheControl.post方法的典型用法代码示例。如果您正苦于以下问题:Python CacheControl.post方法的具体用法?Python CacheControl.post怎么用?Python CacheControl.post使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cachecontrol.CacheControl
的用法示例。
在下文中一共展示了CacheControl.post方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getURL
# 需要导入模块: from cachecontrol import CacheControl [as 别名]
# 或者: from cachecontrol.CacheControl import post [as 别名]
def getURL(url, post_data=None, params=None, headers=None, timeout=30, session=None, json=False):
"""
Returns a byte-string retrieved from the url provider.
"""
# request session
cache_dir = sickbeard.CACHE_DIR or _getTempDir()
session = CacheControl(sess=session, cache=caches.FileCache(os.path.join(cache_dir, "sessions")))
# request session headers
req_headers = {"User-Agent": USER_AGENT, "Accept-Encoding": "gzip,deflate"}
if headers:
req_headers.update(headers)
session.headers.update(req_headers)
# request session ssl verify
session.verify = False
# request session paramaters
session.params = params
try:
# Remove double-slashes from url
parsed = list(urlparse.urlparse(url))
parsed[2] = re.sub("/{2,}", "/", parsed[2]) # replace two or more / with one
url = urlparse.urlunparse(parsed)
# request session proxies
if sickbeard.PROXY_SETTING:
logger.log("Using proxy for url: " + url, logger.DEBUG)
session.proxies = {"http": sickbeard.PROXY_SETTING, "https": sickbeard.PROXY_SETTING}
# decide if we get or post data to server
if post_data:
resp = session.post(url, data=post_data, timeout=timeout)
else:
resp = session.get(url, timeout=timeout)
if not resp.ok:
logger.log(
u"Requested url "
+ url
+ " returned status code is "
+ str(resp.status_code)
+ ": "
+ clients.http_error_code[resp.status_code],
logger.DEBUG,
)
return
except requests.exceptions.HTTPError, e:
logger.log(u"HTTP error " + str(e.errno) + " while loading URL " + url, logger.WARNING)
return
示例2: send_answer
# 需要导入模块: from cachecontrol import CacheControl [as 别名]
# 或者: from cachecontrol.CacheControl import post [as 别名]
def send_answer(self, answer, key):
s = requests.session()
s = CacheControl(s, cache=FileCache(os.path.expanduser('~/.tst/cache')))
url = "%s/%s/answers" % (self.url, key)
data = data2json(answer).encode('utf-8')
tokens = JsonFile(os.path.expanduser('~/.tst/tokens.json'))
headers = {"Authorization": "Bearer %s" % tokens.get(self.name)}
try:
response = s.post(url, headers=headers, data=data, allow_redirects=True)
except requests.ConnectionError:
_assert(False, "Connection failed... check your internet connection (1)")
return response
示例3: getURL
# 需要导入模块: from cachecontrol import CacheControl [as 别名]
# 或者: from cachecontrol.CacheControl import post [as 别名]
def getURL(url, post_data=None, params=None, headers={}, timeout=30, session=None, json=False):
"""
Returns a byte-string retrieved from the url provider.
"""
# request session
cache_dir = sickbeard.CACHE_DIR or _getTempDir()
session = CacheControl(sess=session, cache=caches.FileCache(os.path.join(cache_dir, 'sessions')))
# request session headers
session.headers.update({'User-Agent': USER_AGENT, 'Accept-Encoding': 'gzip,deflate'})
session.headers.update(headers)
# request session ssl verify
session.verify = False
# request session paramaters
session.params = params
try:
# request session proxies
if sickbeard.PROXY_SETTING:
logger.log("Using proxy for url: " + url, logger.DEBUG)
session.proxies = {
"http": sickbeard.PROXY_SETTING,
"https": sickbeard.PROXY_SETTING,
}
# decide if we get or post data to server
if post_data:
resp = session.post(url, data=post_data, timeout=timeout)
else:
resp = session.get(url, timeout=timeout)
if not resp.ok:
logger.log(u"Requested url " + url + " returned status code is " + str(
resp.status_code) + ': ' + clients.http_error_code[resp.status_code], logger.DEBUG)
return
except requests.exceptions.HTTPError, e:
logger.log(u"HTTP error " + str(e.errno) + " while loading URL " + url, logger.WARNING)
return
示例4: Request
# 需要导入模块: from cachecontrol import CacheControl [as 别名]
# 或者: from cachecontrol.CacheControl import post [as 别名]
def Request(
url,
method="GET",
headers=DEFAULT_HEADERS,
additional_headers=None,
data=None,
session=None,
allow_redirects=True,
timeout=10,
load_cookies=True,
mobile=False
):
if additional_headers:
headers.update(additional_headers)
try:
session = CacheControl(session)
except Exception as e:
pass
# Error("Init web cache failed!!!", e)
if mobile:
headers["User-Agents"] = MOBILE_IOS_AGENTS
xbmc.log("Requests headers: {0}".format(json.dumps(headers)), 1)
if session:
session.headers.update(headers)
domain = re.search("https*\://(.+?)($|/)", url).group(1)
if load_cookies:
LoadCookies(session, cookies_name=domain)
if data:
response = session.post(url, data=data, allow_redirects=allow_redirects, timeout=timeout, verify=False)
else:
if method == "HEAD":
response = session.head(url, allow_redirects=allow_redirects, timeout=timeout, verify=False)
else:
response = session.get(url, allow_redirects=allow_redirects, timeout=timeout, verify=False)
response.encoding = "utf8"
SaveCookies(session, cookies_name=domain)
return response
else:
if method == "HEAD":
return requests.head(url, headers=headers, allow_redirects=allow_redirects, timeout=timeout, verify=False)
else:
return requests.get(url, headers=headers, allow_redirects=allow_redirects, timeout=timeout, verify=False)
示例5: TwistedEventConsumer
# 需要导入模块: from cachecontrol import CacheControl [as 别名]
# 或者: from cachecontrol.CacheControl import post [as 别名]
class TwistedEventConsumer(EventConsumer):
def __init__(self, queue, api_key, config):
self._queue = queue
""" @type: queue.Queue """
self._session = CacheControl(txrequests.Session())
""" :type: txrequests.Session """
self._api_key = api_key
self._config = config
""" :type: ldclient.twisted.TwistedConfig """
self._looping_call = None
""" :type: LoopingCall"""
def start(self):
self._looping_call = task.LoopingCall(self._consume)
self._looping_call.start(5)
def stop(self):
self._looping_call.stop()
def is_alive(self):
return self._looping_call is not None and self._looping_call.running
def flush(self):
return self._consume()
def _consume(self):
items = []
try:
while True:
items.append(self._queue.get_nowait())
except Empty:
pass
if items:
return self.send_batch(items)
@defer.inlineCallbacks
def send_batch(self, events):
@defer.inlineCallbacks
def do_send(should_retry):
# noinspection PyBroadException
try:
if isinstance(events, dict):
body = [events]
else:
body = events
hdrs = _headers(self._api_key)
uri = self._config.base_uri + '/api/events/bulk'
r = yield self._session.post(uri, headers=hdrs, timeout=(self._config.connect, self._config.read),
data=json.dumps(body))
r.raise_for_status()
except ProtocolError as e:
inner = e.args[1]
if inner.errno == errno.ECONNRESET and should_retry:
log.warning(
'ProtocolError exception caught while sending events. Retrying.')
yield do_send(False)
else:
log.exception(
'Unhandled exception in event consumer. Analytics events were not processed.')
except:
log.exception(
'Unhandled exception in event consumer. Analytics events were not processed.')
try:
yield do_send(True)
finally:
for _ in events:
self._queue.task_done()