您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python – 使用httplib2.Http()对象时的最佳实践

5b51 2022/1/14 8:22:47 python 字数 2958 阅读 569 来源 www.jb51.cc/python

我正在编写一个类似于此类的 pythonic Web API包装器 import httplib2 import urllib class apiWrapper: def __init__(self): self.http = httplib2.Http() def _http(self, url, method, dict): '''

概述

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()方法时重用它(如上面的代码所示)
>或者为每次调用_http()方法方法内创建httplib2.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)

同时,从对httplib2代码的浅层检查来看,似乎httplib2不支持清理未使用的连接,甚至不知道服务器何时决定关闭它不再需要的连接.如果确实如此,它看起来像是httplib2中的一个错误 – 所以我宁愿使用标准库(httplib).

总结

以上是编程之家为你收集整理的python – 使用httplib2.Http()对象时的最佳实践全部内容,希望文章能够帮你解决python – 使用httplib2.Http()对象时的最佳实践所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