如何关闭urllib2连接?

发布于 2024-11-19 00:22:57 字数 282 浏览 3 评论 0原文

我使用 urllib2 编写了一个程序,可以在网络上建立大量连接。我注意到,最终这可能是值得 DDoS 攻击的;我想知道如何在完成业务后关闭每个连接以防止此类攻击。

我用来打开连接的代码是:

cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
r = opener.open("http://www.python.org)
html = r.read()

I have made a program using urllib2 that makes a lot of connections across the web. I noticed that eventually that this can be DDoS worthy; I would like to know how to close down each connection after I have done my business to prevent such an attack.

The code I am using to open a connection is:

cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
r = opener.open("http://www.python.org)
html = r.read()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

吃→可爱长大的 2024-11-26 00:22:57

我假设您使用 urlopen() 函数打开它们。 其文档指出:

此函数返回一个类似文件的对象,具有两个附加方法:

作为类文件对象,它将有一个可以调用的 close 方法:

connection = urllib2.urlopen(url)
# Do cool stuff in here.
connection.close()

Update:使用代码您在问题中添加了:

>>> import urllib2
>>> import cookielib
>>> cj = cookielib.CookieJar()
>>> opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
>>> r = opener.open("http://www.python.org")
>>> html = r.read()
>>> r.close??
Type:  instancemethod
Base Class: <type 'instancemethod'>
String Form: <bound method addinfourl.close of <addinfourl at 150857644 whose fp = <socket._fileobject object at 0x8fd48ec>>>
Namespace: Interactive
File:  /usr/lib/python2.6/urllib.py
Definition: r.close(self)
Source:
    def close(self):
        self.read = None
        self.readline = None
        self.readlines = None
        self.fileno = None
        if self.fp: self.fp.close()
        self.fp = None

因此 close() 方法存在并且实际上做了一些事情:

>>> r.close()
>>> r.read()
------------------------------------------------------------
Traceback (most recent call last):
  File "<ipython console>", line 1, in <module>
TypeError: 'NoneType' object is not callable

I assume you are opening them with the urlopen() function. Its documentation states:

This function returns a file-like object with two additional methods:

As a file-like object, it will have a close method which you can call:

connection = urllib2.urlopen(url)
# Do cool stuff in here.
connection.close()

Update: Using the code you added to your question:

>>> import urllib2
>>> import cookielib
>>> cj = cookielib.CookieJar()
>>> opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
>>> r = opener.open("http://www.python.org")
>>> html = r.read()
>>> r.close??
Type:  instancemethod
Base Class: <type 'instancemethod'>
String Form: <bound method addinfourl.close of <addinfourl at 150857644 whose fp = <socket._fileobject object at 0x8fd48ec>>>
Namespace: Interactive
File:  /usr/lib/python2.6/urllib.py
Definition: r.close(self)
Source:
    def close(self):
        self.read = None
        self.readline = None
        self.readlines = None
        self.fileno = None
        if self.fp: self.fp.close()
        self.fp = None

So the close() method exists and actually does something:

>>> r.close()
>>> r.read()
------------------------------------------------------------
Traceback (most recent call last):
  File "<ipython console>", line 1, in <module>
TypeError: 'NoneType' object is not callable
小嗲 2024-11-26 00:22:57

你的问题非常模糊。

但这里是使用后关闭连接的示例:

f = urllib2.urlopen(req)
f.read()
f.close()

your question is extremely vague.

but here is an example of closing a connection after use:

f = urllib2.urlopen(req)
f.read()
f.close()
幸福丶如此 2024-11-26 00:22:57

一旦获得响应,套接字连接就会自动关闭。因此,您无需显式关闭 urlopen 对象,它会在套接字级别自动发生。

The socket connection closes automatically, once the response is obtained. So you don't explicitly close the urllopen object, it happens automatically at the socket level.

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