示例#1
0
 def open_local_file(self, url):
     host, file = splithost(url)
     if not host:
         return addinfourl(open(url2pathname(file), 'r'), noheaders(),
                           'file:' + file)
     host, port = splitport(host)
     if not port and socket.gethostbyname(host) in (localhost(),
                                                    thishost()):
         file = unquote(file)
         return addinfourl(open(url2pathname(file), 'r'), noheaders(),
                           'file:' + file)
     raise IOError, ('local file error', 'not on local host')
示例#2
0
 def open_local_file(self, url):
     import mimetypes, mimetools, StringIO
     mtype = mimetypes.guess_type(url)[0]
     headers = mimetools.Message(
         StringIO.StringIO('Content-Type: %s\n' % (mtype or 'text/plain')))
     host, file = splithost(url)
     if not host:
         return addinfourl(open(url2pathname(file), 'rb'), headers,
                           'file:' + file)
     host, port = splitport(host)
     if not port \
        and socket.gethostbyname(host) in (localhost(), thishost()):
         return addinfourl(open(url2pathname(file), 'rb'), headers,
                           'file:' + file)
     raise IOError, ('local file error', 'not on local host')
示例#3
0
 def open_local_file(self, url):
     import mimetypes, mimetools, StringIO
     mtype = mimetypes.guess_type(url)[0]
     headers = mimetools.Message(StringIO.StringIO(
         'Content-Type: %s\n' % (mtype or 'text/plain')))
     host, file = splithost(url)
     if not host:
         return addinfourl(open(url2pathname(file), 'rb'),
                           headers, 'file:'+file)
     host, port = splitport(host)
     if not port \
        and socket.gethostbyname(host) in (localhost(), thishost()): 
         return addinfourl(open(url2pathname(file), 'rb'),
                           headers, 'file:'+file)
     raise IOError, ('local file error', 'not on local host')
示例#4
0
 def test_url2pathname(self):
     self.assertEqual(":index.html", macurl2path.url2pathname("index.html"))
     self.assertEqual(":bar:index.html", macurl2path.url2pathname("bar/index.html"))
     self.assertEqual("foo:bar:index.html", macurl2path.url2pathname("/foo/bar/index.html"))
     self.assertEqual("foo:bar", macurl2path.url2pathname("/foo/bar/"))
     self.assertEqual("", macurl2path.url2pathname("/"))
     self.assertRaises(RuntimeError, macurl2path.url2pathname, "http://foo.com")
     self.assertEqual("index.html", macurl2path.url2pathname("///index.html"))
     self.assertRaises(RuntimeError, macurl2path.url2pathname, "//index.html")
     self.assertEqual(":index.html", macurl2path.url2pathname("./index.html"))
     self.assertEqual(":index.html", macurl2path.url2pathname("foo/../index.html"))
     self.assertEqual("::index.html", macurl2path.url2pathname("../index.html"))
示例#5
0
文件: urllib.py 项目: Nevisor/Client
 def open_local_file(self, url):
     """Use local file."""
     import mimetypes, mimetools, rfc822, StringIO
     host, file = splithost(url)
     localname = url2pathname(file)
     stats = os.stat(localname)
     size = stats[stat.ST_SIZE]
     modified = rfc822.formatdate(stats[stat.ST_MTIME])
     mtype = mimetypes.guess_type(url)[0]
     headers = mimetools.Message(StringIO.StringIO(
         'Content-Type: %s\nContent-Length: %d\nLast-modified: %s\n' %
         (mtype or 'text/plain', size, modified)))
     if not host:
         urlfile = file
         if file[:1] == '/':
             urlfile = 'file://' + file
         return addinfourl(open(localname, 'rb'),
                           headers, urlfile)
     host, port = splitport(host)
     if not port \
        and socket.gethostbyname(host) in (localhost(), thishost()):
         urlfile = file
         if file[:1] == '/':
             urlfile = 'file://' + file
         return addinfourl(open(localname, 'rb'),
                           headers, urlfile)
     raise IOError, ('local file error', 'not on local host')
示例#6
0
 def open_local_file(self, url):
     """Use local file."""
     import mimetypes, mimetools, rfc822, StringIO
     host, file = splithost(url)
     localname = url2pathname(file)
     stats = os.stat(localname)
     size = stats[stat.ST_SIZE]
     modified = rfc822.formatdate(stats[stat.ST_MTIME])
     mtype = mimetypes.guess_type(url)[0]
     headers = mimetools.Message(StringIO.StringIO(
         'Content-Type: %s\nContent-Length: %d\nLast-modified: %s\n' %
         (mtype or 'text/plain', size, modified)))
     if not host:
         urlfile = file
         if file[:1] == '/':
             urlfile = 'file://' + file
         return addinfourl(open(localname, 'rb'),
                           headers, urlfile)
     host, port = splitport(host)
     if not port \
        and socket.gethostbyname(host) in (localhost(), thishost()):
         urlfile = file
         if file[:1] == '/':
             urlfile = 'file://' + file
         return addinfourl(open(localname, 'rb'),
                           headers, urlfile)
     raise IOError, ('local file error', 'not on local host')
