From 7915c0f51c9b7556d6af1de9582f68e98f473799 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Sun, 6 Jul 2014 16:13:51 +0400 Subject: [PATCH] Handle ftp - get welcome message --- Robots/bkmk_rurllib2.py | 28 ++++++++++++++++++++++++---- doc/TODO | 2 -- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/Robots/bkmk_rurllib2.py b/Robots/bkmk_rurllib2.py index 49c5568..d9c9149 100644 --- a/Robots/bkmk_rurllib2.py +++ b/Robots/bkmk_rurllib2.py @@ -17,9 +17,19 @@ import urllib2 from Robots.bkmk_robot_base import robot_base, get_error +_fw = None + +class FTPHandler(urllib2.FTPHandler): + def connect_ftp(self, user, passwd, host, port, dirs, timeout): + global _fw + _fw = urllib2.FTPHandler.connect_ftp(self, user, passwd, host, port, + dirs, timeout) + return _fw + + opener = urllib2.OpenerDirector() default_classes = [urllib2.HTTPHandler, urllib2.HTTPDefaultErrorHandler, - urllib2.FTPHandler, urllib2.HTTPErrorProcessor] + FTPHandler, urllib2.HTTPErrorProcessor] if hasattr(httplib, 'HTTPS'): default_classes.insert(0, urllib2.HTTPSHandler) for klass in default_classes: @@ -35,7 +45,7 @@ _x_user_agent = "bookmarks_db (Python %d.%d.%d; urllib2)" % ( class robot_urllib2(robot_base): - def get(self, bookmark, url, accept_charset=False): + def get(self, bookmark, url, accept_charset=False): request = urllib2.Request(url) request.add_header('Accept', '*/*') if accept_charset and bookmark.charset: @@ -47,27 +57,37 @@ class robot_urllib2(robot_base): request.add_header('User-agent', _user_agent) request.add_header('X-User-Agent', _x_user_agent) + global _fw + _fw = None + try: response = urllib2.urlopen(request) + except urllib2.HTTPError, e: if e.code in (301, 302, 303, 307): return None, e.code, e.hdrs['Location'], None, None else: self.log(' HTTP Error %s: %s' % (e.code, e.msg)) return "HTTP Error %s: %s" % (e.code, e.msg), None, None, None, None + except urllib2.URLError, e: self.log(' URL Error: %s' % e.reason) return "URL Error: %s" % e.reason, None, None, None, None + except httplib.HTTPException, e: error = get_error(e) self.log(' HTTP Exception: %s' % error) return "HTTP Exception: %s" % error, None, None, None, None + except IOError, e: error = get_error(e) self.log(' I/O Error: %s' % error) return "I/O Error: %s" % error, None, None, None, None + else: return None, None, None, response.info(), response.read() - def get_ftp_welcome(self): - return '' + def get_ftp_welcome(self): + if _fw is None: + return '' + return _fw.ftp.welcome diff --git a/doc/TODO b/doc/TODO index 7200899..7e98a6e 100644 --- a/doc/TODO +++ b/doc/TODO @@ -1,5 +1,3 @@ -Robot based on urllib2: handle ftp (welcome message). - A new robot based on PycURL. HTML parser based on BeautifulSoup4. -- 2.39.2