久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

關(guān)閉 urllib2 連接

Close urllib2 connection(關(guān)閉 urllib2 連接)
本文介紹了關(guān)閉 urllib2 連接的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在使用 urllib2 從 ftp 和 http 服務(wù)器加載文件.

某些服務(wù)器僅支持每個(gè) IP 一個(gè)連接.問題是,urllib2 不會(huì)立即關(guān)閉連接.查看示例程序.

從 urllib2 導(dǎo)入 urlopen從時(shí)間導(dǎo)入睡眠url = 'ftp://user:pass@host/big_file.ext'定義加載文件(網(wǎng)址):f = urlopen(url)加載 = 0而真:數(shù)據(jù) = f.read(1024)如果數(shù)據(jù) == '':休息已加載 += len(數(shù)據(jù))f.close()#睡眠(1)print('已加載 {0}'.format(已加載))加載文件(網(wǎng)址)加載文件(網(wǎng)址)

代碼從僅支持 1 個(gè)連接的 ftp 服務(wù)器加載兩個(gè)文件(此處兩個(gè)文件相同).這將打印以下日志:

已加載 463675266回溯(最近一次通話最后):文件conection_test.py",第 20 行,在 <module>加載文件(網(wǎng)址)文件connection_test.py",第 7 行,在 load_file 中f = urlopen(url)文件/usr/lib/python2.6/urllib2.py",第 126 行,在 urlopenreturn _opener.open(網(wǎng)址,數(shù)據(jù),超時(shí))文件/usr/lib/python2.6/urllib2.py",第 391 行,打開響應(yīng) = self._open(請求,數(shù)據(jù))_open 中的文件/usr/lib/python2.6/urllib2.py",第 409 行'_open',請求)_call_chain 中的文件/usr/lib/python2.6/urllib2.py",第 369 行結(jié)果 = 函數(shù)(*args)文件/usr/lib/python2.6/urllib2.py",第 1331 行,在 ftp_openfw = self.connect_ftp(用戶,密碼,主機(jī),端口,目錄,req.timeout)文件/usr/lib/python2.6/urllib2.py",第 1352 行,在 connect_ftpfw = ftpwrapper(用戶、密碼、主機(jī)、端口、目錄、超時(shí))__init__ 中的文件/usr/lib/python2.6/urllib.py",第 854 行self.init()文件/usr/lib/python2.6/urllib.py",第 860 行,在 initself.ftp.connect(self.host,self.port,self.timeout)文件/usr/lib/python2.6/ftplib.py",第 134 行,在連接中self.welcome = self.getresp()文件/usr/lib/python2.6/ftplib.py",第 216 行,在 getresp 中提高error_temp,respurllib2.URLError: <urlopen 錯(cuò)誤 ftp 錯(cuò)誤: 421 來自您的 Internet 地址的連接太多.>

所以第一個(gè)文件被加載,第二個(gè)文件失敗,因?yàn)榈谝粋€(gè)連接沒有關(guān)閉.

但是當(dāng)我在 f.close() 之后使用 sleep(1) 時(shí)不會(huì)發(fā)生錯(cuò)誤:

已加載 463675266已加載 463675266

有什么辦法可以強(qiáng)制關(guān)閉連接,以免第二次下載失敗?

解決方案

原因確實(shí)是文件描述符泄漏.我們還發(fā)現(xiàn),使用 jython 時(shí),問題比使用 cpython 時(shí)要明顯得多.一位同事提出了這個(gè)解決方案:

<上一頁>fdurl = urllib2.urlopen(req,timeout=self.timeout)realsock = fdurl.fp._sock.fp._sock** # 我們想稍后關(guān)閉真實(shí)"套接字req = urllib2.Request(url, header)嘗試:fdurl = urllib2.urlopen(req,timeout=self.timeout)除了 urllib2.URLError,e:打印urlopen 異常",erealsock.close()fdurl.close()

修復(fù)很丑陋,但確實(shí)有效,不再有打開的連接太多".

I'm using urllib2 to load files from ftp- and http-servers.

Some of the servers support only one connection per IP. The problem is, that urllib2 does not close the connection instantly. Look at the example-program.

from urllib2 import urlopen
from time import sleep

url = 'ftp://user:pass@host/big_file.ext'

