我们如何处理Python xmlrpclib连接被拒绝? [英] How do we handle Python xmlrpclib Connection Refused?

查看:1366
本文介绍了我们如何处理Python xmlrpclib连接被拒绝?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道我在这里做错了什么,我写了一个RPC客户端尝试连接到一个不存在的服务器,我试图处理被抛出的异常,但不管我试着找不到我应该如何处理这个问题:

  def _get_rpc():
尝试:
a = ServerProxy('http:// dd:LNXFhcZnYshy5mKyOFfy@127.0.0.1:9001')
a = a.supervisor
返回
除了:
返回False

rpc = _get_rpc()
如果不是rpc:
打印无RPC

由于没有服务器运行,我希望输出为无RPC,但是我会收到一个例外:

 追溯(最近的最后一次呼叫):
文件xmlrpctest.py,第20行,< module>
如果不是rpc:
文件/usr/lib/python2.6/xmlrpclib.py,第1199行,__call__
return self .__ send(self .__ name,args)
文件/usr/lib/python2.6/xmlrpclib.py,第1489行,__request
verbose = self .__ verbose
文件/usr/lib/python2.6/xmlrpclib.py ,第1235行,请求
self.send_content(h,request_body)
文件/usr/lib/python2.6/xmlrpclib.py,第1349行,在send_content
连接中。 endheaders()
文件/usr/lib/python2.6/httplib.py,第908行,endheaders
self._send_output()
文件/ usr / lib / python2 6 / httplib.py,第780行,在_send_output
self.send(msg)
文件/usr/lib/python2.6/httplib.py,第739行,发送
self.connect()
文件/usr/lib/python2.6/httplib.py,第720行,连接
self.timeout)
文件/ usr / lib / python2.6 / socket.py,第561行,在create_connection
raise错误,msg
socket.error:[Errno 111]连接拒绝


解决方案

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

  Python 2.6.5(r265:79063,2010年4月16日,13:57:41)
[GCC 4.4.3] on linux2
输入help,copyright,credits或license了解更多信息。
>>> import xmlrpclib
>>> xmlrpclib.ServerProxy( http://127.0.0.1);
< ServerProxy for 127.0.0.1/RPC2>
>>> xmlrpclib.ServerProxy( http://127.0.0.1).supervisor;
追溯(最近的最后一次呼叫):
文件< stdin>,第1行,< module>
文件/usr/lib/python2.6/xmlrpclib.py,第1199行,__call__
return self .__ send(self .__ name,args)
文件/ usr / lib / python2.6/xmlrpclib.py,第1489行,__request
verbose = self .__ verbose
文件/usr/lib/python2.6/xmlrpclib.py,第1243行,请求
标头
xmlrpclib.ProtocolError:< 127.0.0.1/RPC2的协议错误:404未找到>
>>> 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
追溯(最近的最后一次呼叫):
文件< stdin>,第1行,< module>
文件/usr/lib/python2.6/xmlrpclib.py,第1199行,__call__
return self .__ send(self .__ name,args)
文件/ usr / lib / python2.6/xmlrpclib.py,第1489行,__request
verbose = self .__ verbose
文件/usr/lib/python2.6/xmlrpclib.py,第1243行,请求
标头
xmlrpclib.ProtocolError:< 127.0.0.1/RPC2的协议错误:404未找到>
>>> bar = foo.supervisor
>>> bar
追溯(最近的最后一次呼叫):
文件< stdin>,第1行,< module>
文件/usr/lib/python2.6/xmlrpclib.py,第1199行,__call__
return self .__ send(self .__ name,args)
文件/ usr / lib / python2.6/xmlrpclib.py,第1489行,__request
verbose = self .__ verbose
文件/usr/lib/python2.6/xmlrpclib.py,第1243行,请求
标头
xmlrpclib.ProtocolError:< 127.0.0.1/RPC2的协议错误:404未找到>注意,当您尝试评估.supervisor方法(ServerProxy(...))时,请注意您是如何看到异常的。 .supervisor,foo.supervisor或bar),而不是在其他地方分配(bar = foo.supervisor)。


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:LNXFhcZnYshy5mKyOFfy@127.0.0.1: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

解决方案

_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:

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>

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).

这篇关于我们如何处理Python xmlrpclib连接被拒绝?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