0

I'm having basic python issues.. In the following example no errors are returned but displaying the contents of all variables using pprint shows that contents is = '' -- why would this possibly be the case?

import sys, os, re, StringIO, pprint, time
from BeautifulSoup import BeautifulSoup, BeautifulStoneSoup
import pycurl

url = "http://google.com/";

strio = StringIO.StringIO()

curlobj = pycurl.Curl()
curlobj.setopt(pycurl.URL, url)
curlobj.perform()
curlobj.close()

contents = strio.getvalue()
strio.close()

Any ideas? Thanks

casperOne
  • 73,706
  • 19
  • 184
  • 253
Alexander Cameron
  • 309
  • 1
  • 4
  • 11

2 Answers2

5

Look at the lines that involve StringIO.

strio = StringIO.StringIO()
contents = strio.getvalue()
strio.close()

None of these statements draw content from curlobj. So strio is empty.


Edit (thanks to @Alexander Cameron and @agf):

Perhaps you meant

curlobj.setopt(pycurl.WRITEFUNCTION, strio.write)    
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • Thanks! Looks like strio = StringIO.StringIO() curlobj = pycurl.Curl() curlobj.setopt(pycurl.URL, url) curlobj.setopt(pycurl.WRITEFUNCTION, strio.write) curlobj.perform() curlobj.close() contents = strio.getvalue() strio.close() is the way to do it. – Alexander Cameron Aug 25 '11 at 12:50
  • 1
    or `curlobj.setopt(pycurl.WRITEFUNCTION, strio.write)` – agf Aug 25 '11 at 12:50
2

You never do anything with your strio variable. You have to pass it in to some function in order for anything to get written to it.

Gabe
  • 84,912
  • 12
  • 139
  • 238
  • Thanks! Looks like strio = StringIO.StringIO() curlobj = pycurl.Curl() curlobj.setopt(pycurl.URL, url) curlobj.setopt(pycurl.WRITEFUNCTION, strio.write) curlobj.perform() curlobj.close() contents = strio.getvalue() strio.close() is the way to do it. – Alexander Cameron Aug 25 '11 at 12:50