Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practise when using httplib2.Http() object

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)
like image 898
tomaz Avatar asked Aug 08 '09 13:08

tomaz


2 Answers

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.

like image 34
Martin v. Löwis Avatar answered Sep 28 '22 16:09

Martin v. Löwis


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)
like image 169
Chris Hannam Avatar answered Sep 28 '22 15:09

Chris Hannam