3

Is there an easy way to specify the source port range the xml-rpc client connection should originate from?

Let's suppose there is a firewall between the client and xml-rpc server which passes traffic to the server only from specific sockets (defined by IP and port range).

By default the xmlrpc.client.ServerProxy lets the OS to decide which local port to use. As a result each xml-rpc call opens connection to the server originating from its own socket which stays in TIME_WAIT state until the timeout expires. On windows netstat -b shows such connections:

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

What I would like to achieve is a configurable range for ports (15XX from the example above) the xmlrpc.client.ServerProxy would use when calling rpc methods on the server.

Mr. Girgitt
  • 2,853
  • 1
  • 19
  • 22

2 Answers2

2

: is delimiter between host and port.

netstat -a | grep :15* 
PersianGulf
  • 2,845
  • 6
  • 47
  • 67
  • Thanks but the question is not about filtering netstat output; it's about configuring ServerProxy object so it uses configured local port range when communicating with an xml-rpc server. Probably a custom transport must be defined with an overridden socket.bind() function but I was hoping someone already implemented such option. – Mr. Girgitt Mar 13 '14 at 19:03
0

After getting from ServerProxy call:

cli = xmlrpclib.ServerProxy(..)

down to

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

I found the source_address kw used to specify what I was looking for. Additional stack overflow search against source_address leads to thread: Python: Is it possible to set the clientport with xmlrpclib? which defines the custom transport specifying source address and more-less answers my question; at least it's a good starting point to implement the port range.

Community
  • 1
  • 1
Mr. Girgitt
  • 2,853
  • 1
  • 19
  • 22