需求:客户需要通过HTTP请求(URL)方式访问子系统,用于请求XXX数据,返回XML数据格式。
方案:使用一个python脚本来起一个socket服务,用于客户来访问该服务获取数据。
代码如下:
以下代码处理两个不同的客户访问我们服务,上下文分别为/doTrader 一个返回json字符串 /wjdInterface 一个返回XML报文
def doTrader(paramXml, httpUrl):
xxxxxx
return json.dumps(results)
def wjdInterface(paramXml, httpUrl):
xxxxx
res_xml = makeXmlForHandle(results)
return res_xml
# 将对象编辑成XML报文格式
def makeXmlForHandle(dic):
xml = "<FUND_RECORD> "
print(dic)
if dic != None:
for item in dic:
xml += "<" + item + ">" + str(dic[item]) + "</" + item + "> "
else:
xml += "<rescode>0</rescode> <resmsg>未找到相关数据</resmsg> "
xml += "</FUND_RECORD>"
return xml
----------------------------------------------------------------------------------------------------------------------------
from flask import Flask
from flask_spyne import Spyne
from spyne.model.primitive import Unicode, Integer
from spyne.model.complex import Iterable
from spyne import Application as TraderService
from spyne import rpc
from spyne import ServiceBase
from spyne import Iterable, Integer, Unicode, String
from spyne.protocol.soap import Soap11
from spyne.server.wsgi import WsgiApplication
from dateutil.relativedelta import relativedelta
from HBase import HBase
import sys
from hbase.ttypes import ColumnDescriptor, Mutation, BatchMutation
from ELSearch import ELSearch
import logging
import datetime
import urllib
import urllib2
import json
import time
import select
import os
import sys
import socket
import threading # install
socketHost="0.0.0.0"
sport = sys.argv[1]
iport = int(sport)
socketPort =iport
today = datetime.datetime.today()
# logging.basicConfig()
# reload(sys)
# sys.setdefaultencoding('utf-8')
# count = 0
elHost = "127.0.0.1"
elPort = 9200
# # 业务系统接口
interfaceURL = "http://127.0.0.1:9010/gjj/intservice"
elSearch = ELSearch(elHost, elPort)
#设置默认编码 用于解决URL方法获取结果后转为中间编码超长(128)问题
defaultencoding = 'utf-8'
if sys.getdefaultencoding() != defaultencoding:
reload(sys)
sys.setdefaultencoding(defaultencoding)
def process(event):
try:
event.setblocking(0)
data = event.recv(buffLen)
if data:
arr = data.split(" ")
#post提交的参数
json1 = arr[len(arr) - 1]
postUrl = ""
rsp = "" # 相应报文
context, url, urlGetParams = getContextAndParams(arr[0])
if (len(arr) > 0):
if context == "doTrader":
rsp = doTrader(json1, url)
# rsp = doTrader(json1, postUrl.split()[1])
# rsp = "{中国}"
resp = 'HTTP/1.1 200 OK '
resp += 'Server:RsServer '
resp += 'Content-Type: application/json;charset=utf-8 '
resp += 'Connection: close '
resp += 'Cache-Control: no-cache '
resp += 'pragma: no-cache '
resp += 'Content-Length: ' + str(len(rsp)) + ' '
resp += rsp
event.send(resp)
else:
rsp = wjdInterface(urlGetParams, url)
# rsp = doTrader(json1, postUrl.split()[1])
# rsp = "{中国}"
resp = 'HTTP/1.1 200 OK '
resp += 'Server:RsServer '
resp += 'Content-Type: text/xml;charset=utf-8 '
resp += 'Connection: close '
resp += 'Cache-Control: no-cache '
resp += 'pragma: no-cache '
resp += 'Content-Length: ' + str(len(rsp)) + ' '
resp += rsp
event.send(resp)
except Exception, e:
print(e)
event.close()
server=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.setblocking(0)
server.bind((socketHost,socketPort))
server.listen(1024)
print("Bind "+socketHost+":"+str(socketPort)+" ok!")
inputs=[server,]
buffLen=25600
while True:
r_list,w_list,e_list=select.select(inputs,[],[],1)
for event in r_list:
if event==server:
ss,addr=event.accept()
inputs.append(ss)
else:
thread = threading.Thread(target=process, args=(event,))
thread.start()
inputs.remove(event)
---------------------------------------------------------------------------
启动脚本 restart.sh
单线程脚本内容
pkill -9 -f ys.py
nohup python -u ys.py 6030 &
------------------------------------------------------------------------------------------------------------------------
通过POSTMAN发送http请求
A客户:
B客户: