python3 通过ctypes调用tuxedo client
时间:2015-01-27 19:16 来源: 我爱IT技术网 作者:小搜
为了调用tuxedo client库,用python3的ctypes直接调用wsc库实现了对tuxedo服务的访问,而且对中文支持的也挺好
环境
操作系统:ubuntu 10.10
python:3.1.2
tuxedo:tuxedo11gR1 11.1.1.2.0
为了弄这个ctypes,可真是费了我不少功夫。
啥都不说了,直接上代码
# -*- coding:utf-8 -*-
#!/usr/bin/python3
import os, sys
from ctypes import CDLL, c_long, c_int, c_bool, c_char_p, c_void_p, c_byte, byref, POINTER, memmove
class ATMI:
__lib = None
wsnAddr = None
def __init__(self):
if os.name == 'nt':
wscLibName = "wtuxws32.dll"
elif os.name == 'posix':
wscLibName = "libwsc.so"
else:
raise Exception("未知操作系统!")
self.__lib = CDLL(wscLibName)
def init(self):
'''
初始化环境变量等
'''
self.__lib.tuxputenv(c_char_p(self.wsnAddr))
self.__lib.tuxputenv(c_char_p("WSINTOPPRE71=yes"))
def call(self, serviceName, inputStr, respList):
'''
同步执行ATMI服务
'''
ret = self.__lib.tpinit(c_int(0))
if ret < 0:
raise Exception("tpinit = {0}".format(ret))
try:
sendBuf = c_char_p()
recvBuf = c_char_p()
lenRecv = c_long(0)
sendBuf.value = self.__lib.tpalloc(c_char_p("STRING"), None, c_int(10240))
try:
recvBuf.value = self.__lib.tpalloc(c_char_p("STRING"), None, c_int(10240))
try:
lenSend = len(inputStr)
memmove(sendBuf, c_char_p(inputStr.encode("gbk")), lenSend)
ret = self.__lib.tpcall(c_char_p(serviceName), sendBuf, c_int(lenSend), byref(recvBuf), byref(lenRecv), c_long(0))
#print("tpcall", ret, "recvlen=", lenRecv.value)
#print(recvBuf.value)
if ret < 0:
raise Exception("tpcall ={0}".format(ret))
respList[0] = recvBuf.value
return True
finally:
self.__lib.tpfree(recvBuf.value)
finally:
self.__lib.tpfree(sendBuf.value)
finally:
ret = self.__lib.tpterm(None)
print("tpterm", ret)
if __name__ == "__main__":
atmi = ATMI()
atmi.wsnAddr = "WSNADDR=//192.168.0.7:7110"
atmi.init()
recv = [None]
ret = atmi.call("TOUPPER", "abcdefg 123kdkf aa", recv)
print(ret, recv[0])
print("end")
执行后的控制台输出如下
>> tpterm 1 >> True b'ABCDEFG 123KDKF AA' >> end
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
