@@ -0,0 +1,1696 @@
+#!/usr/bin/env python%0A# -*- coding: utf-8 -*-%0A%0Aimport dns.resolver%0Aimport dns.reversename%0Aimport json%0Aimport urlparse%0A%0A%0Adef resolve_mx(hostname):%0A    response = %5B%5D%0A    for data in dns.resolver.query(hostname, 'MX'):%0A        response.append((data.exchange.to_text(), data.preference))%0A    return response%0A%0A%0Adef resolve_ip(hostname):%0A    response = %5B%5D%0A    for data in dns.resolver.query(hostname, 'A'):%0A        response.append(data.address)%0A    return response%0A%0A%0Adef resolve_ptr(ip):%0A    try:%0A        for data in dns.resolver.query(dns.reversename.from_address(ip), 'PTR'):%0A            return data.target.to_text()%0A    except dns.resolver.NXDOMAIN:%0A        pass%0A    return ''%0A%0A%0Adef check_mx(domainname):%0A    mails = resolve_mx(domainname)%0A    in_all = True%0A    in_any = False%0A    response = %5B%5D%0A    for mx in mails:%0A        ips = %5B%5D%0A        for ip in resolve_ip(mx%5B0%5D):%0A            ptr = resolve_ptr(ip)%0A            host_in = ptr == mx%5B0%5D%0A            if host_in:%0A                in_any = True%0A            else:%0A                in_all = False%0A            ips.append(%7B'ip': ip, 'ptr': ptr, 'check': host_in%7D)%0A        response.append(%7B'name': mx%5B0%5D, 'preference': mx%5B1%5D, 'ips': ips%7D)%0A    return %7B'domain': domainname, 'mx': response, 'all': in_all, 'any': in_any%7D%0A%0A%0Adef application(environ, start_response):%0A    query = urlparse.parse_qs(environ.get('QUERY_STRING', ''))%0A    status = '200 OK'%0A    response_headers = %5B('Content-type', 'application/json')%5D%0A    try:%0A        content = check_mx(query%5B'domain'%5D%5B0%5D)%0A    except Exception as e:%0A        status = '400 ERROR'%0A        content = %7B'message': e.message%7D%0A    start_response(status, response_headers)%0A    return %5Bjson.dumps(content).encode('utf-8')%5D%0A

Add API for check MX