蟒蛇鼻子和扭曲
我正在为一个函数编写一个测试,该函数使用 Twisted 从 url 下载数据(我知道twisted.web.client.getPage,但是这个添加了一些额外的功能)。不管怎样,我想使用nosetests,因为我在整个项目中都使用它,并且仅对这个特定的测试使用Twisted Trial 看起来并不合适。 所以我想做的是:
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
在 localhost:8000 上监听测试服务器。问题是我总是遇到twisted.internet.error.DNSLookupError
DNSLookupError: DNS Lookup failed: address 'localhost:8000' not found: [Errno -5] No address linked with hostname.
有没有办法我可以解决这个问题吗?有人真正使用nose.twistedtools吗?
更新:更完整的回溯
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.
更新2
我的不好,似乎在 getPage 的实现中,我正在做类似的事情:
obj = urlparse.urlparse(url) netloc = obj.netloc
当我应该传递 netloc.split(':')[0]
时,将 netloc 传递给工厂
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]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确定您的
getPage
函数正确解析 URL 吗?该错误消息似乎表明它在进行 dns 查找时同时使用主机名和端口。您说您的
getPage
类似于twisted.web.client.getPage
,但是当我在这个完整的脚本中使用它时,这对我来说效果很好:运行一个简单的 http 服务器时在我的主目录中:
鼻子测试给出以下输出:
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 totwisted.web.client.getPage
, but that works fine for me when I use it in this complete script:While running a simple http server in my home directory:
The nose test gives the following output: