蟒蛇鼻子和扭曲

发布于 2024-09-25 19:54:04 字数 1403 浏览 2 评论 0原文

我正在为一个函数编写一个测试,该函数使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七婞 2024-10-02 19:54:04

您确定您的 getPage 函数正确解析 URL 吗?该错误消息似乎表明它在进行 dns 查找时同时使用主机名和端口。

您说您的 getPage 类似于 twisted.web.client.getPage,但是当我在这个完整的脚本中使用它时,这对我来说效果很好:

#!/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)

运行一个简单的 http 服务器时在我的主目录中:

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

鼻子测试给出以下输出:

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

OK

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文