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 <fangkailove@gmail.com>
  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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wxgnolux

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值