views:

84

answers:

1

I am writing a test for a function that downloads the data from an url with Twisted (I know about twisted.web.client.getPage, but this one adds some extra functionality). Either ways, I want to use nosetests since I am using it throughout the project and it doesn't look appropriate to use Twisted Trial only for this particular test. So what I am trying to do is something like:

from nose.twistedtools import deferred

@deferred()
def test_download(self):
    url = 'http://localhost:8000'

    d = getPage(url)

    def callback(data):
        assert len(data) != 0

    d.addCallback(callback)
    return d

On localhost:8000 listens a test server. The issue is I always get twisted.internet.error.DNSLookupError

DNSLookupError: DNS lookup failed: address 'localhost:8000' not found: [Errno -5] No address associated with hostname.

Is there a way I can fix this? Does anyone actually uses nose.twistedtools?

Update: A more complete traceback

Traceback (most recent call last):
File "/usr/local/lib/python2.6/dist-packages/nose-0.11.2-py2.6.egg/nose/twistedtools.py", line 138, in errback
failure.raiseException()
File "/usr/local/lib/python2.6/dist-packages/Twisted-9.0.0-py2.6-linux-x86_64.egg/twisted/python/failure.py", line 326, in raiseException
raise self.type, self.value, self.tb
DNSLookupError: DNS lookup failed: address 'localhost:8000' not found: [Errno -5] No address associated with hostname.

Update 2

My bad, it seems in the implementation of getPage, I was doing something like:

obj = urlparse.urlparse(url) netloc = obj.netloc and passing netloc to the the factory when I should've passed netloc.split(':')[0]

+2  A: 

Are you sure your getPage function is parsing the URL correctly? The error message seems to suggest that it is using the hostname and port together when doing the dns lookup.

You say your getPage is similar to twisted.web.client.getPage, but that works fine for me when I use it in this complete script:

#!/usr/bin/env python
from nose.twistedtools import deferred
from twisted.web import client
import nose

@deferred()
def test_download():
    url = 'http://localhost:8000'

    d = client.getPage(url)

    def callback(data):
        assert len(data) != 0

    d.addCallback(callback)
    return d

if __name__ == "__main__":
    args = ['--verbosity=2', __file__]
    nose.run(argv=args)

While running a simple http server in my home directory:

$ python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...

The nose test gives the following output:

.
----------------------------------------------------------------------
Ran 1 test in 0.019s

OK
Day
You are right, it looks like it's using both the hostname and port to do the lookup, but I didn't ask for this. I am not doing any lookup in getPage, the stack trace is coming from nose.
hyperboreean
@hyperboreean Well the URL is passed to getPage, and it presumably uses that somehow to connect to the server. What does your getPage do with the URL it is given exactly?
Day
Ah I see you edited the question... thought it might be something like that. Glad you got it fixed ;)
Day