python-將一個原有的winform程序包裝成web服務

歷史遺留下來的程序,可能沒有提供編程接口,但是又需要將這個程序的功能被其它代碼調用。剛好做了類似一個系統間

集成的例子。 Web 形式的OA 要集成 原有 winform 的短信息發送程序。

方法是:

    首先將原窗口程序包裝成可調用的函數模塊,請參考之前文章:    Python - 將window 窗口操作過程包裝成腳本函數可被調用 

    然後將此函數再次包裝成 web 服務



SMsWebproxy.py

通過 http://xxxxxx:8000/putmsg/手機號碼/信息文本   的方式就可以調用發送短信了。


  1 #! /usr/bin/python
  2 # -*- coding: gbk -*-
  3 
  4 # SmsWebProxy.py
  5 #
  6 # Copyright (C) 2012 - xulong <[email protected]>
  7 #
  8 import time
  9 import BaseHTTPServer, SocketServer, urllib, urllib2, urlparse
 10 import hashlib
 11 import math
 12 import traceback
 13 import string
 14 import os
 15 import re
 16 import threading
 17 from QXT_CTRL import QXT_Send
 18 allow_client=['192.168.1.113','192.168.1.9']
 19 msgqueue = []
 20 acitve = True
 21 class  SmsWebProxyProxyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
 22 
 23     def do_GET(self):
 24         # path check
 25         client_ip,t=self.client_address
 26         #print self.path , client_ip
 27 
 28 
 29 
 30         flag = '/putmsg/'
 31         if client_ip in allow_client and self.path.startswith(flag) :
 32             #Get Music ID
 33             raw_str = urllib.unquote(self.path).decode('utf8').encode('gbk')[len(flag):]
 34 
 35             phones = raw_str[0:raw_str.find('/')]
 36             msg=raw_str[raw_str.find('/')+1:]
 37             #print 'phones:',phones , 'msg:',msg 
 38             msgqueue.append((phones,msg))
 39 
 40             self.send_response(200)
 41             self.send_header("content-type","text/html")
 42             self.end_headers()
 43             self.wfile.write("<html><body>OK</bod></html>")
 44             self.connection.close()
 45             #print "msgqueue",msgqueue
 46             return
 47 
 48 
 49 class ThreadingHTTPServer(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer):
 50     pass
 51 
 52 class webserver(threading.Thread):
 53     def __init__(self):
 54         threading.Thread.__init__(self)
 55 
 56     def run(self):
 57         httpd = ThreadingHTTPServer(('', 8000),SmsWebProxyProxyHandler )
 58         httpd.serve_forever()
 59 class sender(threading.Thread):
 60     def __ini__(self):
 61         threading.Thread.__init__()
 62 
 63     def run(self):
 64         while acitve:
 65             if len(msgqueue) >0:
 66                 (p,m)= msgqueue.pop()
 67                 QXT_Send(p,m)
 68             time.sleep(0.01)
 69 
 70 
 71 
 72 
 73 def main():
 74 
 75     ws = webserver()
 76     ws.setDaemon(True)
 77     ws.start()
 78     sd = sender()
 79     sd.setDaemon(True)
 80     sd.start()
 81 
 82     print ''
 83     print '                           ####                          '
 84     print '                         ########                         '
 85     print '                         ########                         '
 86     print '###########################################################'
 87     print 'XXXXX-接口服務程序已啓動'
 88     print '請勿關閉此服務程序,請保持XXXXX程序也在運行中!!!'
 89     print '###########################################################'
 90     print '                         ########                          '
 91     print '                         ########                          '
 92     print '                           ####                          '
 93     while True:
 94         ins = raw_input("")
 95         time.sleep(0.01)
 96     #httpd = ThreadingHTTPServer(('', 8000),SmsWebProxyProxyHandler )
 97     #httpd.serve_forever()
 98     #acitve = False
 99 
100 
101 if __name__ == '__main__':
102     main()
103 
104 
105 
106 
107 
</html


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章