faqts : Computers : Programming : Languages : Python : Snippets

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

1 of 2 people (50%) answered Yes
Recently 0 of 1 people (0%) answered Yes

Entry

urllib.getsize()

Nov 26th, 2004 15:23
Kyle Brooks, Nathan Wallace, Hans Nowak, Snippet 284, Gerrit Holl


"""
Packages: networking.internet
"""
"""
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
"""
Added by Kyle Brooks:
     No.
"""