How do I 'stat' online files?

Carsten Haese carsten at uniqsys.com
Tue Jul 24 09:47:16 EDT 2007


On Tue, 2007-07-24 at 09:07 -0400, DB Daniel Brown wrote:
> I am working on a program that needs to stat files (gif, swf, xml,
> dirs, etc) from the web. I know how to stat a local file…
> 
> 
> import os
> tplStat = os.stat(path)
> 
>  
> 
> but I can’t figure out how to stat a file that resides on a web
> server.

You can't stat a file on a web server.

>  I am not sure if it makes a difference, but most (maybe all) of the
> files that I need to stat reside within the same domain that will
> generate the request.

As long as you use HTTP to get to the file, that makes no difference. If
you can get to the file via NFS or SMB, that would help.

>  I am able to open the file by using
> 
>  
> 
> import urllib
> 
> f = urllib.urlopen(url)
> 
>  
> 
> but for some reason I cannot stat the files.

That's because urlopen returns a file-like object, not a file. The best
you can hope for is to inspect the headers that the web server returns:

>>> import urllib
>>> f = urllib.urlopen("http://www.python.org")
>>> f.headers['last-modified']
'Mon, 23 Jul 2007 20:35:52 GMT'
>>> f.headers.items()
[('content-length', '14053'), ('accept-ranges', 'bytes'), ('server',
'Apache/2.2.3 (Debian) DAV/2 SVN/1.4.2 mod_ssl/2.2.3 OpenSSL/0.9.8c'),
('last-modified', 'Mon, 23 Jul 2007 20:35:52 GMT'), ('connection',
'close'), ('etag', '"60193-36e5-39089a00"'), ('date', 'Tue, 24 Jul 2007
13:42:57 GMT'), ('content-type', 'text/html')]

Maybe that's good enough for your needs.

HTH,

-- 
Carsten Haese
http://informixdb.sourceforge.net





More information about the Python-list mailing list