python標準庫已包含對http的支持,通過很簡單的辦法就可以直接使用http代理服務器獲取網頁數據:
a = POP3("10.3.4.3","3128")
print ="this is a test"
self.host = "10.3.4.3"
self.port = "3128"
msg = "getaddrinfo returns an empty list"
self.sock = None
for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM):
af, socktype, proto, canonname, sa = res
try:
self.sock = socket.socket(af, socktype, proto)
self.sock.connect(sa)
except socket.error, msg:
if self.sock:
self.sock.close()
self.sock = None
continue
break
if not self.sock:
raise socket.error, msg
self.file = self.sock.makefile('rb')
self._debugging = 0
self._putline("CONNECT 220.181.15.121:110 HTTP/1.0/r/n") #pop.126.com的ip地址
msg_proxy = self._getline()
msg_proxy = self._getline()
self.welcome = self._getresp()
import smtplib
addr_from = "測試郵件發送地址"<[email protected]>"
smtp = "smtp.gamil.com"
head_format = """To: %s/nFrom: %s/nContent-Type: text/plain;/ncharset="gb2312"/nSubject: Test mail from python/n/n"""
server = smtplib.SMTP('smtp.changhong.com')
server.login("name","password")
msg = head + body
server.sendmail(self.addr_from,addr_to ,msg)
class smtp_server:
server = None
subject = "Python mail sender"
addr_from = """PythonMail<[email protected]>"""
addr_to = """PythonMail<[email protected]>"""
charset = 'GB2312'
def __init__(self):
import smtplib
self.server = smtplib.SMTP("smtp.126.com")
self.server.login("user_name","mypass")
return
def __del__(self):
if(self.server != ""):
self.server.quit()
return
def send(self, addr_from , addr_to ,msg):
self.server.sendmail(addr_from , addr_to , msg)
return
def send_html(self, addr_from , addr_to ,html , subject):
msg = self.create_html_mail(html,None,subject,addr_from,addr_to)
self.send(addr_from,addr_to,msg)
return
def create_html_mail(self,html, text=None ,subject=None, addr_from=None , addr_to=None):
"Create a mime-message that will render as HTML or text, as appropriate"
import MimeWriter
import mimetools
import cStringIO
import base64
charset = self.charset
if subject is None:
subject=self.subject
if addr_from is None:
addr_from=self.addr_from
if addr_to is None:
addr_to=self.addr_to
if text is None:
# Produce an approximate textual rendering of the HTML string,
# unless you have been given a better version as an argument
import htmllib, formatter
textout = cStringIO.StringIO( )
formtext = formatter.AbstractFormatter(formatter.DumbWriter(textout))
parser = htmllib.HTMLParser(formtext)
parser.feed(html)
parser.close( )
text = textout.getvalue( )
del textout, formtext, parser
out = cStringIO.StringIO( ) # output buffer for our message
htmlin = cStringIO.StringIO(html)
txtin = cStringIO.StringIO(text)
writer = MimeWriter.MimeWriter(out)
# Set up some basic headers. Place subject here
# because smtplib.sendmail expects it to be in the
# message body, as relevant RFCs prescribe.
writer.addheader("From",addr_from)
writer.addheader("To",addr_to)
writer.addheader("Subject", subject)
writer.addheader("MIME-Version", "1.0")
# Start the multipart section of the message.
# Multipart/alternative seems to work better
# on some MUAs than multipart/mixed.
writer.startmultipartbody("alternative")
writer.flushheaders( )
# the plain-text section: just copied through, assuming iso-8859-1
subpart = writer.nextpart( )
pout = subpart.startbody("text/plain", [("charset", charset)])
pout.write(txtin.read( ))
txtin.close( )
# the HTML subpart of the message: quoted-printable, just in case
subpart = writer.nextpart( )
subpart.addheader("Content-Transfer-Encoding", "quoted-printable")
pout = subpart.startbody("text/html", [("charset", charset)])
mimetools.encode(htmlin, pout, 'quoted-printable')
htmlin.close( )
# You're done; close your writer and return the message body
writer.lastpart( )
msg = out.getvalue( )
out.close( )
return msg
f = open("test.html", 'r')
html = f.read( )
f.close( )
fromAddr = """PythonMail<[email protected]>"""
toAddr = """PythonMail<[email protected]>"""
server = smtp_server()
#message = server.create_html_mail(html)
#server.send(fromAddr, toAddr, message)
server.send_html(fromAddr, toAddr,html,"subject")
相應修改smtp服務器的地址和認證信息,保存爲"html_smtp.py"文件,直接運行即可發送內容爲當前目錄下名爲:“test.html”的html格式郵件