WEB开发网
开发学院软件开发Python Python Web 服务开发者: 通过 SMTP 处理 SOAP 阅读

Python Web 服务开发者: 通过 SMTP 处理 SOAP

 2008-09-30 13:01:38 来源:WEB开发网   
核心提示: 用户输入包含在 HandleInput 方法(请参阅 清单 4)中,该方法要求用户输入月份和年份,Python Web 服务开发者: 通过 SMTP 处理 SOAP(6),然后,它创建一个 SOAP 请求并将其发送给服务器,输出显示在 清单 5中, 清单 5. 客户机输出[molson@p

用户输入包含在 HandleInput 方法(请参阅 清单 4)中。该方法要求用户输入月份和年份。然后,它创建一个 SOAP 请求并将其发送给服务器。它还向侦听器注册这个请求,这样侦听器就知道要期待一个响应。在这个示例中,所有请求都向 DisplayResults 回调注册了。DisplayResults 方法只是将结果显示到屏幕上。

清单 4. 余下的客户机代码def DisplayResults(ID,ps):
  #This method is the generic callback used by all requests.
  #It uses the parsed SOAP to print out the results.
  print "
Results for ID: " + ID
  tc = TC.String()
  data = _child_elements(ps.body_root)
  if len(data) == 0: print None
  print tc.parse(data[0], ps)
def HandleInput(server):
  #This method is used to query the user for a year and a month.
  #When one is received, then a new message is sent, and the server
  #is told to expect the results
  done = 0
  lastID = 1
  while not done:
    year = raw_input("Year of request(Return to exit): ")
    if not year: done = 1
    else:
      year = int(year)
      month = int(raw_input("Month of request: "))
      lastID += 1
      mID = lastID
      
      msg = MIMEText.MIMEText(BODY_TEMPLATE%(year,month))
      msg['Subject'] = "Test Message"
      msg['To'] = 'calendar@localhost'
      msg['From'] = 'Mike.Olson@Fourthought.com'
      msg['Message-Id'] = str(mID)
      server.expectResponse(mID,DisplayResults)
      print "Sending out message ID: " + str(mID)
      common.SendMessage("127.0.0.1",8023,"me@fourthought.com",
       ["Mike.Olson@Fourthought.com"],msg)
def StartServer():
  #Start up our response server in another thread.
  server = ClientServer(("localhost", 8024),
             (None, 0))
  def run():
    try:
      asyncore.loop()
    except KeyboardInterrupt:
      pass
  print "Starting Client Server"
  t = threading.Thread(None,run)
  t.start()
  return server
if __name__ == '__main__':
  server = StartServer()
  HandleInput(server)

运行示例

客户机应用程序向用户查询月份和日期。您愿意进行多少次查询都可以。一旦返回了一个有效应答,这个有效应答将被打印到屏幕。您将注意到,您可能没有让所有的应答都按照和发送时相同的顺序返回。输出显示在 清单 5中。

清单 5. 客户机输出[molson@penny src]$ python client.py
Starting Client Server
Year of request(Return to exit): 2003
Month of request: 1
Sending out message ID: 2
Year of request(Return to exit): 2004
Month of request: 1
Sending out message ID: 3
Year of request(Return to exit): 2005
Month of request: 1
Sending out message ID: 4
Year of request(Return to exit):
Results for ID: 2
January 2003
Mo Tu We Th Fr Sa Su
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Results for ID: 3
January 2004
Mo Tu We Th Fr Sa Su
     1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Results for ID: 4
January 2005
Mo Tu We Th Fr Sa Su
        1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31

下一篇专栏文章

在本专栏的下一篇文章中,我们将研究一下与 Google 的 SOAP API 进行交互都需要些什么。

上一页  1 2 3 4 5 6 

Tags:Python Web 服务

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接