def load_file(url):
    f = urlopen(url)
    loaded = 0
    while True:
        data = f.read(1024)
        if data == '':
            break
        loaded += len(data)
    f.close()
    #sleep(1)
    print('loaded {0}'.format(loaded))

load_file(url)
load_file(url)

The code loads two files (here the two files are the same) from an ftp-server which supports only 1 connection. This will print the following log:

loaded 463675266
Traceback (most recent call last):
  File "conection_test.py", line 20, in <module>
    load_file(url)
  File "conection_test.py", line 7, in load_file
    f = urlopen(url)
  File "/usr/lib/python2.6/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.6/urllib2.py", line 391, in open
    response = self._open(req, data)
  File "/usr/lib/python2.6/urllib2.py", line 409, in _open
    '_open', req)
  File "/usr/lib/python2.6/urllib2.py", line 369, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.6/urllib2.py", line 1331, in ftp_open
    fw = self.connect_ftp(user, passwd, host, port, dirs, req.timeout)
  File "/usr/lib/python2.6/urllib2.py", line 1352, in connect_ftp
    fw = ftpwrapper(user, passwd, host, port, dirs, timeout)
  File "/usr/lib/python2.6/urllib.py", line 854, in __init__
    self.init()
  File "/usr/lib/python2.6/urllib.py", line 860, in init
    self.ftp.connect(self.host, self.port, self.timeout)
  File "/usr/lib/python2.6/ftplib.py", line 134, in connect
    self.welcome = self.getresp()
  File "/usr/lib/python2.6/ftplib.py", line 216, in getresp
    raise error_temp, resp
urllib2.URLError: <urlopen error ftp error: 421 There are too many connections from your internet address.>

So the first file is loaded and the second fails because the first connection was not closed.

But when i use sleep(1) after f.close() the error does not occurr:

loaded 463675266
loaded 463675266

Is there any way to force close the connection so that the second download would not fail?

解決方案

The cause is indeed a file descriptor leak. We found also that with jython, the problem is much more obvious than with cpython. A colleague proposed this sollution:

 

    fdurl = urllib2.urlopen(req,timeout=self.timeout)
    realsock = fdurl.fp._sock.fp._sock** # we want to close the "real" socket later 
    req = urllib2.Request(url, header)
    try:
             fdurl = urllib2.urlopen(req,timeout=self.timeout)
    except urllib2.URLError,e:
              print "urlopen exception", e
    realsock.close() 
    fdurl.close()

The fix is ugly, but does the job, no more "too many open connections".

這篇關(guān)于關(guān)閉 urllib2 連接的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Why I cannot make an insert to Python list?(為什么我不能插入 Python 列表?)
Insert a column at the beginning (leftmost end) of a DataFrame(在 DataFrame 的開頭(最左端)插入一列)
Python psycopg2 not inserting into postgresql table(Python psycopg2 沒有插入到 postgresql 表中)
list extend() to index, inserting list elements not only to the end(list extend() 索引,不僅將列表元素插入到末尾)
How to add element in Python to the end of list using list.insert?(如何使用 list.insert 將 Python 中的元素添加到列表末尾?)
TypeError: #39;float#39; object is not subscriptable(TypeError:“浮動(dòng)對象不可下標(biāo))
主站蜘蛛池模板: 天天干天天操天天插 | 日韩中文字幕在线观看 | 精品国产乱码一区二区三 | 夜间福利视频 | 国产日韩欧美一区 | 欧美日韩在线播放 | 久久久久久免费 | 中文字幕一区二区在线播放 | 波多野一区| 毛片在线观看视频 | 日韩欧美视频在线 | 精品亚洲一区二区三区四区五区 | 97免费在线| 国产黄a三级三级三级看三级男男 | 国产a级大片 | 国产高清网站 | a级成人毛片 | a级毛毛片 | 国产一区二区在线播放 | 又大又黄又爽 | 久操国产 | 色网站在线 | 国产精品日日摸天天碰 | 日本在线网站 | 欧美特黄视频 | 最新免费黄色网址 | 亚洲精选在线观看 | 日韩精品免费看 | 欧美一区二区在线观看 | 九九热这里只有 | 成人一级片 | 在线看黄色片 | 日日夜夜天天干 | 国产高清毛片 | 亚洲国产精品视频 | 黄色一级大片 | 亚洲天天干 | 在线成人小视频 | 色交视频| 日韩二区在线 | 亚洲欧美在线播放 |