本文整理汇总了Python中requests.structures.CaseInsensitiveDict.get方法的典型用法代码示例。如果您正苦于以下问题:Python CaseInsensitiveDict.get方法的具体用法?Python CaseInsensitiveDict.get怎么用?Python CaseInsensitiveDict.get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类requests.structures.CaseInsensitiveDict
的用法示例。
在下文中一共展示了CaseInsensitiveDict.get方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: string_to_sign
# 需要导入模块: from requests.structures import CaseInsensitiveDict [as 别名]
# 或者: from requests.structures.CaseInsensitiveDict import get [as 别名]
def string_to_sign(self, request):
"""
Generates the string we need to sign on.
Params:
- request The request object
Returns
String ready to be signed on
"""
# We'll use case insensitive dict to store the headers
h = CaseInsensitiveDict()
# Add the hearders
h.update(request.headers)
# If we have an 'x-amz-date' header,
# we'll try to use it instead of the date
if b'x-amz-date' in h or 'x-amz-date' in h:
date = ''
else:
# No x-amz-header, we'll generate a date
date = h.get('Date') or self._get_date()
# Set the date header
request.headers['Date'] = date
# A fix for the content type header extraction in python 3
# This have to be done because requests will try to set
# application/www-url-encoded header if we pass bytes as the content,
# and the content-type is set with a key that is b'Content-Type' and
# not 'Content-Type'
content_type = ''
if b'Content-Type' in request.headers:
# Fix content type
content_type = h.get(b'Content-Type')
del request.headers[b'Content-Type']
request.headers['Content-Type'] = content_type
# The string we're about to generate
# There's more information about it here:
# http://docs.aws.amazon.com/AmazonS3/latest/dev/
# RESTAuthentication.html#ConstructingTheAuthenticationHeader
msg = [
# HTTP Method
request.method,
# MD5 If provided
h.get(b'Content-MD5', '') or h.get('Content-MD5', ''),
# Content type if provided
content_type or h.get('Content-Type', ''),
# Date
date,
# Canonicalized special amazon headers and resource uri
self._get_canonicalized_amz_headers(h) +
self._get_canonicalized_resource(request)
]
# join with a newline and return
return '\n'.join(msg)
示例2: Attachment
# 需要导入模块: from requests.structures import CaseInsensitiveDict [as 别名]
# 或者: from requests.structures.CaseInsensitiveDict import get [as 别名]
class Attachment(object):
def __init__(self, part):
encoding = part.encoding or 'utf-8'
self.headers = CaseInsensitiveDict({
k.decode(encoding): v.decode(encoding)
for k, v in part.headers.items()
})
self.content_type = self.headers.get('Content-Type', None)
self.content_id = self.headers.get('Content-ID', None)
self.content_location = self.headers.get('Content-Location', None)
self._part = part
def __repr__(self):
return '<Attachment(%r, %r)>' % (self.content_id, self.content_type)
@cached_property
def content(self):
"""Return the content of the attachment
:rtype: bytes or str
"""
encoding = self.headers.get('Content-Transfer-Encoding', None)
content = self._part.content
if encoding == 'base64':
return base64.b64decode(content)
elif encoding == 'binary':
return content.strip(b'\r\n')
else:
return content
示例3: string_to_sign
# 需要导入模块: from requests.structures import CaseInsensitiveDict [as 别名]
# 或者: from requests.structures.CaseInsensitiveDict import get [as 别名]
def string_to_sign(self, request):
h = CaseInsensitiveDict()
h.update(request.headers)
# Try to use
if b'x-amz-date' in h or 'x-amz-date' in h:
date = ''
else:
date = h.get('Date') or self._get_date()
request.headers['Date'] = date
# Set the date header
request.headers['Date'] = date
# A fix for the content type header extraction in python 3
# This have to be done because requests will try to set application/www-url-encoded herader
# if we pass bytes as the content, and the content-type is set with a key that is b'Content-Type' and not
# 'Content-Type'
content_type = ''
if b'Content-Type' in request.headers:
# Fix content type
content_type = h.get(b'Content-Type')
del request.headers[b'Content-Type']
request.headers['Content-Type'] = content_type
msg = [
request.method,
h.get(b'Content-MD5', '') or h.get('Content-MD5', ''),
content_type or h.get('Content-Type', ''),
date,
self._get_canonicalized_amz_headers(h) + self._get_canonicalized_resource(request)
]
return '\n'.join(msg)
示例4: test_get
# 需要导入模块: from requests.structures import CaseInsensitiveDict [as 别名]
# 或者: from requests.structures.CaseInsensitiveDict import get [as 别名]
def test_get(self):
cid = CaseInsensitiveDict()
cid["spam"] = "oneval"
cid["SPAM"] = "blueval"
assert cid.get("spam") == "blueval"
assert cid.get("SPAM") == "blueval"
assert cid.get("sPam") == "blueval"
assert cid.get("notspam", "default") == "default"
示例5: test_get
# 需要导入模块: from requests.structures import CaseInsensitiveDict [as 别名]
# 或者: from requests.structures.CaseInsensitiveDict import get [as 别名]
def test_get(self):
cid = CaseInsensitiveDict()
cid['spam'] = 'oneval'
cid['SPAM'] = 'blueval'
self.assertEqual(cid.get('spam'), 'blueval')
self.assertEqual(cid.get('SPAM'), 'blueval')
self.assertEqual(cid.get('sPam'), 'blueval')
self.assertEqual(cid.get('notspam', 'default'), 'default')
示例6: prepare_response
# 需要导入模块: from requests.structures import CaseInsensitiveDict [as 别名]
# 或者: from requests.structures.CaseInsensitiveDict import get [as 别名]
def prepare_response(self, cached):
"""Verify our vary headers match and construct a real urllib3
HTTPResponse object.
"""
# Special case the '*' Vary value as it means we cannot actually
# determine if the cached response is suitable for this request.
if "*" in cached.get("vary", {}):
return
body_raw = cached["response"].pop("body")
headers = CaseInsensitiveDict(data=cached['response']['headers'])
if headers.get('transfer-encoding', '') == 'chunked':
headers.pop('transfer-encoding')
cached['response']['headers'] = headers
try:
body = io.BytesIO(body_raw)
except TypeError:
# This can happen if cachecontrol serialized to v1 format (pickle)
# using Python 2. A Python 2 str(byte string) will be unpickled as
# a Python 3 str (unicode string), which will cause the above to
# fail with:
#
# TypeError: 'str' does not support the buffer interface
body = io.BytesIO(body_raw.encode('utf8'))
return HTTPResponse(
body=body,
preload_content=False,
**cached["response"]
)
示例7: DDWRT
# 需要导入模块: from requests.structures import CaseInsensitiveDict [as 别名]
# 或者: from requests.structures.CaseInsensitiveDict import get [as 别名]
class DDWRT(Router):
def __init__(self, conf, hostnames):
self.hostnames = CaseInsensitiveDict()
self.hostnames.update(hostnames)
self.conf = conf
self.auth = self.conf.auth()
def clients(self):
""" Receives all currently logged in users in a wifi network.
:rtype : list
:return: Returns a list of dicts, containing the following keys: mac, ipv4, seen, hostname
"""
clients = self._get_clients_raw()
clients_json = []
for client in clients:
client_hostname_from_router = client[0]
client_ipv4 = client[1].strip()
client_mac = client[2].strip().upper()
client_hostname = self.hostnames.get(client_mac, client_hostname_from_router).strip()
client_connections = int(client[3].strip())
# Clients with less than 20 connections are considered offline
if client_connections < 20:
continue
clients_json.append({
'mac': client_mac,
'ipv4': client_ipv4,
'seen': int(time.time()),
'hostname': client_hostname,
})
logger.debug('The router got us {} clients.'.format(len(clients_json)))
logger.debug(str(clients_json))
return clients_json
def _get_clients_raw(self):
info_page = self.conf.internal()
response = requests.get(info_page, auth=self.auth)
logger.info('Got response from router with code {}.'.format(response.status_code))
return DDWRT._convert_to_clients(response.text) or []
@staticmethod
def _convert_to_clients(router_info_all):
# Split router info in lines and filter empty info
router_info_lines = filter(None, router_info_all.split("\n"))
# Get key / value of router info
router_info_items = dict()
for item in router_info_lines:
key, value = item[1:-1].split("::") # Remove curly braces and split
router_info_items[key.strip()] = value.strip()
# Get client info as a list
arp_table = utils.groupn(router_info_items['arp_table'].replace("'", "").split(","), 4)
dhcp_leases = utils.groupn(router_info_items['dhcp_leases'].replace("'", "").split(","), 5)
return arp_table if (len(arp_table) > 0) else []
示例8: read_resp_record
# 需要导入模块: from requests.structures import CaseInsensitiveDict [as 别名]
# 或者: from requests.structures.CaseInsensitiveDict import get [as 别名]
def read_resp_record(self, resp_headers, payload):
len_ = payload.tell()
payload.seek(0)
warc_headers = self.parser.parse(payload)
warc_headers = CaseInsensitiveDict(warc_headers.headers)
record_type = warc_headers.get('WARC-Type', 'response')
if record_type == 'response':
status_headers = self.parser.parse(payload)
else:
status_headers = None
record = ArcWarcRecord('warc', record_type, warc_headers, payload,
status_headers, '', len_)
if record_type == 'response':
self._set_header_buff(record)
self.ensure_digest(record)
return record_type, record
示例9: prepare_response
# 需要导入模块: from requests.structures import CaseInsensitiveDict [as 别名]
# 或者: from requests.structures.CaseInsensitiveDict import get [as 别名]
def prepare_response(self, request, cached):
"""Verify our vary headers match and construct a real urllib3
HTTPResponse object.
"""
# Special case the '*' Vary value as it means we cannot actually
# determine if the cached response is suitable for this request.
# This case is also handled in the controller code when creating
# a cache entry, but is left here for backwards compatibility.
if "*" in cached.get("vary", {}):
return
# Ensure that the Vary headers for the cached response match our
# request
for header, value in cached.get("vary", {}).items():
if request.headers.get(header, None) != value:
return
body_raw = cached["response"].pop("body")
headers = CaseInsensitiveDict(data=cached["response"]["headers"])
if headers.get("transfer-encoding", "") == "chunked":
headers.pop("transfer-encoding")
cached["response"]["headers"] = headers
try:
body = io.BytesIO(body_raw)
except TypeError:
# This can happen if cachecontrol serialized to v1 format (pickle)
# using Python 2. A Python 2 str(byte string) will be unpickled as
# a Python 3 str (unicode string), which will cause the above to
# fail with:
#
# TypeError: 'str' does not support the buffer interface
body = io.BytesIO(body_raw.encode("utf8"))
return HTTPResponse(body=body, preload_content=False, **cached["response"])
示例10: MessageParser
# 需要导入模块: from requests.structures import CaseInsensitiveDict [as 别名]
# 或者: from requests.structures.CaseInsensitiveDict import get [as 别名]
class MessageParser(object):
def __init__(self, name, content, init_headers={}):
self.name = name
self.content = content
self.headers = CaseInsensitiveDict(init_headers)
self.html_stats = HTMLStats()
self.email_stats = EmailStats()
if '\n\n' in self.content:
self.head, self.body = self.content.split('\n\n', 1)
else:
self.head, self.body = "", ""
self._parse_headers()
self._decode_body()
self._count_links()
@property
def subject(self):
return self.headers['Subject']
@property
def mime_type(self):
if 'Content-Type' in self.headers:
content_type = self.headers['Content-Type']
mime_type = content_type.split(';', 1)[0]
mime_type = mime_type.strip().lower()
return mime_type
@property
def charset(self):
if 'Content-Type' in self.headers:
content_type = self.headers['Content-Type']
rem = RE_CHARSET.search(content_type)
if rem:
charset = rem.group(1).lower()
return charset
@property
def is_multipart(self):
if self.mime_type and self.mime_type.startswith('multipart/'):
return True
else:
return False
def _get_boundary(self):
if 'Content-Type' in self.headers:
content_type = self.headers['Content-Type']
rem = RE_HEADER_BOUNDARY.search(content_type)
if rem:
return rem.group(1)
def _parse_headers(self):
lines = self.head.split('\n')
# Drop the "From..." line
while lines and not RE_HEADER.match(lines[0]):
del lines[0]
while lines:
line = self._get_header_line(lines)
key, value = line.split(':', 1)
key, value = key.strip(), value.strip()
self.headers[key] = value
def _get_header_line(self, lines):
line_parts = [lines.pop(0)]
while lines and (lines[0].startswith('\t') or
lines[0].startswith(' ')):
line_parts.append(lines.pop(0))
line = " ".join([part.strip() for part in line_parts])
return line
def _decode_body(self):
if self.mime_type and (self.mime_type.startswith('image/') or
self.mime_type.startswith('application/')):
LOG.info("Body marked as image, skipping body")
self.email_stats['attached_images'] += 1
self.body = ""
return
if self.is_multipart:
LOG.debug("Detected multipart/* content-type")
self._decode_multipart_body()
else:
self._decode_single_body()
def _decode_single_body(self):
self.body = self.body.strip()
cte = self.headers.get('Content-Transfer-Encoding', '').lower()
if 'quoted-printable' in cte:
LOG.debug("Detected quoted-printable encoding, decoding")
self.body = quopri.decodestring(self.body)
if 'base64' in cte:
LOG.debug("Detected base64 encoding, decoding")
try:
self.body = base64.decodestring(self.body)
except base64.binascii.Error:
LOG.info("base64 decoder failed, trying partial decoding")
self.body = base64_partial_decode(self.body)
#.........这里部分代码省略.........
示例11: tizgmusicproxy
# 需要导入模块: from requests.structures import CaseInsensitiveDict [as 别名]
# 或者: from requests.structures.CaseInsensitiveDict import get [as 别名]
class tizgmusicproxy(object):
"""A class for logging into a Google Play Music account and retrieving song
URLs.
"""
all_songs_album_title = "All Songs"
thumbs_up_playlist_name = "Thumbs Up"
# pylint: disable=too-many-instance-attributes,too-many-public-methods
def __init__(self, email, password, device_id):
self.__gmusic = Mobileclient()
self.__email = email
self.__device_id = device_id
self.logged_in = False
self.queue = list()
self.queue_index = -1
self.play_queue_order = list()
self.play_modes = TizEnumeration(["NORMAL", "SHUFFLE"])
self.current_play_mode = self.play_modes.NORMAL
self.now_playing_song = None
userdir = os.path.expanduser('~')
tizconfig = os.path.join(userdir, ".config/tizonia/." + email + ".auth_token")
auth_token = ""
if os.path.isfile(tizconfig):
with open(tizconfig, "r") as f:
auth_token = pickle.load(f)
if auth_token:
# 'Keep track of the auth token' workaround. See:
# https://github.com/diraimondo/gmusicproxy/issues/34#issuecomment-147359198
print_msg("[Google Play Music] [Authenticating] : " \
"'with cached auth token'")
self.__gmusic.android_id = device_id
self.__gmusic.session._authtoken = auth_token
self.__gmusic.session.is_authenticated = True
try:
self.__gmusic.get_registered_devices()
except CallFailure:
# The token has expired. Reset the client object
print_wrn("[Google Play Music] [Authenticating] : " \
"'auth token expired'")
self.__gmusic = Mobileclient()
auth_token = ""
if not auth_token:
attempts = 0
print_nfo("[Google Play Music] [Authenticating] : " \
"'with user credentials'")
while not self.logged_in and attempts < 3:
self.logged_in = self.__gmusic.login(email, password, device_id)
attempts += 1
with open(tizconfig, "a+") as f:
f.truncate()
pickle.dump(self.__gmusic.session._authtoken, f)
self.library = CaseInsensitiveDict()
self.song_map = CaseInsensitiveDict()
self.playlists = CaseInsensitiveDict()
self.stations = CaseInsensitiveDict()
def logout(self):
""" Reset the session to an unauthenticated, default state.
"""
self.__gmusic.logout()
def set_play_mode(self, mode):
""" Set the playback mode.
:param mode: curren tvalid values are "NORMAL" and "SHUFFLE"
"""
self.current_play_mode = getattr(self.play_modes, mode)
self.__update_play_queue_order()
def current_song_title_and_artist(self):
""" Retrieve the current track's title and artist name.
"""
logging.info("current_song_title_and_artist")
song = self.now_playing_song
if song:
title = to_ascii(song.get('title'))
artist = to_ascii(song.get('artist'))
logging.info("Now playing %s by %s", title, artist)
return artist, title
else:
return '', ''
def current_song_album_and_duration(self):
""" Retrieve the current track's album and duration.
"""
logging.info("current_song_album_and_duration")
song = self.now_playing_song
if song:
album = to_ascii(song.get('album'))
duration = to_ascii \
#.........这里部分代码省略.........
示例12: post
# 需要导入模块: from requests.structures import CaseInsensitiveDict [as 别名]
# 或者: from requests.structures.CaseInsensitiveDict import get [as 别名]
def post(self):
form = RequestDataForm()
data = form.data.data
lines = data.splitlines(True)
if len(lines) < 3:
return 'data less 3 lines'
origin_headers = []
body = []
body_start = False
for index, line in enumerate(lines):
if index == 0:
method, path, _ = line.split(' ')
continue
if not line.split():
body_start = True
continue
if body_start:
body.append(line)
else:
line = line.strip()
key, value = line.split(': ', 1)
origin_headers.append((key, value))
# for get header value
header_dict = CaseInsensitiveDict(origin_headers)
method = method.lower()
body = ''.join(body)
content_type = header_dict.get('Content-Type', '')
# set headers
headers = []
origin_host = header_dict.get('Host')
if form.host.data and origin_host and form.host.data != origin_host:
headers.append(('Host', header_dict.get('Host')))
user_agent = header_dict.get('User-Agent')
referer = header_dict.get('Referer')
if user_agent:
headers.append(('User-Agent', user_agent))
if referer:
headers.append(('Referer', referer))
# set cookie
cookies = []
cookie = header_dict.get('Cookie')
C = SimpleCookie(cookie)
for morsel in C.values():
cookies.append((morsel.key, morsel.coded_value))
host = form.host.data or header_dict.get('Host')
p = urlsplit(path)
url = urljoin('http://{}'.format(host), p.path)
params = [(x, repr_value(y)) for x, y in parse_qsl(p.query)]
if not content_type:
pass
elif 'x-www-form-urlencoded' in content_type:
body = [(x, repr_value(y)) for x, y in parse_qsl(body)]
elif 'json' in content_type:
body = [(x, repr_value(y)) for x, y in json.loads(body).items()]
else:
headers.append(('Content-Type', content_type))
code = render_template(
'code.html',
method=method,
url=url,
params=params,
body=body,
headers=headers,
cookies=cookies,
content_type=content_type
)
return render_template('code-page.html', code=code)
示例13: MockRestURI
# 需要导入模块: from requests.structures import CaseInsensitiveDict [as 别名]
# 或者: from requests.structures.CaseInsensitiveDict import get [as 别名]
class MockRestURI(object):
"""Representation of a mock URI."""
def __init__(self, uri_dict):
self._uri_dict = uri_dict
self.name = uri_dict.get('name', None)
self.fullpath = uri_dict['request']['path']
self._regexp = False
self.path = urlparse.urlparse(self.fullpath).path
self.querystring = urlparse.parse_qs(
urlparse.urlparse(self.fullpath).query,
keep_blank_values=True
)
self.method = uri_dict['request'].get('method', None)
self.headers = CaseInsensitiveDict(uri_dict['request'].get('headers', {}))
self.return_code = int(uri_dict['response'].get('code', '200'))
self.request_content_type = self.headers.get('Content-Type', '')
self.response_location = uri_dict['response'].get('location', None)
self.response_content = uri_dict['response'].get('content', "")
self.wait = float(uri_dict['response'].get('wait', 0.0))
self.request_data = uri_dict['request'].get('data', None)
self.encoding = uri_dict['request'].get("encoding", None)
self.response_headers = uri_dict['response'].get("headers", {})
def match(self, request):
"""Does this URI match the request?"""
# Match HTTP verb - GET, POST, PUT, DELETE, etc.
if self.method is not None:
if request.command.lower() != self.method.lower():
return False
# Match path
if self.path != urlparse.urlparse(request.path).path:
return False
# Match querystring
if request.querystring() != self.querystring:
return False
# Match headers
if self.headers is not None:
for header_var, header_value in self.headers.items():
request_headers = CaseInsensitiveDict(request.headers)
if request_headers.get(header_var) is None:
return False
req_maintext, req_pdict = cgi.parse_header(request_headers.get(header_var))
mock_maintext, mock_pdict = cgi.parse_header(header_value)
if "boundary" in req_pdict and "boundary" in mock_pdict:
req_pdict['boundary'] = "xxx"
mock_pdict['boundary'] = "xxx"
if req_maintext != mock_maintext:
return False
if mock_pdict != {}:
if req_pdict != mock_pdict:
return False
# Match processed request data
if self.request_data is not None:
# Check if exact match before parsing
if request.body != self.request_data:
if self.request_content_type.startswith("application/json"):
if json.loads(request.body) != json.loads(self.request_data):
return False
elif self.request_content_type.startswith("application/x-www-form-urlencoded"):
if parse_qsl_as_dict(request.body) != parse_qsl_as_dict(self.request_data):
return False
elif self.request_content_type.startswith('application/xml'):
actual_request_data_root = etree.fromstring(request.body)
mock_request_data_root = etree.fromstring(self.request_data)
if not xml_elements_equal(actual_request_data_root, mock_request_data_root):
return False
elif self.request_content_type.startswith("multipart/form-data"):
ctype, pdict = cgi.parse_header(request.headers.get('Content-Type'))
req_multipart = cgi.parse_multipart(
io.BytesIO(request.body.encode('utf8')),
{x: y.encode('utf8') for x, y in pdict.items()}
)
ctype, pdict = cgi.parse_header(self.headers.get('Content-Type'))
mock_multipart = cgi.parse_multipart(
io.BytesIO(self.request_data.encode('utf8')),
{x: y.encode('utf8') for x, y in pdict.items()}
)
return mock_multipart == req_multipart
else:
if request.body != self.request_data:
return False
return True
def querystring_string(self):
# TODO : Refactor this and docstring.
query = ''
for key in self.querystring.keys():
#.........这里部分代码省略.........
示例14: proxy
# 需要导入模块: from requests.structures import CaseInsensitiveDict [as 别名]
# 或者: from requests.structures.CaseInsensitiveDict import get [as 别名]
def proxy(identifier, in_method, in_headers, data):
# find endpoint
endpoint = pg.select1(
'endpoints',
what=['definition', 'method', 'pass_headers',
'headers', 'url', 'url_dynamic'],
where={'id': identifier}
)
if not endpoint:
return 'endpoint not found, create it at ' \
'<a href="/dashboard">dashboard</a>', 404, {}
event = {
'in': {
'time': time.time(),
'method': in_method,
'headers': dict(in_headers),
'body': data,
'replay': True
},
'out': {
'method': endpoint['method'],
'url': endpoint['url'],
'body': None,
'headers': {}
},
'response': {
'code': 0,
'body': ''
}
}
mutated, error = jq(endpoint['definition'], data=data)
if not mutated or error:
event['out']['error'] = error.decode('utf-8')
publish(identifier, event)
return 'transmutated into null and aborted', 201, {}
h = CaseInsensitiveDict({'Content-Type': 'application/json'})
if endpoint['pass_headers']:
h.update(in_headers)
h.update(endpoint['headers'])
event['out']['headers'] = dict(h)
# reformat the mutated data
mutatedjson = json.loads(mutated.decode('utf-8'))
if h.get('content-type') == 'application/x-www-form-urlencoded':
# oops, not json
mutated = urlencode(mutatedjson)
else:
mutated = json.dumps(mutatedjson)
event['out']['body'] = mutated
if endpoint['url_dynamic']:
urlb, error = jq(endpoint['url'], data=data)
print('URL', urlb, 'ERROR', error)
if not urlb:
event['out']['url_error'] = error.decode('utf-8')
publish(identifier, event)
return 'url building has failed', 200, {}
url = urlb.decode('utf-8')
event['out']['url'] = url
else:
url = endpoint['url']
# event['out'] is completed at this point
# and we all have everything needed to perform the request
if url and is_valid_url(url):
# we will only perform a request if there is an URL and it is valid
try:
s = requests.Session()
req = requests.Request(endpoint['method'], url,
data=mutated, headers=h).prepare()
resp = s.send(req, timeout=4)
if not resp.ok:
print('FAILED TO POST', resp.text, identifier, mutated)
except requests.exceptions.RequestException as e:
print(identifier, 'FAILED TO POST', mutated, 'TO URL', url)
print(e)
publish(identifier, event)
return "<request failed: '%s'>" % e, 503, {}
event['response']['code'] = resp.status_code
event['response']['body'] = resp.text
publish(identifier, event)
return resp.text, resp.status_code, dict(resp.headers)
else:
# no valid URL, just testing
publish(identifier, event)
return 'no URL to send this to', 201, {}
示例15: cache_response
# 需要导入模块: from requests.structures import CaseInsensitiveDict [as 别名]
# 或者: from requests.structures.CaseInsensitiveDict import get [as 别名]
def cache_response(self, request, response, body=None, status_codes=None):
"""
Algorithm for caching requests.
This assumes a requests Response object.
"""
# From httplib2: Don't cache 206's since we aren't going to
# handle byte range requests
cacheable_status_codes = status_codes or self.cacheable_status_codes
if response.status not in cacheable_status_codes:
logger.debug(
"Status code %s not in %s", response.status, cacheable_status_codes
)
return
response_headers = CaseInsensitiveDict(response.headers)
# If we've been given a body, our response has a Content-Length, that
# Content-Length is valid then we can check to see if the body we've
# been given matches the expected size, and if it doesn't we'll just
# skip trying to cache it.
if (
body is not None
and "content-length" in response_headers
and response_headers["content-length"].isdigit()
and int(response_headers["content-length"]) != len(body)
):
return
cc_req = self.parse_cache_control(request.headers)
cc = self.parse_cache_control(response_headers)
cache_url = self.cache_url(request.url)
logger.debug('Updating cache with response from "%s"', cache_url)
# Delete it from the cache if we happen to have it stored there
no_store = False
if "no-store" in cc:
no_store = True
logger.debug('Response header has "no-store"')
if "no-store" in cc_req:
no_store = True
logger.debug('Request header has "no-store"')
if no_store and self.cache.get(cache_url):
logger.debug('Purging existing cache entry to honor "no-store"')
self.cache.delete(cache_url)
if no_store:
return
# https://tools.ietf.org/html/rfc7234#section-4.1:
# A Vary header field-value of "*" always fails to match.
# Storing such a response leads to a deserialization warning
# during cache lookup and is not allowed to ever be served,
# so storing it can be avoided.
if "*" in response_headers.get("vary", ""):
logger.debug('Response header has "Vary: *"')
return
# If we've been given an etag, then keep the response
if self.cache_etags and "etag" in response_headers:
logger.debug("Caching due to etag")
self.cache.set(
cache_url, self.serializer.dumps(request, response, body=body)
)
# Add to the cache any 301s. We do this before looking that
# the Date headers.
elif response.status == 301:
logger.debug("Caching permanant redirect")
self.cache.set(cache_url, self.serializer.dumps(request, response))
# Add to the cache if the response headers demand it. If there
# is no date header then we can't do anything about expiring
# the cache.
elif "date" in response_headers:
# cache when there is a max-age > 0
if "max-age" in cc and cc["max-age"] > 0:
logger.debug("Caching b/c date exists and max-age > 0")
self.cache.set(
cache_url, self.serializer.dumps(request, response, body=body)
)
# If the request can expire, it means we should cache it
# in the meantime.
elif "expires" in response_headers:
if response_headers["expires"]:
logger.debug("Caching b/c of expires header")
self.cache.set(
cache_url, self.serializer.dumps(request, response, body=body)
)