0

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

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

如果我使用 python 独立执行脚本,它可以工作,但是当它在本地或远程的 appengine 中运行时它会失败。堆栈跟踪:

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 查询使用 Google App Engine 套接字类似的问题,但我尝试使用参数 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')
4

1 回答 1

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=''

于 2015-09-02T12:34:49.980 回答