本文整理汇总了Python中twisted.internet.base.ThreadedResolver.getHostByName方法的典型用法代码示例。如果您正苦于以下问题:Python ThreadedResolver.getHostByName方法的具体用法?Python ThreadedResolver.getHostByName怎么用?Python ThreadedResolver.getHostByName使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.internet.base.ThreadedResolver
的用法示例。
在下文中一共展示了ThreadedResolver.getHostByName方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_failure
# 需要导入模块: from twisted.internet.base import ThreadedResolver [as 别名]
# 或者: from twisted.internet.base.ThreadedResolver import getHostByName [as 别名]
def test_failure(self):
"""
L{ThreadedResolver.getHostByName} returns a L{Deferred} which fires a
L{Failure} if the call to L{socket.gethostbyname} raises an exception.
"""
timeout = 30
reactor = FakeReactor()
self.addCleanup(reactor._stop)
def fakeGetHostByName(name):
raise IOError("ENOBUFS (this is a funny joke)")
self.patch(socket, 'gethostbyname', fakeGetHostByName)
failedWith = []
resolver = ThreadedResolver(reactor)
d = resolver.getHostByName("some.name", (timeout,))
self.assertFailure(d, DNSLookupError)
d.addCallback(failedWith.append)
reactor._runThreadCalls()
self.assertEqual(len(failedWith), 1)
# Make sure that any timeout-related stuff gets cleaned up.
reactor._clock.advance(timeout + 1)
self.assertEqual(reactor._clock.calls, [])
示例2: test_success
# 需要导入模块: from twisted.internet.base import ThreadedResolver [as 别名]
# 或者: from twisted.internet.base.ThreadedResolver import getHostByName [as 别名]
def test_success(self):
"""
L{ThreadedResolver.getHostByName} returns a L{Deferred} which fires
with the value returned by the call to L{socket.gethostbyname} in the
threadpool of the reactor passed to L{ThreadedResolver.__init__}.
"""
ip = "10.0.0.17"
name = "foo.bar.example.com"
timeout = 30
reactor = FakeReactor()
self.addCleanup(reactor._stop)
lookedUp = []
resolvedTo = []
def fakeGetHostByName(name):
lookedUp.append(name)
return ip
self.patch(socket, 'gethostbyname', fakeGetHostByName)
resolver = ThreadedResolver(reactor)
d = resolver.getHostByName(name, (timeout,))
d.addCallback(resolvedTo.append)
reactor._runThreadCalls()
self.assertEqual(lookedUp, [name])
self.assertEqual(resolvedTo, [ip])
# Make sure that any timeout-related stuff gets cleaned up.
reactor._clock.advance(timeout + 1)
self.assertEqual(reactor._clock.calls, [])
示例3: test_timeout
# 需要导入模块: from twisted.internet.base import ThreadedResolver [as 别名]
# 或者: from twisted.internet.base.ThreadedResolver import getHostByName [as 别名]
def test_timeout(self):
"""
If L{socket.gethostbyname} does not complete before the specified
timeout elapsed, the L{Deferred} returned by
L{ThreadedResolver.getHostByBame} fails with L{DNSLookupError}.
"""
timeout = 10
reactor = FakeReactor()
self.addCleanup(reactor._stop)
result = Queue()
def fakeGetHostByName(name):
raise result.get()
self.patch(socket, 'gethostbyname', fakeGetHostByName)
failedWith = []
resolver = ThreadedResolver(reactor)
d = resolver.getHostByName("some.name", (timeout,))
self.assertFailure(d, DNSLookupError)
d.addCallback(failedWith.append)
reactor._clock.advance(timeout - 1)
self.assertEqual(failedWith, [])
reactor._clock.advance(1)
self.assertEqual(len(failedWith), 1)
# Eventually the socket.gethostbyname does finish - in this case, with
# an exception. Nobody cares, though.
result.put(IOError("The I/O was errorful"))
示例4: test_success
# 需要导入模块: from twisted.internet.base import ThreadedResolver [as 别名]
# 或者: from twisted.internet.base.ThreadedResolver import getHostByName [as 别名]
def test_success(self):
"""
L{ThreadedResolver.getHostByName} returns a L{Deferred} which fires
with the value returned by the call to L{socket.getaddrinfo} in the
threadpool of the reactor passed to L{ThreadedResolver.__init__}.
"""
ip = "213.180.204.3"
name = "ya.ru"
timeout = 30
reactor = FakeReactor()
self.addCleanup(reactor._stop)
lookedUp = []
resolvedTo = []
def fakeGetHostByName(name, service):
lookedUp.append(name)
return (2, 1, 6, '', ('213.180.204.3', 0)), (2, 2, 17, '', ('213.180.204.3', 0)), (2, 3, 0, '', ('213.180.204.3', 0))
self.patch(socket, 'getaddrinfo', fakeGetHostByName)
resolver = ThreadedResolver(reactor)
d = resolver.getHostByName(name, (timeout,))
d.addCallback(resolvedTo.append)
reactor._runThreadCalls()
self.assertEqual(lookedUp, [name])
self.assertEqual(resolvedTo, [ip])
# Make sure that any timeout-related stuff gets cleaned up.
reactor._clock.advance(timeout + 1)
self.assertEqual(reactor._clock.calls, [])
示例5: test_real
# 需要导入模块: from twisted.internet.base import ThreadedResolver [as 别名]
# 或者: from twisted.internet.base.ThreadedResolver import getHostByName [as 别名]
def test_real(self):
name = "ya.ru"
timeout = 30
reactor = FakeReactor()
self.addCleanup(reactor._stop)
resolvedTo = []
resolver = ThreadedResolver(reactor)
d = resolver.getHostByName(name, (timeout,))
d.addCallback(resolvedTo.append)
reactor._runThreadCalls()
self.assertTrue(resolvedTo[0].startswith('77.') or
resolvedTo[0].startswith('87.') or
resolvedTo[0].startswith('93.') or
resolvedTo[0].startswith('213.'))
# Make sure that any timeout-related stuff gets cleaned up.
reactor._clock.advance(timeout + 1)
self.assertEqual(reactor._clock.calls, [])
示例6: getHostByName
# 需要导入模块: from twisted.internet.base import ThreadedResolver [as 别名]
# 或者: from twisted.internet.base.ThreadedResolver import getHostByName [as 别名]
def getHostByName(self, name, timeout=(1, 3, 11, 45)):
if name in dnscache:
return defer.succeed(dnscache[name])
d = ThreadedResolver.getHostByName(self, name, timeout)
d.addCallback(self._cache_result, name)
return d