示例#7
0
 def open_local_file(self, url):
     import mimetypes, mimetools, rfc822, StringIO
     host, file = splithost(url)
     localname = url2pathname(file)
     try:
         stats = os.stat(localname)
     except OSError, e:
         raise IOError(e.errno, e.strerror, e.filename)
示例#8
0
 def open_local_file(self, url):
     """Use local file."""
     import mimetypes, mimetools, rfc822, StringIO
     host, file = splithost(url)
     localname = url2pathname(file)
     try:
         stats = os.stat(localname)
     except OSError, e:
         raise IOError(e.errno, e.strerror, e.filename)
 def open_local_file(self, url):
     """Use local file."""
     import mimetypes, mimetools, email.Utils
     try:
         from cStringIO import StringIO
     except ImportError:
         from StringIO import StringIO
     host, file = splithost(url)
     localname = url2pathname(file)
     try:
         stats = os.stat(localname)
     except OSError, e:
         raise IOError(e.errno, e.strerror, e.filename)
示例#10
0
 def retrieve(self, url, filename=None, reporthook=None):
     url = unwrap(url)
     if self.tempcache and self.tempcache.has_key(url):
         return self.tempcache[url]
     type, url1 = splittype(url)
     if not filename and (not type or type == 'file'):
         try:
             fp = self.open_local_file(url1)
             hdrs = fp.info()
             del fp
             return url2pathname(splithost(url1)[1]), hdrs
         except IOError, msg:
             pass
示例#11
0
 def retrieve(self, url, filename=None, reporthook=None):
     url = unwrap(url)
     if self.tempcache and self.tempcache.has_key(url):
         return self.tempcache[url]
     type, url1 = splittype(url)
     if not filename and (not type or type == 'file'):
         try:
             fp = self.open_local_file(url1)
             hdrs = fp.info()
             del fp
             return url2pathname(splithost(url1)[1]), hdrs
         except IOError, msg:
             pass
示例#12
0
 def test_url2pathname(self):
     self.assertEqual(":index.html", macurl2path.url2pathname("index.html"))
     self.assertEqual(":bar:index.html",
                      macurl2path.url2pathname("bar/index.html"))
     self.assertEqual("foo:bar:index.html",
                      macurl2path.url2pathname("/foo/bar/index.html"))
     self.assertEqual("foo:bar", macurl2path.url2pathname("/foo/bar/"))
     self.assertEqual("", macurl2path.url2pathname("/"))
     self.assertRaises(RuntimeError, macurl2path.url2pathname,
                       "http://foo.com")
     self.assertEqual("index.html",
                      macurl2path.url2pathname("///index.html"))
     self.assertRaises(RuntimeError, macurl2path.url2pathname,
                       "//index.html")
     self.assertEqual(":index.html",
                      macurl2path.url2pathname("./index.html"))
     self.assertEqual(":index.html",
                      macurl2path.url2pathname("foo/../index.html"))
     self.assertEqual("::index.html",
                      macurl2path.url2pathname("../index.html"))
 def test_url2pathname(self):
     self.assertEqual(':index.html', macurl2path.url2pathname('index.html'))
     self.assertEqual(':bar:index.html', macurl2path.url2pathname(
         'bar/index.html'))
     self.assertEqual('foo:bar:index.html', macurl2path.url2pathname(
         '/foo/bar/index.html'))
     self.assertEqual('foo:bar', macurl2path.url2pathname('/foo/bar/'))
     self.assertEqual('', macurl2path.url2pathname('/'))
     self.assertRaises(RuntimeError, macurl2path.url2pathname,
         'http://foo.com')
     self.assertEqual('index.html', macurl2path.url2pathname(
         '///index.html'))
     self.assertRaises(RuntimeError, macurl2path.url2pathname,
         '//index.html')
     self.assertEqual(':index.html', macurl2path.url2pathname(
         './index.html'))
     self.assertEqual(':index.html', macurl2path.url2pathname(
         'foo/../index.html'))
     self.assertEqual('::index.html', macurl2path.url2pathname(
         '../index.html'))
示例#14
0
 def retrieve(self, url, filename=None, reporthook=None, data=None):
     """retrieve(url) returns (filename, None) for a local object
     or (tempfilename, headers) for a remote object."""
     url = unwrap(toBytes(url))
     if self.tempcache and self.tempcache.has_key(url):
         return self.tempcache[url]
     type, url1 = splittype(url)
     if not filename and (not type or type == 'file'):
         try:
             fp = self.open_local_file(url1)
             hdrs = fp.info()
             del fp
             return url2pathname(splithost(url1)[1]), hdrs
         except IOError, msg:
             pass
