示例#1
0
class FTPHandler(urllib2.FTPHandler):
    def ftp_open(self, req):
        from eventlib.green import ftplib
        import mimetypes
        host = req.get_host()
        if not host:
            raise IOError, ('ftp error', 'no host given')
        host, port = splitport(host)
        if port is None:
            port = ftplib.FTP_PORT
        else:
            port = int(port)

        # username/password handling
        user, host = splituser(host)
        if user:
            user, passwd = splitpasswd(user)
        else:
            passwd = None
        host = unquote(host)
        user = unquote(user or '')
        passwd = unquote(passwd or '')

        try:
            host = socket.gethostbyname(host)
        except socket.error, msg:
            raise URLError(msg)
        path, attrs = splitattr(req.get_selector())
        dirs = path.split('/')
        dirs = map(unquote, dirs)
        dirs, file = dirs[:-1], dirs[-1]
        if dirs and not dirs[0]:
            dirs = dirs[1:]
        try:
            fw = self.connect_ftp(user, passwd, host, port, dirs)
            type = file and 'I' or 'D'
            for attr in attrs:
                attr, value = splitvalue(attr)
                if attr.lower() == 'type' and \
                   value in ('a', 'A', 'i', 'I', 'd', 'D'):
                    type = value.upper()
            fp, retrlen = fw.retrfile(file, type)
            headers = ""
            mtype = mimetypes.guess_type(req.get_full_url())[0]
            if mtype:
                headers += "Content-type: %s\n" % mtype
            if retrlen is not None and retrlen >= 0:
                headers += "Content-length: %d\n" % retrlen
            sf = StringIO(headers)
            headers = mimetools.Message(sf)
            return addinfourl(fp, headers, req.get_full_url())
        except ftplib.all_errors, msg:
            raise IOError, ('ftp error', msg), sys.exc_info()[2]
示例#2
0
 def gopher_open(self, req):
     # XXX can raise socket.error
     from eventlib.green import gopherlib  # this raises DeprecationWarning in 2.5
     host = req.get_host()
     if not host:
         raise GopherError('no host given')
     host = unquote(host)
     selector = req.get_selector()
     type, selector = splitgophertype(selector)
     selector, query = splitquery(selector)
     selector = unquote(selector)
     if query:
         query = unquote(query)
         fp = gopherlib.send_query(selector, query, host)
     else:
         fp = gopherlib.send_selector(selector, host)
     return addinfourl(fp, noheaders(), req.get_full_url())
示例#3
0
 def open_local_file(self, req):
     import email.Utils
     import mimetypes
     host = req.get_host()
     file = req.get_selector()
     localfile = url2pathname(file)
     stats = os.stat(localfile)
     size = stats.st_size
     modified = email.Utils.formatdate(stats.st_mtime, usegmt=True)
     mtype = mimetypes.guess_type(file)[0]
     headers = mimetools.Message(StringIO(
         'Content-type: %s\nContent-length: %d\nLast-modified: %s\n' %
         (mtype or 'text/plain', size, modified)))
     if host:
         host, port = splitport(host)
     if not host or \
        (not port and socket.gethostbyname(host) in self.get_names()):
         return addinfourl(open(localfile, 'rb'),
                           headers, 'file:'+file)
     raise URLError('file not on local host')