gpt4 book ai didi

python - 如何获取 XMLRPC 服务器的输出(Python 2.7.3)?

转载 作者:太空宇宙 更新时间:2023-11-04 01:28:53 26 4
gpt4 key购买 nike

我从 python 网站获得了 XMLRPC 服务器/客户端。当我从 Windows 运行 pow()、add()、div() 等简单方法时,客户端会正确获得结果。但是,当我运行 brocade() 时,它使用 python 模块(PyCrypto、Paramiko)从 Brocade 交换机获取配置信息,并将字符串输出作为返回值返回,这需要大约 2-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 -

您知道客户端如何获得 brocade() 的结果吗?

谢谢, 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')

最佳答案

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)

关于python - 如何获取 XMLRPC 服务器的输出(Python 2.7.3)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15507552/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com