本文整理汇总了Python中email.errors方法的典型用法代码示例。如果您正苦于以下问题:Python email.errors方法的具体用法?Python email.errors怎么用?Python email.errors使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类email
的用法示例。
在下文中一共展示了email.errors方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _get_version_from_pkg_metadata
# 需要导入模块: import email [as 别名]
# 或者: from email import errors [as 别名]
def _get_version_from_pkg_metadata(package_name):
"""Get the version from package metadata if present.
This looks for PKG-INFO if present (for sdists), and if not looks
for METADATA (for wheels) and failing that will return None.
"""
pkg_metadata_filenames = ['PKG-INFO', 'METADATA']
pkg_metadata = {}
for filename in pkg_metadata_filenames:
try:
pkg_metadata_file = open(filename, 'r')
except (IOError, OSError):
continue
try:
pkg_metadata = email.message_from_file(pkg_metadata_file)
except email.errors.MessageError:
continue
# Check to make sure we're in our own dir
if pkg_metadata.get('Name', None) != package_name:
return None
return pkg_metadata.get('Version', None)
示例2: _http_request_requests
# 需要导入模块: import email [as 别名]
# 或者: from email import errors [as 别名]
def _http_request_requests(self, url, headers, data, params):
kwargs = {
'verify': self.verify_cert,
}
if url is not None:
kwargs['url'] = url
if headers is not None:
kwargs['headers'] = headers
if data is not None:
kwargs['data'] = data
if params is not None:
kwargs['params'] = params
if self.timeout is not None:
kwargs['timeout'] = self.timeout
try:
if data is None:
r = requests.get(**kwargs)
else:
r = requests.post(**kwargs)
except requests.exceptions.RequestException as e:
raise PanHttpError('RequestException: ' + str(e))
self.code = r.status_code
self.reason = r.reason
x = ['%s: %s' % (k, v) for k, v in r.headers.items()]
try:
self.headers = email.message_from_string('\n'.join(x))
except (TypeError, email.errors.MessageError) as e:
raise PanHttpError('email.message_from_string() %s' % e)
self.encoding = self.headers.get_content_charset('utf8')
self.content_type = self.headers.get_content_type()
self.content = r.content
self.text = r.text
# allow non-2XX error codes
# see http://bugs.python.org/issue18543 for why we can't just
# install a new HTTPErrorProcessor()
示例3: test_email_parsing_errors_are_handled
# 需要导入模块: import email [as 别名]
# 或者: from email import errors [as 别名]
def test_email_parsing_errors_are_handled(self):
mocked_open = mock.mock_open()
with mock.patch('pbr.packaging.open', mocked_open):
with mock.patch('email.message_from_file') as message_from_file:
message_from_file.side_effect = [
email.errors.MessageError('Test'),
{'Name': 'pbr_testpackage'}]
version = packaging._get_version_from_pkg_metadata(
'pbr_testpackage')
self.assertTrue(message_from_file.called)
self.assertIsNone(version)
示例4: get_text_content
# 需要导入模块: import email [as 别名]
# 或者: from email import errors [as 别名]
def get_text_content(msg, errors='replace'):
content = msg.get_payload(decode=True)
charset = msg.get_param('charset', 'ASCII')
return content.decode(charset, errors=errors)
示例5: _prepare_set
# 需要导入模块: import email [as 别名]
# 或者: from email import errors [as 别名]
def _prepare_set(msg, maintype, subtype, headers):
msg['Content-Type'] = '/'.join((maintype, subtype))
if headers:
if not hasattr(headers[0], 'name'):
mp = msg.policy
headers = [mp.header_factory(*mp.header_source_parse([header]))
for header in headers]
try:
for header in headers:
if header.defects:
raise header.defects[0]
msg[header.name] = header
except email.errors.HeaderDefect as exc:
raise ValueError("Invalid header: {}".format(
header.fold(policy=msg.policy))) from exc
示例6: _iter_parts
# 需要导入模块: import email [as 别名]
# 或者: from email import errors [as 别名]
def _iter_parts(msg):
"""Extract and decode the text parts from the parsed email message.
For non-text parts the payload will be None.
Yields (payload, part)
"""
for part in msg.walk():
if part.get_content_maintype() == "text":
payload = part.get_payload(decode=True)
charset = part.get_content_charset()
errors = "ignore"
if not charset:
charset = "ascii"
elif charset.lower().replace("_", "-") in STRICT_CHARSETS:
errors = "strict"
try:
payload = payload.decode(charset, errors)
except (LookupError, UnicodeError, AssertionError):
try:
payload = payload.decode("ascii", "ignore")
except UnicodeError:
continue
yield payload, part
else:
yield None, part
示例7: _http_request_urllib
# 需要导入模块: import email [as 别名]
# 或者: from email import errors [as 别名]
def _http_request_urllib(self, url, headers, data, params):
if params is not None:
url += '?' + urlencode(params)
kwargs = {}
if url is not None:
kwargs['url'] = url
if headers is not None:
kwargs['headers'] = headers
if data is not None:
kwargs['data'] = data
if headers is not None:
x = set(k.lower() for k in headers)
if not 'content-type' in x:
kwargs['data'] = urlencode(data)
# data must by type 'bytes' for 3.x
if _isunicode(kwargs['data']):
kwargs['data'] = kwargs['data'].encode()
request = Request(**kwargs)
kwargs = {
'url': request,
}
if self.timeout is not None:
kwargs['timeout'] = self.timeout
if not self.verify_cert and \
(sys.version_info.major == 2 and
sys.hexversion >= 0x02070900 or
sys.version_info.major == 3 and
sys.hexversion >= 0x03040300):
context = ssl._create_unverified_context()
kwargs['context'] = context
try:
response = self._urlopen(**kwargs)
except (URLError, IOError, ssl.CertificateError) as e:
# IOError for urllib timeouts
# ssl.CertificateError for mismatching hostname
raise PanHttpError(str(e))
self.code = response.getcode()
if hasattr(response, 'reason'):
# 3.2
self.reason = response.reason
elif hasattr(response, 'msg'):
# 2.7
self.reason = response.msg
try:
self.headers = email.message_from_string(str(response.info()))
except (TypeError, email.errors.MessageError) as e:
raise PanHttpError('email.message_from_string() %s' % e)
self.encoding = self.headers.get_content_charset('utf8')
self.content_type = self.headers.get_content_type()
self.content = response.read()
self.text = self.content.decode(self.encoding)