您的位置:首页 > 数据库 > Oracle

Python安装cx_Oracle模块和suds.client模块调用webservice服务

2017-07-20 17:27 555 查看
一、cx_Oracle模块安装:

1、安装oracle客户端:rpm -ivh oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm 

2、找出ORACLE_HOME,我这安装之后是: /usr/lib/oracle/11.2/client64/lib/

3、下载instantclient-sdk-linux.x64-11.2.0.4.0.zip,解压把sdk文件夹复制到/usr/lib/oracle/11.2/client64/lib/

4、做一个软连接:cd /usr/lib/oracle/11.2/client64/lib/
ln -s libclntsh.so.11.1 libclntsh.so

5、安装cx_Oracle :

[root@localhost lib]# export ORACLE_HOME=/usr/lib/oracle/11.2/client64/lib

[root@localhost lib]# export LD_LIBRARY_PATH=$LD_LIBRATY_PATH:$ORACLE_HOME

[root@localhost lib]# easy_install cx_oracle

6、试一下:

[root@localhost lib]# python

Python 2.7.5 (default, Nov  6 2016, 00:28:07) 

[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> import cx_Oracle

>>> 

测试正常

7、配置环境变量

vi .bash_profile

增加

export ORACLE_HOME=/usr/lib/oracle/11.2/client64/lib

export LD_LIBRARY_PATH=$LD_LIBRATY_PATH:$ORACLE_HOME

二、webservice服务suds.client模块安装:

1、下载suds-0.3.9.tar.gz

2、上传到服务器解压:tar zxvf suds-0.3.9.tar.gz

3、安装suds: python setup.py install

一个小案例:

#coding=utf-8

import sys

import time

from suds.client import Client

import cx_Oracle

import codecs

#from sendMessage import sendmessage

"""

deault_encoding='utf-8'

if sys.getdefaultencoding()!=deault_encoding:

    reload(sys)

    sys.setdefaultencoding(deault_encoding)

"""

def run_check_service(url,sysname,expcount):

    try:

        client=Client(url)

        print "%s===接口正常!"% sysname

        if expcount>0:

            msg="%s===接口恢复正常!"% sysname

            sendmessage(sysname,msg)

            expcount=0

        return expcount

    except:

        print "%s===接口异常!"% sysname

        if expcount<3:

            expcount+=1

            msg="%s===接口异常!"% sysname

            sendmessage(sysname,msg)

            return expcount

        else:

            expcount+=1

            return expcount

        

def run_transation_service(url,sysname,expcount):

    try:

        client=Client(url)

        print "%s====发起模拟交易!"% url

        print(client.service.switchToBusinessService("####0010|6300|330200101|42D02BD0C73CB27E4FFC7862910EA1F4|33020010120161215990000000|112233|2016-12-15 10:01:33|2016-12-15 10:01:33|0~1$$$$"))

        if expcount>0:

            msg="%s===接口恢复正常!"% sysname

            sendmessage(sysname,msg)

            expcount=0

        return expcount

    except:

        print "%s===接口异常!"% sysname

        if expcount<3:

            expcount+=1

            msg="%s===接口异常!"% sysname

            sendmessage(sysname,msg)

            return expcount

        else:

            expcount+=1

            return expcount

#发送短信

def sendmessage(sysname,msg):

    conn=cx_Oracle.connect("用户名","密码","ip:1521/sid")

    cursor=conn.cursor()

    #读取发送配置信息

    hosts_file=open('sendlist.ini','r')

    for line in hosts_file.readlines():

        line=line.strip('\n')

        items=line.split(',')

        sendname=items[0]

        telphone=items[1]

        #print(host+port+post+url+sysname)

        sql="insert into messageinfo (id,sysname,messageinfo,createtime,sendtime,sendflag,name,telephone) values (seq_id.nextval,'%s','%s',sysdate,sysdate,%s,'%s','%s')" % \

             (sysname,msg,"1",sendname,telphone)

        print "执行sql"

        print sql

        try:

            cursor.execute(sql)

            print "提交数据"

            conn.commit()

        except:

            print "发生错误,回滚数据!"

            conn.rollback()

        conn.close

        

y=0

list=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

trans=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

while True:

    print "开始检查服务!"

    i=0

    #读取webservice配置信息

    hosts_file=open('WebServiceURL.ini','r')

    for line in hosts_file.readlines():      

        line=line.strip('\n')

        items=line.split('#')

        url=items[0]

        sysname=items[1]

        #print sysname

        list[i]=run_check_service(url,sysname,list[i])   #调用检查服务方法

        print "%s异常次数:%s"% (i,list[i])

        i+=1

        

    #读取模拟交易配置信息

    j=0

    hosts_file=open('TransactionURL.ini','r')

    for line in hosts_file.readlines():      

        line=line.strip('\n')

        items=line.split('#')

        url=items[0]

        sysname=items[1]

        trans[j]=run_transation_service(url,sysname,trans[j])  #调用模拟交易方法

        print "%s异常次数:%s"% (j,trans[j])

        j+=1

    print"=================================================================="

    time.sleep(600)   #控制检查频率
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: