Python Web 服务开发者: 通过 SMTP 处理 SOAP
2008-09-30 13:01:38 来源:WEB开发网清单 2. 用于处理 SOAP 消息的服务器代码 def process_message(self, peer, mailfrom, rcpttos, data):
#Parse the message into an email.Message instance
p = Parser.Parser()
m = p.parsestr(data)
print "Received Message"
#See if it is a SOAP request
if m.has_key('To') and m['To'] == 'calendar@localhost':
self.process_soap(m)
else :
#In normal circumstances, this would probably
#forward the email message to another SMTP Server
print "Unknown Email message"
print m
def process_soap(self,message):
#Parse the SOAP Message
ps = parse.ParsedSoap(message.get_payload(decode=1))
#Store the current ID
self.currentID = message['Message-Id']
print "Processing Message: " + self.currentID
#Use ZSI's dispatcher to call the correct function based on the message.
dispatch._Dispatch(ps,
[self],
self.send_xml,
self.send_fault)
#ZSI Callback to send an SOAP(non-Fault) response.
def send_xml(self,xml):
self.return_soap(xml)
#ZSI callback to send a fault.
def send_fault(self,fault):
sys.stderr.write("FAULT While processing request:
");
s = cStringIO.StringIO()
fault.serialize(s)
st = s.getvalue()
print st
#Serialize the fault and send it to the client
self.return_soap(st)
#Called by our code to send result XML.
def return_soap(self,st):
msg = MIMEText.MIMEText(st)
msg['Subject'] = "Test Message"
msg['To'] = 'calendar@localhost'
msg['From'] = 'Mike.Olson@Fourthought.com'
msg['Message-Id'] = "2"
msg['In-Reply-To'] = self.currentID or 0
print "Sending Reply"
common.SendMessage("127.0.0.1",8024,"me@fourthought.com",
["Mike.Olson@Fourthought.com"],msg)
#Implementation of our SOAP Service.
def getMonth(self,year,month):
print "Request for %d,%d" % (year,month)
return calendar.month(year, month)
更多精彩
赞助商链接