python – 使用httplib2.Http()对象时的最佳实践
发布时间:2020-12-30 17:09:54 所属栏目:Python 来源:互联网
导读:我正在编写一个类似于此类的 pythonic Web API包装器 import httplib2import urllibclass apiWrapper: def __init__(self): self.http = httplib2.Http() def _http(self, url, method, dict):
我正在编写一个类似于此类的 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()方法时重用它(如上面的代码所示) import httplib2 import urllib class apiWrapper: def __init__(self): def _http(self,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,method) 解决方法如果重用连接,则应保留Http对象.似乎httplib2能够以你在第一个代码中使用它的方式重用连接,所以这看起来是一个很好的方法.同时,从对httplib2代码的浅层检查来看,似乎httplib2不支持清理未使用的连接,甚至不知道服务器何时决定关闭它不再需要的连接.如果确实如此,它看起来像是httplib2中的一个错误 – 所以我宁愿使用标准库(httplib). (编辑:十堰站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- Python使用metaclass实现Singleton模式的方法
- python-2.7 – TypeError:预期序列或类似数组,得
- 创建虚拟环境(Python)中“virtualenv”和“-m ve
- python – Django的call_command失败,缺少必需的
- Python functools.namedtuple
- python – 与布尔numpy数组VS PEP8 E712的比较
- python – syncdb和迁移有什么区别?
- python – django:django-tables2 DetailView C
- python – 是否可以使用__rmod__覆盖str的%行为
- Django没有为记录器“城市”找到处理程序
热点阅读