<menu id="guoca"></menu>
<nav id="guoca"></nav><xmp id="guoca">
  • <xmp id="guoca">
  • <nav id="guoca"><code id="guoca"></code></nav>
  • <nav id="guoca"><code id="guoca"></code></nav>

    為什么要搭建 ip 地址池,如何搭建

    為什么要搭建ip 地址池,如何搭建?


    發現錯別字 2年前 提問
    回答
    1
    瀏覽
    409
    請勿發布不友善或者負能量的內容。與人為善,比聰明更重要!
    回答數量: 1

    建立ip地址池的目的是為了在滲透測試中防止真實ip地址被禁止,從而導致無法進行接下來的滲透測試。我們搭建好一個ip地址池,接下來所有的測試需要使用ip地址的時候直接從地址池中調取一個,就算是被禁封了也不會影響接下來的滲透測試流程。接下來說一下怎么搭建。

    建立ip地址池就是自己去網上收集公開的免費ip地址,自己建立起ip地址代理池,就是通過 python 程序去抓取網上大量免費的代理 ip ,然后定時的去檢測這些 ip 可不可以用,那么下次你要使用代理 ip 的時候,你只需要去自己的 ip 代理池里面拿就行了。我這里給你推薦一個我以前找到的西刺代理的多線程ip代理爬蟲可能有點長,前提說明我沒有實際用過可能會存在bug什么的如果出現什么問題我們一起討論,如果你覺得有用那就拿走吧。

    #!/usr/bin/python3
    
    #@Readme : IP代理==模擬一個ip地址去訪問某個網站(爬的次數太多,ip被屏蔽)
    
    # 多線程的方式構造ip代理池。
    from bs4 import BeautifulSoup
    import requests
    from urllib import request, error
    import threading
    
    import os
    from fake_useragent import UserAgent
    
    
    inFile = open('proxy.txt')           # 存放爬蟲下來的ip
    verifiedtxt = open('verified.txt')  # 存放已證實的可用的ip
    
    
    
    lock = threading.Lock()
    
    def getProxy(url):
        # 打開我們創建的txt文件
        proxyFile = open('proxy.txt', 'a')
    
        # 偽裝
        ua = UserAgent()
        headers = {
            'User-Agent': ua.random
        }
    
        # page是我們需要獲取多少頁的ip,這里我們獲取到第9頁
        for page in range(1, 10):
            # 通過觀察URL,我們發現原網址+頁碼就是我們需要的網址了,這里的page需要轉換成str類型
            urls = url + str(page)
            # 通過requests來獲取網頁源碼
            rsp = requests.get(urls, headers=headers)
            html = rsp.text
            # 通過BeautifulSoup,來解析html頁面
            soup = BeautifulSoup(html,'html.parser')
            # 通過分析我們發現數據在 id為ip_list的table標簽中的tr標簽中
            trs = soup.find('table', id='ip_list').find_all('tr')  # 這里獲得的是一個list列表
            # 我們循環這個列表
            for item in trs[1:]:
                # 并至少出每個tr中的所有td標簽
                tds = item.find_all('td')
                # 我們會發現有些img標簽里面是空的,所以這里我們需要加一個判斷
                if tds[0].find('img') is None:
                    nation = '未知'
                    locate = '未知'
                else:
                    nation = tds[0].find('img')['alt'].strip()
                    locate = tds[3].text.strip()
                # 通過td列表里面的數據,我們分別把它們提取出來
                ip = tds[1].text.strip()
                port = tds[2].text.strip()
                anony = tds[4].text.strip()
                protocol = tds[5].text.strip()
                speed = tds[6].find('div')['title'].strip()
                time = tds[8].text.strip()
                # 將獲取到的數據按照規定格式寫入txt文本中,這樣方便我們獲取
                proxyFile.write('%s|%s|%s|%s|%s|%s|%s|%s\n' % (nation, ip, port, locate, anony, protocol, speed, time))
    
    
    def verifyProxyList():
        verifiedFile = open('verified.txt', 'a')
    
        while True:
            lock.acquire()
            ll = inFile.readline().strip()
            lock.release()
            if len(ll) == 0: break
            line = ll.strip().split('|')
            ip = line[1]
            port = line[2]
            realip = ip + ':' + port
            code = verifyProxy(realip)
            if code == 200:
                lock.acquire()
                print("---Success成功:" + ip + ":" + port)
                verifiedFile.write(ll + "\n")
                lock.release()
            else:
                print("---Failure失敗:" + ip + ":" + port)
    
    
    def verifyProxy(ip):
        '''
        驗證代理的有效性
        '''
        ua = UserAgent()
        requestHeader = {
            'User-Agent': ua.random
        }
        url = "http://www.baidu.com"
        # 填寫代理地址
        proxy = {'http': ip}
        # 創建proxyHandler
        proxy_handler = request.ProxyHandler(proxy)
        # 創建opener
        proxy_opener = request.build_opener(proxy_handler)
        # 安裝opener
        request.install_opener(proxy_opener)
    
        try:
            req = request.Request(url, headers=requestHeader)
            rsq = request.urlopen(req, timeout=5.0)
            code = rsq.getcode()
            return code
        except error.URLError as e:
            return e
    
    
    if __name__ == '__main__':
        # 手動新建兩個文件
        filename = 'proxy.txt'
        filename2 = 'verified.txt'
        if not os.path.isfile(filename):
            inFile = open(filename, mode="w", encoding="utf-8")
        if not os.path.isfile(filename2):
            verifiedtxt = open(filename2, mode="w", encoding="utf-8")
        tmp = open('proxy.txt', 'w')
        tmp.write("")
        tmp.close()
        tmp1 = open('verified.txt', 'w')
        tmp1.write("")
        tmp1.close()
        # 多線程爬蟲西刺代理網,找可用ip
        getProxy("http://www.xicidaili.com/nn/")
        getProxy("http://www.xicidaili.com/nt/")
        getProxy("http://www.xicidaili.com/wn/")
        getProxy("http://www.xicidaili.com/wt/")
    
        all_thread = []
        for i in range(30):
            t = threading.Thread(target=verifyProxyList)
            all_thread.append(t)
            t.start()
    
        for t in all_thread:
            t.join()
    
        inFile.close()
        verifiedtxt.close()

    回答所涉及的環境:聯想天逸510S、Windows 10。

    2年前 / 評論
    亚洲 欧美 自拍 唯美 另类