gpt4 book ai didi

python - 我们如何处理 Python xmlrpclib Connection Refused?

转载 作者:太空狗 更新时间:2023-10-30 01:17:25 26 4
gpt4 key购买 nike

我不知道我到底做错了什么,我写了一个 RPC 客户端试图连接到一个不存在的服务器,我试图处理抛出的异常,但不管我尝试的是我无法弄清楚我应该如何处理这个:

def _get_rpc():
try:
a = ServerProxy('http://dd:LNXFhcZnYshy5mKyOFfy@127.0.0.1:9001')
a = a.supervisor
return a
except:
return False

rpc = _get_rpc()
if not rpc:
print "No RPC"

由于没有服务器在运行,我希望输出为“No RPC”,但我得到了一个异常:

Traceback (most recent call last):
File "xmlrpctest.py", line 20, in <module>
if not rpc:
File "/usr/lib/python2.6/xmlrpclib.py", line 1199, in __call__
return self.__send(self.__name, args)
File "/usr/lib/python2.6/xmlrpclib.py", line 1489, in __request
verbose=self.__verbose
File "/usr/lib/python2.6/xmlrpclib.py", line 1235, in request
self.send_content(h, request_body)
File "/usr/lib/python2.6/xmlrpclib.py", line 1349, in send_content
connection.endheaders()
File "/usr/lib/python2.6/httplib.py", line 908, in endheaders
self._send_output()
File "/usr/lib/python2.6/httplib.py", line 780, in _send_output
self.send(msg)
File "/usr/lib/python2.6/httplib.py", line 739, in send
self.connect()
File "/usr/lib/python2.6/httplib.py", line 720, in connect
self.timeout)
File "/usr/lib/python2.6/socket.py", line 561, in create_connection
raise error, msg
socket.error: [Errno 111] Connection refused

最佳答案

_get_rpc 返回对未连接的 ServerProxy 的主管方法的引用。在您处理它的 _get_rpc 调用中没有发生异常;当您尝试评估此主管方法时(在“if not rpc”中),它就会发生。从交互式提示尝试:

Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import xmlrpclib
>>> xmlrpclib.ServerProxy("http://127.0.0.1");
<ServerProxy for 127.0.0.1/RPC2>
>>> xmlrpclib.ServerProxy("http://127.0.0.1").supervisor;
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/xmlrpclib.py", line 1199, in __call__
return self.__send(self.__name, args)
File "/usr/lib/python2.6/xmlrpclib.py", line 1489, in __request
verbose=self.__verbose
File "/usr/lib/python2.6/xmlrpclib.py", line 1243, in request
headers
xmlrpclib.ProtocolError: <ProtocolError for 127.0.0.1/RPC2: 404 Not Found>
>>> foo = xmlrpclib.ServerProxy("http://127.0.0.1");
>>> dir (foo)
['_ServerProxy__allow_none', '_ServerProxy__encoding', '_ServerProxy__handler', '_ServerProxy__host', '_ServerProxy__request', '_ServerProxy__transport', '_ServerProxy__verbose', '__doc__', '__getattr__', '__init__', '__module__', '__repr__', '__str__']
>>> foo.supervisor
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/xmlrpclib.py", line 1199, in __call__
return self.__send(self.__name, args)
File "/usr/lib/python2.6/xmlrpclib.py", line 1489, in __request
verbose=self.__verbose
File "/usr/lib/python2.6/xmlrpclib.py", line 1243, in request
headers
xmlrpclib.ProtocolError: <ProtocolError for 127.0.0.1/RPC2: 404 Not Found>
>>> bar = foo.supervisor
>>> bar
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/xmlrpclib.py", line 1199, in __call__
return self.__send(self.__name, args)
File "/usr/lib/python2.6/xmlrpclib.py", line 1489, in __request
verbose=self.__verbose
File "/usr/lib/python2.6/xmlrpclib.py", line 1243, in request
headers
xmlrpclib.ProtocolError: <ProtocolError for 127.0.0.1/RPC2: 404 Not Found>

请注意,在尝试评估 .supervisor 方法(ServerProxy(...).supervisor、foo.supervisor 或 bar)时,您是如何看到异常的,但在将它分配到其他地方时(bar = foo.supervisor)却看不到。

关于python - 我们如何处理 Python xmlrpclib Connection Refused?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4696642/

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