我们如何处理Python xmlrpclib连接被拒绝?
我不知道我在这里做错了什么,我写了一个 RPC 客户端试图连接到一个不存在的服务器,并且我试图处理抛出的异常,但无论我尝试什么我不知道应该如何处理这个问题:
def _get_rpc():
try:
a = ServerProxy('http://dd:[email protected]: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
I don't know what the heck I'm doing wrong here, I wrote have an RPC client trying to connect to a non-existent server, and I'm trying to handle the exception that is thrown, but no matter what I try I can't figure out how I'm supposed to handle this:
def _get_rpc():
try:
a = ServerProxy('http://dd:[email protected]:9001')
a = a.supervisor
return a
except:
return False
rpc = _get_rpc()
if not rpc:
print "No RPC"
Since there is no server running, I would expect the output to be "No RPC" but instead I get an exception:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
_get_rpc 返回对未连接的 ServerProxy 的管理者方法的引用。异常不会发生在您处理它的 _get_rpc 调用中;当您尝试评估此主管方法(在“if not rpc”中)时,就会发生这种情况。从交互式提示中尝试:
注意在尝试计算 .supervisor 方法(ServerProxy(...).supervisor、foo.supervisor 或 bar)时如何看到异常,但在将其分配到其他地方时(bar = foo.导师)。
_get_rpc returns a reference to unconnected ServerProxy's supervisor method. The exception isn't happening in the call to _get_rpc where you handle it; it's happening when you try to evaluate this supervisor method (in "if not rpc"). Try from the interactive prompt:
Note how you see the exception when trying to evaluate the .supervisor method (ServerProxy(...).supervisor, foo.supervisor, or bar), but not when just assigning it elsewhere (bar = foo.supervisor).