本文整理汇总了Python中quopri.encode函数的典型用法代码示例。如果您正苦于以下问题:Python encode函数的具体用法?Python encode怎么用?Python encode使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了encode函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_encode
def test_encode(self):
for p, e in self.STRINGS:
infp = cStringIO.StringIO(p)
outfp = cStringIO.StringIO()
if not test_support.due_to_ironpython_bug("http://tkbgitvstfat01:8080/WorkItemTracking/WorkItem.aspx?artifactMoniker=305368"):
quopri.encode(infp, outfp, quotetabs=False)
self.assertTrue(outfp.getvalue() == e)
示例2: __init__
def __init__(self, http_request, request_id):
HTTPMessage.__init__(self)
self.set_type('application/http-request')
self.add_header('Multipart-Request-ID', str(request_id))
self.add_header('Content-transfer-encoding', 'quoted-printable')
payload = StringIO()
quopri.encode(StringIO(http_request), payload, quotetabs=False)
self.set_payload(payload.getvalue())
payload.close()
示例3: __init__
def __init__(self, http_response, request_id):
HTTPMessage.__init__(self)
self.set_type("application/http-response")
self.add_header("Multipart-Request-ID", str(request_id))
self.add_header("Content-transfer-encoding", "quoted-printable")
payload = StringIO()
quopri.encode(StringIO(http_response), payload, quotetabs=False)
self.set_payload(payload.getvalue())
payload.close()
示例4: send_mail
def send_mail(send_from, send_to, subject, text, files=[], server="localhost", user=None, password=None):
assert type(send_to)==list
assert type(files)==list
msg = MIMEMultipart()
msg['From'] = send_from
msg['To'] = COMMASPACE.join(send_to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach( MIMEText(text, _charset='utf-8') )
for fileName in files:
contentType,ignored=mimetypes.guess_type(fileName)
if contentType==None: # If no guess, use generic opaque type
contentType="application/octet-stream"
contentsEncoded=cStringIO.StringIO()
f=open(fileName,"rb")
mainType=contentType[:contentType.find("/")]
if mainType=="text":
cte="quoted-printable"
quopri.encode(f,contentsEncoded,1) # 1 for encode tabs
else:
cte="base64"
base64.encode(f,contentsEncoded)
f.close()
subMsg=email.Message.Message()
subMsg.add_header("Content-type",contentType,name=flatten_name(fileName))
subMsg.add_header("Content-transfer-encoding",cte)
subMsg.set_payload(contentsEncoded.getvalue())
contentsEncoded.close()
msg.attach(subMsg)
# part = MIMEBase('application', "octet-stream")
# part.set_payload( open(file,"rb").read() )
# Encoders.encode_base64(part)
# part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
# msg.attach(part)
# print server
smtp = smtplib.SMTP(server, 587)
# smtp.set_debuglevel(1)
smtp.ehlo()
settings = HandshakeSettings()
smtp.starttls()
if (user != None and password != None):
smtp.login(user, password)
print "Sending mail to " + msg['To']
smtp.sendmail(send_from, send_to, msg.as_string())
print "mail sent."
smtp.close()
print "done"
示例5: _qencode
def _qencode(s):
if not s:
return s
hasnewline = (s[-1] == '\n')
infp = StringIO(s)
outfp = StringIO()
_quopri.encode(infp, outfp, quotetabs=1)
# Python 2.x's encode() doesn't encode spaces even when quotetabs==1
value = outfp.getvalue().replace(' ', '=20')
if not hasnewline and value[-1] == '\n':
return value[:-1]
return value
示例6: quopri_encode
def quopri_encode(input, errors='strict'):
"""Encode the input, returning a tuple (output object, length consumed).
errors defines the error handling to apply. It defaults to
'strict' handling which is the only currently supported
error handling for this codec.
"""
assert errors == 'strict'
f = StringIO(input)
g = StringIO()
quopri.encode(f, g, 1)
output = g.getvalue()
return (output, len(input))
示例7: quopri_encode
def quopri_encode(input, errors='strict'):
"""Encode the input, returning a tuple (output object, length consumed).
errors defines the error handling to apply. It defaults to
'strict' handling which is the only currently supported
error handling for this codec.
"""
assert errors == 'strict'
# using str() because of cStringIO's Unicode undesired Unicode behavior.
f = StringIO(str(input))
g = StringIO()
quopri.encode(f, g, quotetabs=True)
output = g.getvalue()
return (output, len(input))
示例8: standard_message
def standard_message(self, to, subject, content, author=None):
"""Send a standard message.
Arguments:
- to: a list of addresses usable by rfc822.parseaddr().
- subject: the subject as a string.
- content: the body of the message as a string.
- author: the sender as a (name, address) tuple
"""
message, writer = self.get_standard_message(to, subject, author)
writer.addheader("Content-Transfer-Encoding", "quoted-printable")
body = writer.startbody("text/plain; charset=utf-8")
content = StringIO(content)
quopri.encode(content, body, 0)
self.smtp_send(to, message)
示例9: main
def main():
mainMsg=email.Message.Message()
mainMsg["To"]=toAddr
mainMsg["From"]=fromAddr
mainMsg["Subject"]="Directory contents"
mainMsg["Date"]=email.Utils.formatdate(localtime=1)
mainMsg["Message-ID"]=email.Utils.make_msgid()
mainMsg["Mime-version"]="1.0"
mainMsg["Content-type"]="Multipart/mixed"
mainMsg.preamble="Mime message\n"
mainMsg.epilogue="" # To ensure that message ends with newline
firstSubMsg=email.Message.Message()
firstSubMsg["Content-type"]="text/plain"
firstSubMsg["Content-transfer-encoding"]="7bit"
firstSubMsg.set_payload("Files from directory\n")
mainMsg.attach(firstSubMsg)
# Get names of plain files
fileNames=[f for f in os.listdir(os.curdir) if os.path.isfile(f)]
for fileName in fileNames:
contentType,ignored=mimetypes.guess_type(fileName)
if contentType==None: # If no guess, use generic opaque type
contentType="application/octet-stream"
contentsEncoded=cStringIO.StringIO()
f=open(fileName,"rb")
mainType=contentType[:contentType.find("/")]
if mainType=="text":
cte="quoted-printable"
quopri.encode(f,contentsEncoded,1) # 1 for encode tabs
else:
cte="base64"
base64.encode(f,contentsEncoded)
f.close()
subMsg=email.Message.Message()
subMsg.add_header("Content-type",contentType,name=fileName)
subMsg.add_header("Content-transfer-encoding",cte)
subMsg.set_payload(contentsEncoded.getvalue())
contentsEncoded.close()
mainMsg.attach(subMsg)
f=open(outputFile,"wb")
f.write(mainMsg.as_string())
f.close()
return None
示例10: send
def send(self, report_file, from_addr, to_addrs):
if self.init == False:
print("Instance not properly instanitiated")
return -1
self.msgs = [email.Message.Message() for to in to_addrs]
for msg , to in zip(self.msgs, to_addrs):
msg["From"] = from_addr
msg["To"] = to
msg["Subject"] = "Auto-Test Result Reporting"
msg["MIME-Version"] = "1.0"
msg["Content-Type"] = "Multipart/mixed"
msg.preamble = "MIME Message"
msg.epilogue = ""
content_type, ignored = mimetypes.guess_type(report_file)
if content_type == None:
content_type = "Application/octet-stream"
content_encoded = cStringIO.StringIO()
with open(report_file, "rb") as f:
main_type = content_type[:content_type.find("/")]
if main_type == "text":
cte = "quoted-printable"
quopri.encode(f, content_encoded, 1)
else:
cte = "base64"
base64.encode(f, content_encoded)
f.close()
sub_msg = email.Message.Message()
sub_msg.add_header("Content-Type", content_type, name = report_file)
sub_msg.add_header("Content-Transfer-Encoding", cte)
sub_msg.set_payload(content_encoded.getvalue())
content_encoded.close()
for msg, to in zip(self.msgs, to_addrs):
msg.attach(sub_msg)
f = StringIO.StringIO()
g = email.Generator.Generator(f)
g.flatten(msg)
try:
self.smtp.sendmail(from_addr, to, f.getvalue())
except Exception:
print("send to %s failed" % to)
f.close()
示例11: emailTurnin
def emailTurnin(self):
#Get email information
try:
addr = JESAddressFinder.JESAddressFinder()
taEmail = addr.getTargetAddress(self.gtNumber, self.hwTitle)
if(taEmail == None):
raise StandardError, "Could not find an e-mail to send assignment."
return
filehandle = open(self.zipFile,"rb")
#CONSTRUCT EMAIL
#Build the email from all the parts of information:
subject = "%s : %s : %s : %s" % \
(self.hwTitle, self.studentName, self.gtNumber, self.fileName)
msgBody = 'From: %s\n' % self.studentEmail
msgBody += 'Subject: %s\n' % subject
file = StringIO.StringIO()
mime = MimeWriter.MimeWriter(file)
mime.addheader("Mime-Version","1.0")
mime.startmultipartbody("mixed")
part=mime.nextpart()
part.addheader("Content-Transfer-Encoding","quoted-printable")
part.startbody("text/plain")
quopri.encode(StringIO.StringIO("An Assignment Submission from "+self.studentName),file,0)
quopri.encode(StringIO.StringIO("Notes to TA: "),file,0)
#quopri.encode(StringIO.StringIO(notes),file,0)
part = mime.nextpart()
part.addheader("Content-Transfer-Encoding","base64")
part.startbody('application/x-zip-compressed; name='+self.zipFile)
base64.encode(filehandle, file)
mime.lastpart()
msgBody += file.getvalue()
filehandle.close()
#END CONSTRUCT EMAIL
#SEND EMAIL:
servObj = smtplib.SMTP(self.mailServer)
servObj.sendmail(self.studentEmail, taEmail, msgBody)
except:
print "Student E-mail: " + self.studentEmail + "\n"
import sys
a,b,c=sys.exc_info()
print a,b,c
raise StandardError, "Error emailing assignment."
示例12: main
def main():
main_msg = email.Message.Message()
main_msg["To"] = T_ADDR
main_msg["From"] = F_ADDR
main_msg["Subject"] = "Directory contents"
main_msg["Mime-version"] = "1.0"
main_msg["Content-type"] = "Multipart/mixed"
main_msg.preamble = "Mime message\n"
main_msg.epilogue = ""
file_names = [f for f in os.listdir(os.curdir) if os.path.isfile(f)]
for file_name in file_names:
content_type,ignored = mimetypes.guess_type(file_name)
if content_type is None:
content_type = "application/octet-stream"
contents_encoded = cStringIO.StringIO()
with open(file_name,"rb") as f:
main_type = content_type[:content_type.find("/")]
if main_type == "text":
cte = "quoted-printable"
quopri.encode(f,contents_encoded,True)
else:
cte = "base64"
base64.encode(f,contents_encoded)
sub_msg = email.Message.Message()
sub_msg.add_header("Content-type",content_type,name=file_name)
sub_msg.add_header("Content-transfer-encoding",cte)
sub_msg.set_payload(contents_encoded.getvalue())
contents_encoded.close()
main_msg.attach(sub_msg)
body_content = email.Message.Message()
body_content.set_type("text/plain")
body_content.set_payload("hello word")
main_msg.attach(body_content)
f = open(output_file,"wb")
g = email.Generator.Generator(f)
g.flatten(main_msg)
f.close()
return None
示例13: write
def write(self, data, mimetype="text/plain"):
"Write data from string or file to message"
# data is either an opened file or a string
if type(data) is type(""):
file = StringIO.StringIO(data)
else:
file = data
data = None
part = self.mime.nextpart()
typ, subtyp = string.split(mimetype, "/", 1)
if typ == "text":
# text data
encoding = "quoted-printable"
encoder = lambda i, o: quopri.encode(i, o, 0)
if data and not must_quote(data):
# copy, don't encode
encoding = "7bit"
encoder = None
else:
# binary data (image, audio, application, ...)
encoding = "base64"
encoder = base64.encode
#
# write part headers
if encoding:
part.addheader("Content-Transfer-Encoding", encoding)
part.startbody(mimetype)
#
# write part body
if encoder:
encoder(file, self.file)
elif data:
self.file.write(data)
else:
while 1:
data = infile.read(16384)
if not data:
break
outfile.write(data)
示例14: attach
def attach(self, chemin_fichier):
raise NotImplementedError
fileName = chemin_fichier.split( '/' )[-1]
contentType,ignored=mimetypes.guess_type(fileName)
if contentType==None: # If no guess, use generic opaque type
contentType="application/octet-stream"
contentsEncoded=cStringIO.StringIO()
f=open(chemin_fichier,"rb")
mainType=contentType[:contentType.find("/")]
if mainType=="text":
cte="quoted-printable"
quopri.encode(f,contentsEncoded,1) # 1 for encode tabs
else:
cte="base64"
base64.encode(f,contentsEncoded)
f.close()
subMsg=email.Message.Message()
subMsg.add_header("Content-type",contentType,name=fileName)
subMsg.add_header("Content-transfer-encoding",cte)
subMsg.set_payload(contentsEncoded.getvalue())
contentsEncoded.close()
self.__email.attach( subMsg )
示例15: encode
def encode(input, output, encoding):
if encoding == 'base64':
import base64
return base64.encode(input, output)
if encoding == 'quoted-printable':
import quopri
return quopri.encode(input, output, 0)
if encoding in ('uuencode', 'x-uuencode', 'uue', 'x-uue'):
import uu
return uu.encode(input, output)
if encodetab.has_key(encoding):
pipethrough(input, encodetab[encoding], output)
else:
raise ValueError, \
'unknown Content-Transfer-Encoding: %s' % encoding