【问题标题】:python xmlrpc.client.ServerProxy - how to specify local port rangepython xmlrpc.client.ServerProxy - 如何指定本地端口范围
【发布时间】:2014-04-07 16:00:27
【问题描述】:

是否有一种简单的方法来指定 xml-rpc 客户端连接应该源自的源端口范围?

假设在客户端和 xml-rpc 服务器之间有一个防火墙,它只将流量从特定的套接字(由 IP 和端口范围定义)传递到服务器。

默认情况下,xmlrpc.client.ServerProxy 让操作系统决定使用哪个本地端口。结果,每个 xml-rpc 调用都会打开与服务器的连接,该连接源自其自己的套接字,该套接字保持 TIME_WAIT 状态,直到超时到期。在 windows netstat -b 显示这样的连接:

C:\tmp>netstat -b | grep 51000
  TCP    ZIBI:51000             localhost:1552         TIME_WAIT    0
  TCP    ZIBI:51000             localhost:1562         TIME_WAIT    0
  TCP    ZIBI:51000             localhost:1561         TIME_WAIT    0
  TCP    ZIBI:51000             localhost:1553         TIME_WAIT    0
  TCP    ZIBI:51000             localhost:1559         TIME_WAIT    0
  TCP    ZIBI:51000             localhost:1558         TIME_WAIT    0
  TCP    ZIBI:51000             localhost:1557         TIME_WAIT    0

我想要实现的是在服务器上调用 rpc 方法时xmlrpc.client.ServerProxy 将使用的端口(上例中的 15XX)的可配置范围。

【问题讨论】:

    标签: python xml-rpc xmlrpcclient


    【解决方案1】:

    : 是主机和端口之间的分隔符。

    netstat -a | grep :15* 
    

    【讨论】:

    • 谢谢,但问题不在于过滤 netstat 输出;它是关于配置 ServerProxy 对象,以便在与 xml-rpc 服务器通信时使用配置的本地端口范围。可能必须使用重写的 socket.bind() 函数定义自定义传输,但我希望有人已经实现了这样的选项。
    【解决方案2】:

    从 ServerProxy 调用获取后:

    cli = xmlrpclib.ServerProxy(..)
    

    下到

    <httplib.py>
    
    class HTTPConnection:
    
      _http_vsn = 11
      _http_vsn_str = 'HTTP/1.1'
    
      response_class = HTTPResponse
      default_port = HTTP_PORT
      auto_open = 1
      debuglevel = 0
      strict = 0
    
      def __init__(self, host, port=None, strict=None,
                 timeout=socket._GLOBAL_DEFAULT_TIMEOUT, source_address=None):
    

    我找到了用于指定我要查找的内容的 source_address kw。针对 source_address 的额外堆栈溢出搜索导致线程:Python: Is it possible to set the clientport with xmlrpclib? 它定义了指定源地址的自定义传输,并且更多地回答了我的问题;至少这是实施端口范围的一个很好的起点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-26
      • 2014-04-05
      • 1970-01-01
      • 1970-01-01
      • 2021-05-16
      相关资源
      最近更新 更多