views:

430

answers:

2

I'm writing a pythonic web API wrapper with a class like this

import httplib2
import urllib

class apiWrapper:

    def __init__(self):
     self.http = httplib2.Http()

    def _http(self, url, method, dict):
     '''
     Im using this wrapper arround the http object
     all the time inside the class
     '''
     params = urllib.urlencode(dict)
     response, content = self.http.request(url,params,method)


as you can see I'm using the _http() method to simplify the interaction with the httplib2.Http() object. This method is called quite often inside the class and I'm wondering what's the best way to interact with this object:

  • create the object in the __init__ and then reuse it when the _http() method is called (as shown in the code above)
  • or create the httplib2.Http() object inside the method for every call of the _http() method (as shown in the code sample below)


import httplib2
import urllib


class apiWrapper:

    def __init__(self):

    def _http(self, url, method, dict):
     '''Im using this wrapper arround the http object
     all the time inside the class'''
     http = httplib2.Http()
     params = urllib.urlencode(dict)
     response, content = http.request(url,params,method)
A: 

You should keep the Http object if you reuse connections. It seems httplib2 is capable of reusing connections the way you use it in your first code, so this looks like a good approach.

At the same time, from a shallow inspection of the httplib2 code, it seems that httplib2 has no support for cleaning up unused connections, or to even notice when a server has decided to close a connection it no longer wants. If that is indeed the case, it looks like a bug in httplib2 to me - so I would rather use the standard library (httplib) instead.

Martin v. Löwis
+1  A: 

Supplying 'connection': 'close' in your headers should according to the docs close the connection after a response is received.:

headers = {'connection': 'close'}
resp, content = h.request(url, headers=headers)
Chris Hannam