查询DNS时GAE超时

时间:2013-11-04 14:24:28

标签: python google-app-engine sockets dns

我正在尝试使用脚本来检查是否存在电子邮件。为此,我正在使用DNS查询。这是失败的调用:

from dns import resolver
mx_data = resolver.query(hostname, 'MX', source='')

如果我使用python独立执行脚本,但是当它在本地或远程的appengine中运行时,它会失败。 stacktrace:

File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1535, in __call__
  rv = self.handle_exception(request, response, e)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1529, in __call__
  rv = self.router.dispatch(request, response)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
  return route.handler_adapter(request, response)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1102, in __call__
  return handler.dispatch()
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 572, in dispatch
  return self.handle_exception(e, self.app.debug)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 570, in dispatch
  return method(*args, **kwargs)
File "/Users/user/dev/gaeapp/request.py", line 5827, in get
  check2 = email_checker.validate_email(email)
File "/Users/user/dev/gaeapp/tools/email_checker.py", line 89, in validate_email
  mx_data = resolver.query(hostname, 'MX')
File "/Users/user/dev/gaeapp/dns/resolver.py", line 974, in query
  raise_on_no_answer, source_port)
File "/Users/user/dev/gaeapp/dns/resolver.py", line 894, in query
  timeout = self._compute_timeout(start)
File "/Users/user/dev/gaeapp/dns/resolver.py", line 734, in _compute_timeout
  raise Timeout

我遇到问题DNS query using Google App Engine socket的类似问题,但我尝试使用参数source =''调用查询但没有成功。

我正在使用dnspython 1.11.1

更新:手动设置DNS解析器后,它可以正常工作:

r = resolver.Resolver()
r.nameservers = ['8.8.8.8', '8.8.4.4']
mx_data = r.query(hostname, 'MX')

1 个答案:

答案 0 :(得分:0)

正如蒂姆所说,你需要明确设置解析器。

示例代码:

import dns.resolver

resolver = dns.resolver.Resolver()
resolver.nameservers = ['8.8.8.8']
mx_data = resolver.query(hostname, 'MX')

请注意,8.8.8.8是googles dns服务器,但可以是任何其他服务器。

另请注意,您无需设置source =''