urllib.getsize()

Gerrit Holl gerrit.holl at pobox.com
Thu Nov 18 15:39:25 EST 1999


Hello,

I've written a function to get the size of a HTTP/FTP url, is it good enough
to be put in urllib.py?

def getsize(url):
        import urlparse
        urltup = urlparse.urlparse(url)

        protocol = urltup[0]
        host = urltup[1]
        file = urltup[2]

        if protocol == HTTP:
                import httplib
                http = httplib.HTTP(host) # www.nl.linux.org:80 mag ook
                http.putrequest("HEAD", file)
                http.putheader("Accept", "*/*")
                http.endheaders()
                return http.getreply()[2].get("Content-Length")
        elif protocol == FTP:
                import ftplib
                ftp = ftplib.FTP(host) # ftp.nl.linux.org:1234 mag ook
                ftp.login()
                return ftp.size(file)
	else:
		return -1

Maybe there should be some more error checking...

regards,
Gerrit.

-- 
"I'd crawl over an acre of 'Visual This++' and 'Integrated Development
That' to get to gcc, Emacs, and gdb.  Thank you."
(By Vance Petree, Virginia Power)




More information about the Python-list mailing list