Random website generator

I have always wanted a tool that brings you to an actually random website, so I made a very simple python script that does exactly that. 

There are 2 versions, one that directs to a random IP, and one that generates a random domain name to visit, the program tests many in parallel then opens your browser when it finds one that connects.

WEB VERSION (NOT AS GOOD)

 Method 1. By IP
An obvious problem with the IP based solution is that big hosting services own huge swarths of the IP space, so first a reverse dns lookup is done then if its amazon AWS or something it doesn't load it.

import random
import socket
import requests
import webbrowser
from multiprocessing import Process
from time import sleep

domainBlacklist = ["akamaitechnologies.com","cloudfront.net"]#,"compute.amazonaws.com"]
timeout = 5
auto_open = True

def try_site(ip):
    r = requests.get("http://"+ip, timeout=timeout)
    print("")
    print("Response from ip",ip,r.headers,r.status_code)
    if r.status_code < 400:
        if auto_open:
            webbrowser.open("http://"+ip)
        open("ips.txt", "a").write(ip+"\n")

def scan():
    while 1:
        ip = str(random.randint(0,255))+"."+str(random.randint(0,255))+"."+str(random.randint(0,255))+"."+str(random.randint(1,255))
        #print("Trying IP", ip)
        try:
            try_site(ip)
            domain = socket.gethostbyaddr(ip)[0]
            print("")
            print("Found domain:",domain,"for ip",ip)
            for dom in domainBlacklist:
                if dom == domain[len(domain)-len(dom):len(domain)]:
                    print("Domain on blacklist")
                    raise ValueError("Domain on blacklist")
                
            try_site(domain)
        except KeyboardInterrupt:
            print("X",end="", flush=True) #print("Program interrupted. Killing thread...")
            return
        except:
            print(".",end="", flush=True)
            #print("The request timed out")
      
if __name__ == '__main__':
    no_tasks = 1000
    processes = []
    for i in range(no_tasks):
        processes.append(Process(target=scan))
    for process in processes:
        process.start()
        sleep(timeout/no_tasks)

Method 2. By Domain
The domain based solution also has issues, because with a purely random system the shorter domains will come up again and again, so the generation is heavily skewed towards longer domains and a list of short domains that have already been tried is kept. You can try a terrible web version HERE, my python version is much better and configurable. (18+ Be warned that this is truly random and there's a lot of stuff on the internet)

import random
import requests
import webbrowser
import socket
import os
from multiprocessing import Process, Manager
from time import sleep

chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-"
tlds = ["com","net","ru","info","in","ir","uk","au","de","ua","ca","tr","co","jp","vn","cn","gr","fr","tk","tw","id","br","io","xyz","it","nl","pl","za","us","eu"]
ip_blacklist = {"76.223.54.146","212.227.240.134","46.62.139.140","13.248.169.48","15.197.225.128","91.195.241.232","3.33.224.147","139.162.174.209","3.33.130.190","15.197.148.33","76.223.67.189"}
server_blacklist = set()
timeout = 5
auto_open = True


def try_connect(domain, protocol="http"):
    ip = socket.gethostbyname(domain)
    if ip in ip_blacklist:
        raise RuntimeError("IP is blocked")

    r = requests.get(f"{protocol}://{domain}", timeout=timeout)
    print("Response from ip", ip, r.headers, r.status_code)

    if r.headers.get("Server", "") in server_blacklist:
        raise RuntimeError("Server on blacklist")

    if r.status_code < 400:
        if auto_open:
            webbrowser.open("http://" + domain)
        with open("ips.txt", "a") as f:
            f.write(domain + "\n")


def scan(visited):
    me = os.getpid()
    while True:
        domainlen = int(random.triangular(5, 12, 10))
        label = "".join(random.choice(chars) for _ in range(domainlen))
        domain = label + "." + tlds[int(random.triangular(0, len(tlds) - 1, 0))]

        if domainlen <= 4:
            try:
                if visited.setdefault(domain, me) != me:
                    continue
            except (OSError, EOFError, BrokenPipeError):
                return

        try:
            try_connect(domain, "http")
            print("")
        except KeyboardInterrupt:
            print("X", end="", flush=True)
            return
        except Exception:
            print(".", end="", flush=True)


if __name__ == '__main__':
    no_tasks = 100
    with Manager() as manager:
        visited = manager.dict()

        processes = [Process(target=scan, args=(visited,)) for _ in range(no_tasks)]
        try:
            for p in processes:
                p.start()
                sleep(timeout / no_tasks)
            for p in processes:
                p.join()          
        except KeyboardInterrupt:
            for p in processes:
                p.terminate()

Comments

⚠ Served by Backup Server