历史遗留下来的程序,可能没有提供编程接口,但是又需要将这个程序的功能被其它代码调用。刚好做了类似一个系统间
集成的例子。 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
标签:__,web,python,self,print,import,msgqueue,def,winform From: https://blog.51cto.com/fangkailove/5881701