本文整理汇总了Python中cgi.parse_header方法的典型用法代码示例。如果您正苦于以下问题:Python cgi.parse_header方法的具体用法?Python cgi.parse_header怎么用?Python cgi.parse_header使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cgi
的用法示例。
在下文中一共展示了cgi.parse_header方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_encoding_from_headers
# 需要导入模块: import cgi [as 别名]
# 或者: from cgi import parse_header [as 别名]
def get_encoding_from_headers(headers):
"""Returns encodings from given HTTP Header Dict.
:param headers: dictionary to extract encoding from.
"""
content_type = headers.get('content-type')
if not content_type:
return None
content_type, params = cgi.parse_header(content_type)
if 'charset' in params:
return params['charset'].strip("'\"")
if 'text' in content_type:
return 'ISO-8859-1'
示例2: get_encoding
# 需要导入模块: import cgi [as 别名]
# 或者: from cgi import parse_header [as 别名]
def get_encoding(headers, content):
"""Get encoding from request headers or page head."""
encoding = None
content_type = headers.get('content-type')
if content_type:
_, params = cgi.parse_header(content_type)
if 'charset' in params:
encoding = params['charset'].strip("'\"")
if not encoding:
content = utils.pretty_unicode(content[:1000]) if six.PY3 else content
charset_re = re.compile(r'<meta.*?charset=["\']*(.+?)["\'>]',
flags=re.I)
pragma_re = re.compile(r'<meta.*?content=["\']*;?charset=(.+?)["\'>]',
flags=re.I)
xml_re = re.compile(r'^<\?xml.*?encoding=["\']*(.+?)["\'>]')
encoding = (charset_re.findall(content) +
pragma_re.findall(content) +
xml_re.findall(content))
encoding = encoding and encoding[0] or None
return encoding
示例3: _parse_accept
# 需要导入模块: import cgi [as 别名]
# 或者: from cgi import parse_header [as 别名]
def _parse_accept(request):
"""Get the accept type for a given request.
Valid accept types are "application/json", "application/jsonlines", "application/x-recordio-protobuf",
and "text/csv". If no accept type is set, use the value in SAGEMAKER_DEFAULT_INVOCATIONS_ACCEPT.
:param request: flask request
:return: parsed accept type
"""
accept, _ = cgi.parse_header(request.headers.get("accept", ""))
if not accept or accept == "*/*":
return os.getenv(sm_env_constants.SAGEMAKER_DEFAULT_INVOCATIONS_ACCEPT, "text/csv")
if accept.lower() not in SUPPORTED_ACCEPTS:
raise ValueError("Accept type {} is not supported. Please use supported accept types: {}."
.format(accept, SUPPORTED_ACCEPTS))
return accept.lower()
示例4: get_encoding_from_headers
# 需要导入模块: import cgi [as 别名]
# 或者: from cgi import parse_header [as 别名]
def get_encoding_from_headers(headers):
"""Returns encodings from given HTTP Header Dict.
:param headers: dictionary to extract encoding from.
:rtype: str
"""
content_type = headers.get('content-type')
if not content_type:
return None
content_type, params = cgi.parse_header(content_type)
if 'charset' in params:
return params['charset'].strip("'\"")
if 'text' in content_type:
return 'ISO-8859-1'
示例5: __init__
# 需要导入模块: import cgi [as 别名]
# 或者: from cgi import parse_header [as 别名]
def __init__(self, content, url, headers=None):
# Determine if we have any encoding information in our headers
encoding = None
if headers and "Content-Type" in headers:
content_type, params = cgi.parse_header(headers["Content-Type"])
if "charset" in params:
encoding = params['charset']
self.content = content
self.parsed = html5lib.parse(
self.content,
transport_encoding=encoding,
namespaceHTMLElements=False,
)
self.url = url
self.headers = headers
示例6: test_success
# 需要导入模块: import cgi [as 别名]
# 或者: from cgi import parse_header [as 别名]
def test_success(self):
"""Basic dispatcher request flow."""
# Create upload.
upload_data = (
"""--================1234==
Content-Type: text/plain
MIME-Version: 1.0
Content-Disposition: form-data; name="field1"; filename="stuff.txt"
value
--================1234==--""")
upload_url = blobstore.create_upload_url('/success?foo=bar')
upload, forward_environ, _ = self._run_test_success(
upload_data, upload_url)
self.assertEquals('/success', forward_environ['PATH_INFO'])
self.assertEquals('foo=bar', forward_environ['QUERY_STRING'])
self.assertEquals(
('form-data', {'filename': 'stuff.txt', 'name': 'field1'}),
cgi.parse_header(upload['content-disposition']))
示例7: test_success_with_bucket
# 需要导入模块: import cgi [as 别名]
# 或者: from cgi import parse_header [as 别名]
def test_success_with_bucket(self):
"""Basic dispatcher request flow."""
# Create upload.
upload_data = (
"""--================1234==
Content-Type: text/plain
MIME-Version: 1.0
Content-Disposition: form-data; name="field1"; filename="stuff.txt"
value
--================1234==--""")
upload_url = blobstore.create_upload_url('/success?foo=bar',
gs_bucket_name='my_test_bucket')
upload, forward_environ, forward_body = self._run_test_success(
upload_data, upload_url)
self.assertEquals('/success', forward_environ['PATH_INFO'])
self.assertEquals('foo=bar', forward_environ['QUERY_STRING'])
self.assertEquals(
('form-data', {'filename': 'stuff.txt', 'name': 'field1'}),
cgi.parse_header(upload['content-disposition']))
self.assertIn('X-AppEngine-Cloud-Storage-Object: /gs/%s' % 'my_test_bucket',
forward_body)
示例8: test_success_full_success_url
# 需要导入模块: import cgi [as 别名]
# 或者: from cgi import parse_header [as 别名]
def test_success_full_success_url(self):
"""Request flow with a success url containing protocol, host and port."""
# Create upload.
upload_data = (
"""--================1234==
Content-Type: text/plain
MIME-Version: 1.0
Content-Disposition: form-data; name="field1"; filename="stuff.txt"
value
--================1234==--""")
# The scheme, host and port should all be ignored.
upload_url = blobstore.create_upload_url(
'https://example.com:1234/success?foo=bar')
upload, forward_environ, _ = self._run_test_success(
upload_data, upload_url)
self.assertEquals('/success', forward_environ['PATH_INFO'])
self.assertEquals('foo=bar', forward_environ['QUERY_STRING'])
self.assertEquals(
('form-data', {'filename': 'stuff.txt', 'name': 'field1'}),
cgi.parse_header(upload['content-disposition']))
示例9: __init__
# 需要导入模块: import cgi [as 别名]
# 或者: from cgi import parse_header [as 别名]
def __init__(self, content, url, headers=None):
# Determine if we have any encoding information in our headers
encoding = None
if headers and "Content-Type" in headers:
content_type, params = cgi.parse_header(headers["Content-Type"])
if "charset" in params:
encoding = params['charset']
self.content = content
self.parsed = html5lib.parse(
self.content,
encoding=encoding,
namespaceHTMLElements=False,
)
self.url = url
self.headers = headers
示例10: __init__
# 需要导入模块: import cgi [as 别名]
# 或者: from cgi import parse_header [as 别名]
def __init__(self, url, content, headers):
if not url.endswith("/"):
url += "/"
self._url = url
encoding = None
if headers and "Content-Type" in headers:
content_type, params = cgi.parse_header(headers["Content-Type"])
if "charset" in params:
encoding = params["charset"]
self._content = content
if encoding is None:
self._parsed = html5lib.parse(content, namespaceHTMLElements=False)
else:
self._parsed = html5lib.parse(
content, transport_encoding=encoding, namespaceHTMLElements=False
)