Re: xmlrpc, httplib and SSL (HTTP 1.1 XMLRPC client)

Discussion in 'Python' started by Etienne Posthumus, Apr 1, 2004.

  1. On Mar 27, 2004, at 5:03 AM, Roger Binns wrote:
    > However I was wondering if anyone was working on fixing the
    > (IMHO horrible) mess and wants any moral support?


    I wanted to do keep the connections on which my clients do XMLRPC calls
    open, and after staring at the xmlrpclib.py source for a while, came up
    with the class at the bottom of the message. Just thought I would post
    it here in the spirit of sharing. You would use it like this:

    import httplib, xmlrpclib
    s = xmlrpclib.ServerProxy('http://SOMEURL',
    transport=PersistTransport())

    And then use as normal. When any error occurs, the connection is
    closed, seems a bit pessimistic, but I didn't want to do anything more
    fancy.

    cheers,

    Etienne Posthumus
    ---
    http://www.mnemosyne.org/
    Cultural Heritage Research
    Python, Zope, XML expertise for hire.
    Amsterdam, Nederland
    ----

    class PersistTransport(xmlrpclib.Transport):
    '''Provides a Transport for the xmlrpclib that uses httplib
    supporting persistent connections
    Does not close the connection after each request.
    '''
    connection = None

    def request(self, host, handler, request_body, verbose=0):
    if not self.connection:
    host, extra_headers, x509 = self.get_host_info(host)
    self.connection = httplib.HTTPConnection(host)
    self.headers = {"User-Agent" : self.user_agent,
    "Content-Type" : "text/xml",
    "Accept": "text/xml"}
    if extra_headers:
    for key, item in extra_headers:
    self.headers[key] = item

    self.headers["Content-Length"] = str(len(request_body))
    self.connection.request('POST', handler, request_body,
    self.headers)
    r = self.connection.getresponse()
    if r.status != 200:
    self.connection.close()
    self.connection = None
    raise xmlrpclib.ProtocolError( host + handler, r.status,
    r.reason, '' )
    data = r.read()
    p, u = self.getparser()
    p.feed(data)
    p.close()
    return u.close()
    Etienne Posthumus, Apr 1, 2004
    #1
    1. Advertising

  2. Etienne Posthumus

    Roger Binns Guest

    > I wanted to do keep the connections on which my clients do XMLRPC calls
    > open, and after staring at the xmlrpclib.py source for a while, came up
    > with the class at the bottom of the message.


    Are you sure you are actually getting persistent connections? The
    code will auto-close and auto-open the connection even just keeping
    the same HTTP connection object.

    In order for the connection to not be auto-closed, the remote end
    must return a HTTP/1.1 response (see httplib.HTTPResponse._check_close).

    If your server end is Python, then it will always close the connection
    unless the request was HTTP/1.1 *and* a 'Connection: keep-alive' header
    was sent, which the Python client does not do.

    The above is all true for Python 2.3. For 2.2 it does HTTP/1.0 IIRC
    so you have no hope.

    Roger
    Roger Binns, Apr 1, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Leon
    Replies:
    0
    Views:
    543
  2. Philippe C. Martin
    Replies:
    1
    Views:
    285
    Philippe C. Martin
    Oct 21, 2005
  3. ak
    Replies:
    7
    Views:
    5,425
  4. News123
    Replies:
    9
    Views:
    3,059
    vilas
    Feb 15, 2012
  5. Chris Down
    Replies:
    0
    Views:
    150
    Chris Down
    Aug 26, 2013
Loading...

Share This Page