1

I am trying to get started with some example code of the treq library, to little avail. While it is easy to get the status code and a few other properties of the response to the request, getting the actual text of the response is a little more difficult. The print_response function available in this example code is not present in the version that I have:

from twisted.internet.task import react
from _utils import print_response

import treq


def main(reactor, *args):
    d = treq.get('http://httpbin.org/get')
    d.addCallback(print_response)
    return d

react(main, [])

Here is the traceback:

Traceback (most recent call last):
  File "test.py", line 2, in <module>
    from _utils import print_response
ModuleNotFoundError: No module named '_utils'

I am not really sure where to go from here...any help would be greatly appreciated.

  • Why are you importing from the private module `_utils`? – Stephen Rauch Feb 14 '18 at 03:46
  • @Stephen Rauch I am just trying to make the example code work. The module was provided in the treq library and allegedly contains some function named print_response that every example I can find uses to print the response text. It seemed odd to me as well, but it's all I can find. – Spencer Perkins Feb 14 '18 at 03:49
  • The traceback you show does not match your code. Your line 2 is `from treq import get`, which can't throw the import error of `print_response` – Anthony Kong Feb 14 '18 at 04:13
  • Apologies...I have edited the post to show the correct code and traceback. This just tells me that it can't find the module, which is why I attempted to import from treq._utils (and forgot to reflect that in the original post). – Spencer Perkins Feb 14 '18 at 04:23

1 Answers1

5

Now that I look at it, that example is extremely bad, especially if you're new to twisted. Please give this a try:

import treq
from twisted.internet import defer, task

def main(reactor):
    d = treq.get('https://httpbin.org/get')
    d.addCallback(print_results)
    return d

@defer.inlineCallbacks
def print_results(response):
    content = yield response.content()
    text = yield response.text()
    json = yield response.json()

    print(content)  # raw content in bytes
    print(text)     # encoded text
    print(json)     # JSON

task.react(main)

The only thing you really have to know is that the .content(),.text(),.json() return Deferred objects that eventually return the body of the response. For this reason, you need to yield or execute callbacks.

Let's say you only want the text content, you could this:

def main(reactor):
    d = treq.get('https://httpbin.org/get')
    d.addCallback(treq.text_content)
    d.addCallback(print)    # replace print with your own callback function
    return d

The treq.content() line of functions make it easy to return only the content, if thats all you care about, and do stuff with it.

notorious.no
  • 4,919
  • 3
  • 20
  • 34