0

我对运行良好的解决方案有疑问,但突然停止这样做。我编写了服务器/客户端脚本和模块来为我的应用程序创建一个 SOAP 连接类型。这是我的服务器代码:

import os
import rsglobal
import signal
import soaplib
from soaplib.core.service import rpc, DefinitionBase, soap
from soaplib.core.model.primitive import String, Integer
from soaplib.core.server import wsgi
from soaplib.core.model.clazz import Array

try:
    import psycopg
except:
    import psycopg2 as psycopg

import rssql

LogFile = "serwer_soap.log"


#-##############################################################################
#-- SOAP SERVER CLASS
#-##############################################################################

class AnakondaSOAPServer(DefinitionBase):

    #-##############################################################################
    #-- SOAP FUNCTIONS
    #-##############################################################################

    @soap(String,Integer,_returns=Array(String))
    def say_hello(self,name,times):
        results = []
        for i in range(0,times):
            results.append('Hello, %s'%name)
        return results

    @soap(String,String,_returns=String)
    def ConnectToBase(self,name,passwd):
        global PolSQL
        try:
            stat = 'OK'
            dane=[]
            PolSQL=rssql.RSSQL()

            if PolSQL.ConnectToSQL(name,passwd,'127.0.0.1',5432,'','databasename')==1:
                stat = u'Connection Error'

            SQL='SELECT * FROM table1;'
            stat,dane,dump,dump=PolSQL.Execute(SQL,3)
        except Exception,e:
            return u'Connection Error '+str(e)

        return stat+' '+str(dane)

    @soap(_returns=String)
    def GiveData(self):
        global PolSQL
        try:
            stat = 'OK'
            dane=[]
            SQL='SELECT * FROM table1;'
            stat,dane,dump,dump=PolSQL.Execute(SQL,3)
        except Exception,e:
            return u'Data getting error '+str(e)

        return stat+' '+str(dane)

    #------------------------------------------
    # ADMINISTRATIVE FUNCTIONS
    #------------------------------------------

    def SetDefaultData(self):
        self._oldHupHandler = signal.SIG_DFL
        self._oldAlarmHandler = signal.SIG_DFL
        self._oldTermHandler = signal.SIG_DFL
        self._childProcess = None
        self.PolSQL = None

#-------------------------------------------------------------------------------

    def _OnSIGALRM(self, sig, frame):
        pass

#-------------------------------------------------------------------------------

    def _OnSIGHUP(self, sig, frame):
        if self._childProcess:
            os.kill(self._childProcess, signal.SIGHUP)
        else:
            pass

#-------------------------------------------------------------------------------

    def _OnSIGTERM(self, sig, frame):
        pass

#-------------------------------------------------------------------------------

    def _exit(self, status):
        if self.PolSQL:
            self.PolSQL.DisconnectSQL()
        if status:
            rsglobal.Log(u"SOAP - finishing : "+str(status), 1)
        os._exit(status)

#-------------------------------------------------------------------------------

    def StartSOAPServer(self, dbspec,HostSOAP,PortSOAP):
        import os

        self.dbspec = dbspec
        childPID = os.fork()
        if childPID:
            self._childProcess = childPID
            return childPID
        else:
            try:
                signal.signal(signal.SIGUSR1, signal.SIG_DFL)
                signal.signal(signal.SIGCHLD, signal.SIG_DFL)
                if LogFile:
                    rsglobal.LogFile = LogFile

                self._oldHupHandler = signal.signal(signal.SIGHUP, lambda x, y: self._OnSIGHUP(x, y))
                self._oldAlarmHandler = signal.signal(signal.SIGALRM, lambda x, y: self._OnSIGALRM(x, y))
                self._oldTermHandler = signal.signal(signal.SIGTERM, lambda x, y: self._OnSIGTERM(x, y))
                self.PolSQL = SQLConnect(self.dbspec)

                # running SOAP server
                from wsgiref.simple_server import make_server
                ServiceSoap = soaplib.core.Application([AnakondaSOAPServer],'AnakSOAP',name='AnakSOAP')
                WSGI = wsgi.Application(ServiceSoap)
                Serwer = make_server(HostSOAP, int(PortSOAP), WSGI)
                Serwer.serve_forever()

            except Exception, exc:
                rsglobal.ZapiszDoLogow(u"Server SOAP Error : "+str(exc), 2)
                self._exit(1)

#-------------------------------------------------------------------------------

    def StopSOAPServer(self):
        if self._childProcess:
            os.kill(self._childProcess, signal.SIGTERM)

#-##################################################################


#-### - This is main SOAP server object used in other modules
SerwerSOAP = AnakondaSOAPServer()
SerwerSOAP.SetDefaultData()

#-##############################################################################
#-## ADDITIONAL FUNCTIONS
#-##############################################################################

def SQLConnect(dbspec):
    # creates connection to database

    PolSQL = rssql.RSSQL()
    dbuser, dbpass, dbhost, dbport, dbase = dbspec
    if PolSQL.PolaczZSQL(dbuser, dbpass, dbhost, dbport, None, Baza=dbase):
        return False
    else:
        return PolSQL

我的客户端代码(只是测试服务器)是这样的:

from suds.client import Client

hello_client = Client('http://128.1.2.3:1234/AnakondaSOAPServer?wsdl')

hello_client.options.cache.clear()

result = hello_client.service.ConnectToBase("username", "password")
print 'Result1',result
result = hello_client.service.GiveData()
print 'Result2',result

在我的主应用程序的早期,我使用具有适当参数的函数 StartSOAPServer 并且它正在等待连接。我可以在良好的地址和端口上使用 netstat 看到它。

运行客户端脚本后,我得到:

Traceback (most recent call last):
  File "./testsoapclient.py", line 8, in <module>
    hello_client = Client('http://128.1.2.3:1234/AnakondaSOAPServer?wsdl')
  File "/usr/local/lib/python2.6/dist-packages/suds-0.4-py2.6.egg/suds/client.py", line 112, in __init__
    self.wsdl = reader.open(url)
  File "/usr/local/lib/python2.6/dist-packages/suds-0.4-py2.6.egg/suds/reader.py", line 152, in open
    d = self.fn(url, self.options)
  File "/usr/local/lib/python2.6/dist-packages/suds-0.4-py2.6.egg/suds/wsdl.py", line 158, in __init__
    self.resolve()
  File "/usr/local/lib/python2.6/dist-packages/suds-0.4-py2.6.egg/suds/wsdl.py", line 207, in resolve
    c.resolve(self)
  File "/usr/local/lib/python2.6/dist-packages/suds-0.4-py2.6.egg/suds/wsdl.py", line 494, in resolve
    raise Exception("msg '%s', not-found" % op.input)
Exception: msg 's0:ConnectToBase', not-found

早些时候,我在创建 SOAP 连接后遇到了函数可见性问题。解决方案是清除缓存。现在我什至无法创建它。我使用 python 2.6,最近我的主应用程序从 2.4 转移到了 2.6。这是我能想到的唯一区别。

我尝试使用soaplib.core.Application 定义进行操作,但没有奏效。

请帮忙 :)

4

1 回答 1

0

我设法克服了这些问题,它又开始工作了。修复情况的第一件事是将服务器分成 2 个类 - 一个用于创建对象并运行 SOAPServer,第二个 - SOAPServer 仅包含函数定义。

即使做了这些改变,我的结果也很糟糕。我发现,服务器定义的损坏结构隐藏在 /tmp/suds 目录中(这就是为什么它在之前有损坏的代码时工作的原因)。删除此文件允许刷新此结构,并且所有脚本都重新开始工作。

于 2013-11-19T19:27:02.880 回答