Reputation: 496
I want to write a downloader with Python and I use PycURL as my library, but I have a problem. I can't get the size of the file which I want to 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 has been edited)
Upvotes: 1
Views: 2948
Reputation: 59436
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).
Upvotes: 4
Reputation: 824
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)
Upvotes: 0
Reputation: 14962
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.
Upvotes: 2