Question

I want to write a downloader with python and I use PycURL as my library, but I got a problem. I can't get the size of the file wich I wanna download. Here is part of my code :

import pycurl
url = 'http://www.google.com'
c = pycurl.Curl()
c.setopt(c.URL, url)
print c.getinfo(c.CONTENT_LENGTH_DOWNLOAD)
c.perform()

When I test this code in python shell, it's ok but when I write it as a function and run it, it gives me -1 instead of the size. What is the problem?

(code's been edited)

Was it helpful?

Solution

From the pycurl documentation on the Curl object:

The getinfo method should not be called unless perform has been called and finished.

You're calling getinfo before you've called perform.

Here is a simplified version of your example, does this work?

import pycurl

url = 'http://www.google.com'
c = pycurl.Curl()
c.setopt(c.URL, url)
c.perform()
print c.getinfo(c.CONTENT_LENGTH_DOWNLOAD)

You should see the HTML content followed by the size.

OTHER TIPS

This answer adds the missing c.setopt(c.NOBODY, 1) and is otherwise the same as the one given some months ago:

import pycurl

c = pycurl.Curl()
c.setopt(c.URL, 'http://www.alfe.de')
c.setopt(c.NOBODY, 1)
c.perform()
c.getinfo(c.CONTENT_LENGTH_DOWNLOAD)

Calling c.setopt(c.NOBODY, 1) before calling c.perform() avoids downloading the contents of the file ("No Body", but all headers).

Try adding debug to see what happens actually. After you created curl make this:

def curl_debug(debug_type, msg):
    print("debug: %s %s" % (repr(debug_type), repr(msg)))

c.setopt(pycurl.VERBOSE, 1)
c.setopt(pycurl.DEBUGFUNCTION, curl_debug)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top