示例#15
0
 def retrieve(self, url, filename=None, reporthook=None, data=None):
     """retrieve(url) returns (filename, headers) for a local object
     or (tempfilename, headers) for a remote object."""
     url = unwrap(toBytes(url))
     if self.tempcache and self.tempcache.has_key(url):
         return self.tempcache[url]
     type, url1 = splittype(url)
     if not filename and (not type or type == 'file'):
         try:
             fp = self.open_local_file(url1)
             hdrs = fp.info()
             del fp
             return url2pathname(splithost(url1)[1]), hdrs
         except IOError, msg:
             pass
示例#16
0
 def retrieve(self, url, filename=None):
     if self.tempcache and self.tempcache.has_key(url):
         return self.tempcache[url]
     url1 = unwrap(url)
     self.openedurl = url1
     if self.tempcache and self.tempcache.has_key(url1):
         self.tempcache[url] = self.tempcache[url1]
         return self.tempcache[url1]
     type, url1 = splittype(url1)
     if not filename and (not type or type == 'file'):
         try:
             fp = self.open_local_file(url1)
             del fp
             return url2pathname(splithost(url1)[1]), None
         except IOError, msg:
             pass
示例#17
0
    def open_local_file(self, url):
        import mimetypes as mimetypes
        import mimetools as mimetools
        import email.Utils as email
        import StringIO as StringIO
        (host, file) = splithost(url)
        localname = url2pathname(file)
        
        try:
            stats = os.stat(localname)
        except OSError:
            e = None
            raise IOError(e.errno, e.strerror, e.filename)

        size = stats.st_size
        modified = email.Utils.formatdate(stats.st_mtime, usegmt = True)
        mtype = mimetypes.guess_type(url)[0]
        if not mtype:
            pass
        headers = mimetools.Message(StringIO.StringIO('Content-Type: %s\nContent-Length: %d\nLast-modified: %s\n' % ('text/plain', size, modified)))
        if not host:
            urlfile = file
            if file[:1] == '/':
                urlfile = 'file://' + file
            
            return addinfourl(open(localname, 'rb'), headers, urlfile)
        
        (host, port) = splitport(host)
        if not port and socket.gethostbyname(host) in (localhost(), thishost()):
            urlfile = file
            if file[:1] == '/':
                urlfile = 'file://' + file
            
            return addinfourl(open(localname, 'rb'), headers, urlfile)
        
        raise IOError, ('local file error', 'not on local host')
示例#18
0
"""Open an arbitrary URL.
示例#19
0
'''
macurl2path 模块

(功能实现模块) macurl2path 模块用于 URL 和 Macintosh 文件名的相互映射.
一般没有必要直接使用它, 请使用 urllib 中的机制.
'''
import macurl2path

file = ':my:little:pony'

print(macurl2path.pathname2url(file))
print(macurl2path.url2pathname(macurl2path.pathname2url(file)))
'''
my/little/pony
:my:little:pony
[Finished in 0.3s]
'''
示例#20
0
"""Open an arbitrary URL.
示例#21
0
    def retrieve(self, url, filename = None, reporthook = None, data = None):
        url = unwrap(toBytes(url))
        if self.tempcache and url in self.tempcache:
            return self.tempcache[url]
        
        (type, url1) = splittype(url)
        if filename is None:
            if not type or type == 'file':
                
                try:
                    fp = self.open_local_file(url1)
                    hdrs = fp.info()
                    del fp
                    return (url2pathname(splithost(url1)[1]), hdrs)
                except IOError:
                    msg = None
                

        fp = self.open(url, data)
        headers = fp.info()
        if filename:
            tfp = open(filename, 'wb')
        else:
            import tempfile as tempfile
            (garbage, path) = splittype(url)
            if not path:
                pass
            (garbage, path) = splithost('')
            if not path:
                pass
            (path, garbage) = splitquery('')
            if not path:
                pass
            (path, garbage) = splitattr('')
            suffix = os.path.splitext(path)[1]
            (fd, filename) = tempfile.mkstemp(suffix)
            self._URLopener__tempfiles.append(filename)
            tfp = os.fdopen(fd, 'wb')
        result = (filename, headers)
        if self.tempcache is not None:
            self.tempcache[url] = result
        
        bs = 1024 * 8
        size = -1
        blocknum = 1
        if reporthook:
            if 'content-length' in headers:
                size = int(headers['Content-Length'])
            
            reporthook(0, bs, size)
        
        block = fp.read(bs)
        if reporthook:
            reporthook(1, bs, size)
        
        while block:
            tfp.write(block)
            block = fp.read(bs)
            blocknum = blocknum + 1
            if reporthook:
                reporthook(blocknum, bs, size)
                continue
        fp.close()
        tfp.close()
        del fp
        del tfp
        return result