本文整理汇总了Python中email.parser.HeaderParser方法的典型用法代码示例。如果您正苦于以下问题:Python parser.HeaderParser方法的具体用法?Python parser.HeaderParser怎么用?Python parser.HeaderParser使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类email.parser
的用法示例。
在下文中一共展示了parser.HeaderParser方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: parse_docstring
# 需要导入模块: from email import parser [as 别名]
# 或者: from email.parser import HeaderParser [as 别名]
def parse_docstring(docstring):
"""
Parse out the parts of a docstring. Return (title, body, metadata).
"""
docstring = trim_docstring(docstring)
parts = re.split(r'\n{2,}', docstring)
title = parts[0]
if len(parts) == 1:
body = ''
metadata = {}
else:
parser = HeaderParser()
try:
metadata = parser.parsestr(parts[-1])
except HeaderParseError:
metadata = {}
body = "\n\n".join(parts[1:])
else:
metadata = dict(metadata.items())
if metadata:
body = "\n\n".join(parts[1:-1])
else:
body = "\n\n".join(parts[1:])
return title, body, metadata
示例2: run_post_send
# 需要导入模块: from email import parser [as 别名]
# 或者: from email.parser import HeaderParser [as 别名]
def run_post_send(message):
""" Execute command after successful send, can be used to notify successful sends """
command = message.partner.cmd_send
if command:
models.Log.objects.create(message=message, status='S', text=_(u'Execute command post successful send'))
# Create command template and replace variables in the command
command = Template(command)
variables = {
'filename': message.payload.name,
'sender': message.organization.as2_name,
'recevier': message.partner.as2_name,
'messageid': message.message_id
}
variables.update(dict(HeaderParser().parsestr(message.headers).items()))
# Execute the command
os.system(command.safe_substitute(variables))
示例3: run_post_receive
# 需要导入模块: from email import parser [as 别名]
# 或者: from email.parser import HeaderParser [as 别名]
def run_post_receive(message, full_filename):
""" Execute command after successful receive, can be used to call the edi program for further processing"""
command = message.partner.cmd_receive
if command:
models.Log.objects.create(message=message, status='S', text=_(u'Execute command post successful receive'))
# Create command template and replace variables in the command
command = Template(command)
variables = {
'filename': message.payload.name,
'fullfilename': full_filename,
'sender': message.organization.as2_name,
'recevier': message.partner.as2_name,
'messageid': message.message_id
}
variables.update(dict(HeaderParser().parsestr(message.headers).items()))
# Execute the command
os.system(command.safe_substitute(variables))
示例4: get
# 需要导入模块: from email import parser [as 别名]
# 或者: from email.parser import HeaderParser [as 别名]
def get(self, request, pk, *args, **kwargs):
try:
mdn = models.MDN.objects.get(message_id=pk)
if request.GET['action'] == 'downl':
response = HttpResponse(content_type='multipart/report')
disposition_type = 'attachment'
response['Content-Disposition'] = disposition_type + '; filename=' + pk + '.mdn'
response.write(as2utils.readdata(mdn.file))
return response
elif request.GET['action'] == 'this':
file_obj = dict()
file_obj['name'] = pk + '.mdn'
file_obj['id'] = pk
file_obj['content'] = as2utils.readdata(mdn.file, charset='utf-8', errors='ignore')
file_obj['direction'] = mdn.get_status_display()
file_obj['type'] = 'AS2 MDN'
file_obj['headers'] = dict(HeaderParser().parsestr(mdn.headers or '').items())
return render(request, self.template_name, {'file_obj': file_obj})
except Exception:
return render(request, self.template_name, {'error_content': _(u'No such file.')})
示例5: send_async_mdn
# 需要导入模块: from email import parser [as 别名]
# 或者: from email.parser import HeaderParser [as 别名]
def send_async_mdn(self):
""" Send the asynchronous MDN to the partner"""
# convert the mdn headers to dictionary
headers = HeaderParser().parsestr(self.headers.read().decode())
# Send the mdn to the partner
try:
response = requests.post(
self.return_url, headers=dict(headers.items()), data=self.payload.read()
)
response.raise_for_status()
except requests.exceptions.RequestException:
return
# Update the status of the MDN
self.status = "S"
self.save()
示例6: find_by_header
# 需要导入模块: from email import parser [as 别名]
# 或者: from email.parser import HeaderParser [as 别名]
def find_by_header(self, header_name, header_value):
"""Find all uids in the selected folder with the given header value."""
all_uids = self.all_uids()
# It would be nice to just search by header too, but some backends
# don't support that, at least not if you want to search by X-INBOX-ID
# header. So fetch the header for each draft and see if we
# can find one that matches.
# TODO(emfree): are there other ways we can narrow the result set a
# priori (by subject or date, etc.)
matching_draft_headers = self.fetch_headers(all_uids)
results = []
for uid, response in matching_draft_headers.iteritems():
headers = response['BODY[HEADER]']
parser = HeaderParser()
header = parser.parsestr(headers).get(header_name)
if header == header_value:
results.append(uid)
return results
示例7: test_header_parser
# 需要导入模块: from email import parser [as 别名]
# 或者: from email.parser import HeaderParser [as 别名]
def test_header_parser(self):
eq = self.assertEqual
# Parse only the headers of a complex multipart MIME document
fp = openfile('msg_02.txt')
try:
msg = HeaderParser().parse(fp)
finally:
fp.close()
eq(msg['from'], 'ppp-request@zzz.org')
eq(msg['to'], 'ppp@zzz.org')
eq(msg.get_content_type(), 'multipart/mixed')
self.assertFalse(msg.is_multipart())
self.assertIsInstance(msg.get_payload(), str)
示例8: test_header_parser
# 需要导入模块: from email import parser [as 别名]
# 或者: from email.parser import HeaderParser [as 别名]
def test_header_parser(self):
eq = self.assertEqual
# Parse only the headers of a complex multipart MIME document
fp = openfile('msg_02.txt')
try:
msg = HeaderParser().parse(fp)
finally:
fp.close()
eq(msg['from'], 'ppp-request@zzz.org')
eq(msg['to'], 'ppp@zzz.org')
eq(msg.get_content_type(), 'multipart/mixed')
self.assertFalse(msg.is_multipart())
self.assertTrue(isinstance(msg.get_payload(), str))
示例9: handle
# 需要导入模块: from email import parser [as 别名]
# 或者: from email.parser import HeaderParser [as 别名]
def handle(self, *args, **options):
# First part of script sends asynchronous MDNs for inbound messages received from partners
# Fetch all the pending asynchronous MDN objects
pyas2init.logger.info(_(u'Sending all pending asynchronous MDNs'))
in_pending_mdns = models.MDN.objects.filter(status='P') # , timestamp__gt=time_threshold) --> why do this?
for pending_mdn in in_pending_mdns:
# Parse the MDN headers from text
header_parser = HeaderParser()
mdn_headers = header_parser.parsestr(pending_mdn.headers)
try:
# Set http basic auth if enabled in the partner profile
auth = None
if pending_mdn.omessage.partner and pending_mdn.omessage.partner.http_auth:
auth = (pending_mdn.omessage.partner.http_auth_user, pending_mdn.omessage.partner.http_auth_pass)
# Set the ca cert if given in the partner profile
verify = True
if pending_mdn.omessage.partner.https_ca_cert:
verify = pending_mdn.omessage.partner.https_ca_cert.path
# Post the MDN message to the url provided on the original as2 message
with open(pending_mdn.file, 'rb') as payload:
requests.post(pending_mdn.return_url,
auth=auth,
verify=verify,
headers=dict(mdn_headers.items()),
data=payload)
pending_mdn.status = 'S'
models.Log.objects.create(message=pending_mdn.omessage,
status='S',
text=_(u'Successfully sent asynchronous mdn to partner'))
except Exception, e:
models.Log.objects.create(message=pending_mdn.omessage,
status='E',
text=_(
u'Failed to send asynchronous mdn to partner, error is {0:s}'.format(e)))
finally:
示例10: setUpTestData
# 需要导入模块: from email import parser [as 别名]
# 或者: from email.parser import HeaderParser [as 别名]
def setUpTestData(cls):
# Every test needs a client.
cls.client = Client()
cls.header_parser = HeaderParser()
# Load the client and server certificates
cls.server_key = models.PrivateCertificate.objects.create(
certificate=os.path.join(TEST_DIR, 'as2server.pem'),
certificate_passphrase='password'
)
cls.si_public_key = models.PublicCertificate.objects.create(
certificate=os.path.join(TEST_DIR, 'si_public_key.crt'),
ca_cert=os.path.join(TEST_DIR, 'si_public_key.ca'),
verify_cert=False
)
# Setup the server organization and partner
cls.organization = models.Organization.objects.create(
name='Server Organization',
as2_name='as2server',
encryption_key=cls.server_key,
signature_key=cls.server_key
)
cls.partner = models.Partner.objects.create(
name='Sterling B2B Integrator',
as2_name='SIAS2PRD',
target_url='http://localhost:8080/pyas2/as2receive',
compress=False,
mdn=False,
signature_key=cls.si_public_key,
encryption_key=cls.si_public_key
)
# Initialise the payload i.e. the file to be transmitted
cls.payload = models.Payload.objects.create(
name='testmessage.edi',
file=os.path.join(TEST_DIR, 'testmessage.edi'),
content_type='application/edi-consent'
)
示例11: test_message_rfc822_only
# 需要导入模块: from email import parser [as 别名]
# 或者: from email.parser import HeaderParser [as 别名]
def test_message_rfc822_only(self):
# Issue 7970: message/rfc822 not in multipart parsed by
# HeaderParser caused an exception when flattened.
with openfile('msg_46.txt') as fp:
msgdata = fp.read()
parser = HeaderParser()
msg = parser.parsestr(msgdata)
out = StringIO()
gen = Generator(out, True, 0)
gen.flatten(msg, False)
self.assertEqual(out.getvalue(), msgdata)
示例12: test_header_parser
# 需要导入模块: from email import parser [as 别名]
# 或者: from email.parser import HeaderParser [as 别名]
def test_header_parser(self):
eq = self.assertEqual
# Parse only the headers of a complex multipart MIME document
with openfile('msg_02.txt') as fp:
msg = HeaderParser().parse(fp)
eq(msg['from'], 'ppp-request@zzz.org')
eq(msg['to'], 'ppp@zzz.org')
eq(msg.get_content_type(), 'multipart/mixed')
self.assertFalse(msg.is_multipart())
self.assertIsInstance(msg.get_payload(), str)
示例13: each
# 需要导入模块: from email import parser [as 别名]
# 或者: from email.parser import HeaderParser [as 别名]
def each(self, target):
self.results = {}
header_raw = open(target, 'r').read()
header = HeaderParser()
parsed_headers = header.parsestr(header_raw)
# Get Useful Headers
self.results['From'] = decode_mime_words(parsed_headers['From'])
self.results['ReturnPath'] = decode_mime_words(parsed_headers['Return-Path'])
self.results['ReplyTo'] = decode_mime_words(parsed_headers['Reply-To'])
self.results['To'] = decode_mime_words(parsed_headers['To'])
self.results['Subject'] = decode_mime_words(parsed_headers['Subject'])
self.results['Date'] = parsed_headers['Date']
self.results['Cc'] = decode_mime_words(parsed_headers['Cc'])
# Parse Received and Authentication Headers
self.results['Received'] = self.parse_received(parsed_headers.get_all('Received'))
self.results['DKIM'] = self.parse_dkim(parsed_headers.items())
self.results['SPF'] = self.parse_spf(parsed_headers.items())
self.results['DMARC'] = self.parse_dmarc(parsed_headers.items())
self.results['headers'] = parsed_headers.items()
self.results['highlight'] = self.highlight
return True
示例14: test_header_parser
# 需要导入模块: from email import parser [as 别名]
# 或者: from email.parser import HeaderParser [as 别名]
def test_header_parser(self):
eq = self.assertEqual
# Parse only the headers of a complex multipart MIME document
fp = openfile('msg_02.txt')
try:
msg = HeaderParser().parse(fp)
finally:
fp.close()
eq(msg['from'], 'ppp-request@zzz.org')
eq(msg['to'], 'ppp@zzz.org')
eq(msg.get_content_type(), 'multipart/mixed')
self.failIf(msg.is_multipart())
self.failUnless(isinstance(msg.get_payload(), str))
示例15: setUpTestData
# 需要导入模块: from email import parser [as 别名]
# 或者: from email.parser import HeaderParser [as 别名]
def setUpTestData(cls):
# Every test needs a client.
cls.client = Client()
cls.header_parser = HeaderParser()
# Load the client and server certificates
with open(os.path.join(TEST_DIR, "server_private.pem"), "rb") as fp:
cls.server_key = PrivateKey.objects.create(key=fp.read(), key_pass="test")
with open(os.path.join(TEST_DIR, "server_public.pem"), "rb") as fp:
cls.server_crt = PublicCertificate.objects.create(certificate=fp.read())
with open(os.path.join(TEST_DIR, "client_private.pem"), "rb") as fp:
cls.client_key = PrivateKey.objects.create(key=fp.read(), key_pass="test")
with open(os.path.join(TEST_DIR, "client_public.pem"), "rb") as fp:
cls.client_crt = PublicCertificate.objects.create(certificate=fp.read())
# Setup the server organization and partner
Organization.objects.create(
name="AS2 Server",
as2_name="as2server",
encryption_key=cls.server_key,
signature_key=cls.server_key,
)
Partner.objects.create(
name="AS2 Client",
as2_name="as2client",
target_url="http://localhost:8080/pyas2/as2receive",
compress=False,
mdn=False,
signature_cert=cls.client_crt,
encryption_cert=cls.client_crt,
)
# Setup the client organization and partner
cls.organization = Organization.objects.create(
name="AS2 Client",
as2_name="as2client",
encryption_key=cls.client_key,
signature_key=cls.client_key,
)
# Initialise the payload i.e. the file to be transmitted
with open(os.path.join(TEST_DIR, "testmessage.edi"), "rb") as fp:
cls.payload = fp.read()