Ejemplo n.º 1
0
def modify_domain(request, domain_id):
    domain = get_object_or_404(Domain, id=domain_id)  
    if request.method == 'POST':
        form = CheckForm(request.POST)
        if form.is_valid():
            domain_url = form.cleaned_data['domain']
            hosting = form.cleaned_data['host']
            #host = Host.objects.get(name=hosting)
            if len(Host.objects.filter(name__icontains=hosting)) >= 1:
                host = Host.objects.get(name__icontains=hosting)
                domain.url = domain_url
                domain.host = host
                domain.save()
                return redirect('/domains/')
            else:
                host_errors = "Please input correct host name!"
                return render_to_response("domains/check.html", {
                    "form":form,
                    "host_errors":host_errors
                }, context_instance=RequestContext(request))
    else:
        form = CheckForm({"domain":domain.url, "host":domain.host.name})
    return render_to_response("domains/check.html", {
            "form":form
    }, context_instance=RequestContext(request))
Ejemplo n.º 2
0
def check(request):
    if request.method == "POST":
        form = CheckForm(request.POST)
        if form.is_valid():
            domain_url = form.cleaned_data["domain"]
            hosting = form.cleaned_data["host"]
            if domain_url != "Enter your Domain Name" and hosting != "Enter your web host":
                # the host is url or string
                dot_num = len(re.findall("\\.", hosting))
                if dot_num >= 1:
                    temp_host = hosting
                    if len(re.findall("http", hosting)) > 0:
                        temp_host = temp_host[8:]
                    if len(re.findall("www\\.", hosting)) > 0:
                        temp_host = temp_host[4:]
                    host = Host.objects.filter(signup_url__icontains=temp_host)
                else:
                    host = Host.objects.filter(name__icontains=hosting)
                # verify the host against certified green hosts
                if len(host) == 1:  # green
                    # save domain information
                    return redirect(reverse("before_verify") + "?domain=" + domain_url + "&hostid=" + str(host[0].id))
                elif len(host) > 1:  # multiple host
                    host_errors = "The host name is not accurate!"
                    return render_to_response(
                        "domains/check.html",
                        {"form": form, "host_errors": host_errors},
                        context_instance=RequestContext(request),
                    )
                else:  # not find host
                    # here do IP ping
                    r = subprocess.Popen(["ping", domain_url, "-c 6"], stdout=subprocess.PIPE)
                    ip_text = r.stdout.read()
                    ipaddress = re.findall("\\((.+)\\):", ip_text)
                    hostname = re.findall("from (.+) \\(", ip_text)
                    ipstr = ""
                    server = ""
                    if len(ipaddress) > 0:
                        ipstr = ipaddress[0]
                    else:
                        ipstr = "not"
                    if len(hostname) > 0:
                        server = hostname[0]
                    else:
                        server = "not"
                    # here do whois
                    w = subprocess.Popen(["whois", domain_url], stdout=subprocess.PIPE)
                    whois_text = w.stdout.read()
                    logs = Log.objects.filter(url__icontains=domain_url)
                    if len(logs) >= 1:
                        log = logs[0]
                    else:
                        log = Log()
                    log.url = domain_url
                    log.host = hosting
                    log.ip = ipstr
                    log.server = server
                    log.whois = whois_text
                    log.save()
                    return redirect(reverse("co2_display", args=[log.id]))
    else:
        form = CheckForm()
    return render_to_response("domains/check.html", {"form": form}, context_instance=RequestContext(request))