2013-03-19 84 views
2

我從python網站上獲得了XMLRPC服務器/客戶端。當我運行諸如pow()之類的簡單方法,從Windows添加(),div()時,客戶端會正確地獲取結果。但是,當我運行brocade(),它使用python模塊(PyCrypto,Paramiko)從Brocade交換機獲取配置信息,並返回字符串輸出作爲返回值,這需要2-3秒鐘的時間(可能需要更長的時間,具體取決於開關命令)。我觀察到服務器正確執行並打印輸出,但客戶端收到如下所示的消息。如何獲取XMLRPC服務器(Python 2.7.3)的輸出?

C:\CVS\Python\MyTestProject\src>python xmlrpcclient2.py 
8 
5 
2 
Traceback (most recent call last): 
    File "xmlrpcclient2.py", line 8, in <module> 
    print s.brocade('sh ver') 
    File "c:\Python27\lib\xmlrpclib.py", line 1224, in __call__ 
    return self.__send(self.__name, args) 
    File "c:\Python27\lib\xmlrpclib.py", line 1578, in __request 
    verbose=self.__verbose 
    File "c:\Python27\lib\xmlrpclib.py", line 1264, in request 
    return self.single_request(host, handler, request_body, verbose) 
    File "c:\Python27\lib\xmlrpclib.py", line 1297, in single_request 
    return self.parse_response(response) 
    File "c:\Python27\lib\xmlrpclib.py", line 1473, in parse_response 
    return u.close() 
    File "c:\Python27\lib\xmlrpclib.py", line 793, in close 
    raise Fault(**self._stack[0]) 
xmlrpclib.Fault: <Fault 1: "<type 'exceptions.TypeError'>:cannot marshal None unless allow_none is enabled"> 

服務器顯示

T06CORE - - [19/Mar/2013 11:11:33] "POST /RPC2 HTTP/1.1" 200 - 
T06CORE - - [19/Mar/2013 11:12:04] "POST /RPC2 HTTP/1.1" 200 - 
T06CORE - - [19/Mar/2013 11:12:05] "POST /RPC2 HTTP/1.1" 200 - 
T06CORE - - [19/Mar/2013 11:12:06] "POST /RPC2 HTTP/1.1" 200 - 
All jobs enqueued. 
Waiting for the queue to finish. 



You have reached Brocade FCF195 


    cmsh 
gets you into the IOS-style management shell. 
----------------------------------------------------------------- 
broc150_jack:admin> switchshow | grep switchType 
switchType:  76.7 
broc150_jack:admin> cmsh 
broc150_jack#sh ver 
←[?1h←=←[K   Fabric Operating System Software 
Fabric Operating System Version: 7.0 
Copyright (c) 1995-2008 Brocade Communications Systems, Inc. 
Build Time: 21:52:25 Oct 10, 2012 
broc150_jack uptime: 12w4d19h 
Firmware name: v7.0.2a 

Switch Model Name: Brocade 8000 
Control Processor: Freescale Semiconductor 8548E with 1016 MB of memory 

4MB of boot flash memory. 

    8 FC Port(s) 
24 Ten GigabitEthernet/IEEE 802.3 interface(s) 
←[K   ←[?1l←>broc150_jack#10.0.0.150 job is done. 
Destroying queue... 
Queue destroyed. 
T06CORE - - [19/Mar/2013 11:12:08] "POST /RPC2 HTTP/1.1" 200 - 

你有)任何想法的客戶如何能得到織錦的結果(?

謝謝, Spark。

xmlrpcserver2.py

from SimpleXMLRPCServer import SimpleXMLRPCServer 
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler 

# Restrict to a particular path. 
class RequestHandler(SimpleXMLRPCRequestHandler): 
rpc_paths = ('/RPC2',) 

# Create server 
listening_port = 8002 
server = SimpleXMLRPCServer(("localhost", listening_port), 
          requestHandler=RequestHandler) 
print "Listening on port %d..." % listening_port 
server.register_multicall_functions()       
server.register_introspection_functions() 

# Register pow() function; this will use the value of 
# pow.__name__ as the name, which is just 'pow'. 
server.register_function(pow) 

# Register a function under a different name 
def adder_function(x,y): 
    return x + y 
server.register_function(adder_function, 'add') 

def brocade(str): 
    import brocade 
    return brocade.MySwitch().do_something_cool(str) 
server.register_function(brocade, 'brocade') 

# Register an instance; all the methods of the instance are 
# published as XML-RPC methods (in this case, just 'div'). 
class MyFuncs: 
    def div(self, x, y): 
     return x // y 
server.register_instance(MyFuncs()) 

# Run the server's main loop 
server.serve_forever() 

xmlrpcclient2.py

import xmlrpclib 

s = xmlrpclib.ServerProxy('http://localhost:8002') 

print s.pow(2,3) # Returns 2**3 = 8 
print s.add(2,3) # Returns 5 
print s.div(5,2) # Returns 5//2 = 2 
print s.brocade('sh ver') 

回答

3

XMLRPC沒有技術上允許空值,並且當您嘗試序列化None,它正確地告訴你,它不能。許多xmlrpc服務違反了這些細節並以通常理解的方式發送它們。要在python中啓用此行爲,請將allow_none=True傳遞給服務器代碼中的SimpleXMLRPCServer()或客戶端中的ServerProxy()的構造函數。

server = SimpleXMLRPCServer(("localhost", listening_port), 
          requestHandler=RequestHandler, 
          allow_none=True) 

s = xmlrpclib.ServerProxy('http://localhost:8002', allow_none=True) 
+0

@ToekenMacGuy當我用「ALLOW_NONE」關鍵字,則返回「無」就在服務器仍在運行越來越開關版本信息的執行。我如何從客戶那裏獲取這些信息? – spark 2013-03-19 21:10:30

相關問題