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

发布于 2024-07-30 19:38:37 字数 1176 浏览 8 评论 0原文

我正在使用这样的类编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拒绝两难 2024-08-06 19:38:37

在标头中提供 'connection': 'close' 应根据文档在收到响应后关闭连接。:

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

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)
念﹏祤嫣 2024-08-06 19:38:37

如果重用连接,则应保留 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文