0

I am new in python (but not in php) and doing some test to learn it by myself.

When I run the code with urls in the array, I get this error:

print req[1].getValue()
AttributeError: 'cStringIO.StringO' object has no attribute 'getValue'

When I check the attributes, I can see:

['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']

When I check the

req.getValue()

I get this:

print req.getValue()
AttributeError: 'tuple' object has no attribute 'getValue'

What I am doing wrong ? Code found here: https://fragmentsofcode.wordpress.com/2011/01/22/pycurl-curlmulti-example/

    #urls = [...]  # list of urls
    # reqs: List of individual requests.
    # Each list element will be a 3-tuple of url (string), response string buffer
    # (cStringIO.StringIO), and request handle (pycurl.Curl object).
    reqs = []

    # Build multi-request object.
    m = pycurl.CurlMulti()
    for url in urls:
        response = StringIO()
        handle = pycurl.Curl()
        handle.setopt(pycurl.URL, url)
        handle.setopt(pycurl.WRITEFUNCTION, response.write)
        req = (url, response, handle)
        # Note that the handle must be added to the multi object
        # by reference to the req tuple (threading?).
        m.add_handle(req[2])
        reqs.append(req)

    # Perform multi-request.
    # This code copied from pycurl docs, modified to explicitly
    # set num_handles before the outer while loop.
    SELECT_TIMEOUT = 1.0
    num_handles = len(reqs)
    while num_handles:
        ret = m.select(SELECT_TIMEOUT)
        if ret == -1:
            continue
        while 1:
            ret, num_handles = m.perform()
            if ret != pycurl.E_CALL_MULTI_PERFORM:
                break

    # print response.getValue()

    for req in reqs:
        # req[1].getvalue() contains response content
        # print dir(req)
        print req[1].getValue()
zeflex
  • 1,487
  • 1
  • 14
  • 29
  • So take a step back up and see what `print(req)` gives. Presumably a tuple. Then you can move forward from there. – roganjosh Jun 23 '18 at 17:01
  • 2
    And Python is case sensitive. The comment and Python StringIO docs write `getvalue`, while the code (and the error) contains `getValue`. – Serge Ballesta Jun 23 '18 at 17:06
  • This is the result of the print(req) :`(u'https://example.com/api/address/xxx', , ) (u'https://example.com/api/address/xxx', , )` – zeflex Jun 23 '18 at 17:07
  • Since you're new to Python you should be learning Python 3, not Python 2. And as Serge says, `getValue` is not the same as `getvalue`. – PM 2Ring Jun 23 '18 at 17:07
  • @SergeBallesta Using getValue, gives me the same error: `AttributeError: 'cStringIO.StringO' object has no attribute 'getValue'` – zeflex Jun 23 '18 at 17:09
  • @PM2Ring I will learn both but I think learning v2 and v3 could be great instead of only one version – zeflex Jun 23 '18 at 17:10
  • Sure, but you should learn Python 3 first. If you were trying to learn English would you learn Shakespeare's English first, or modern English? – PM 2Ring Jun 23 '18 at 17:12
  • **getValue** is wrong. Python doesn't normally use names of that form. The method to retrieve the value from a `StringIO` object is [`StringIO.getvalue`](https://docs.python.org/2/library/stringio.html#StringIO.StringIO.getvalue). Also, a `tuple` certainly doesn't have a method of that name. – PM 2Ring Jun 23 '18 at 17:17
  • So is there a way to read the content of reqs loop ? – zeflex Jun 23 '18 at 17:25
  • "read the content of reqs loop" doesn't make much sense. In that code, `reqs` is a list of (url, response, handle) tuples. The `response` is a `StringIO` file-like object, and in that `for` loop you can get the contents of each response with `req[1].getvalue()`. – PM 2Ring Jun 23 '18 at 17:32
  • I was not clear maybe, I am not english native (either modern or Shakespeare :) ) I understand the logic of a loop and an array with multiple values but as I said `print req[1].getValue()` still generate the error: `AttributeError: 'cStringIO.StringO' object has no attribute 'getValue'` – zeflex Jun 23 '18 at 17:37
  • That's because there's no **StringIO.getValue** method it's **StringIO.getvalue**, the `getvalue` is all lower case. – PM 2Ring Jun 23 '18 at 17:40
  • The getvalue lowercase return no data, nothing is displayed within the print (better than an error for sure), whatever the urls I am trying to curl, any idea? – zeflex Jun 23 '18 at 18:05
  • Sorry it was because I was trying to use https url. I fixed the code for this. – zeflex Jun 23 '18 at 18:09

0 Answers0