使用 httplib2.Http() 对象的最佳实践
我正在使用这样的类编写一个 pythonic Web API 包装器,
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)
正如您所看到的,我正在使用 _http()
方法来简化与 httplib2.Http()
的交互代码>对象。 这个方法在类中经常被调用,我想知道与这个对象交互的最佳方式是什么:
- 在 __init__ 中创建对象,然后在调用时重用它。调用
_http()
方法(如上面的代码所示) - 或在每次调用的方法内创建
httplib2.Http()
对象_http()
方法(如下面的代码示例所示)
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)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在标头中提供 'connection': 'close' 应根据文档在收到响应后关闭连接。:
Supplying 'connection': 'close' in your headers should according to the docs close the connection after a response is received.:
如果重用连接,则应保留 Http 对象。 看来 httplib2 能够像您在第一个代码中使用连接一样重用连接,因此这看起来是一个很好的方法。
同时,从对 httplib2 代码的浅层检查来看,httplib2 似乎不支持清理未使用的连接,甚至不支持服务器何时决定关闭它不再需要的连接。 如果确实如此,那么对我来说,它看起来像是 httplib2 中的一个错误 - 所以我宁愿使用标准库 (httplib)。
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.