2345技术员联盟

python3 寫的代理服務器驗證工具

  • 来源:未知 原创
  • 时间:2018-07-28
  • 阅读:
  • 本文标签:

由於經常要使用代理服務器,而網上提供的代理服務器很多都是臨時的,一個一個的手動驗證費時費力,所以我就想用python3的urllib庫來寫一個

服務器獲取、驗證工具。


 


    首先,要找一個提供代理服務器的網站,我用的是http://5uproxy.net/,這個網站的代理服務器幾乎每天都更新,所以效果應該是比較好

的。我的思路是,先打開這個網站的頁面,將頁面的信息down下來,然後用正則表達式分析,找出其中的代理服務器和端口號,再用

urllib.request.build_opener構造一個使用該代理服務器的opener,然後用這個opener去打開百度的頁面,探測獲取到的信息是否正確,如果正

確,就說明代理服務器有效,輸出代理服務器的IP、端口即可。


 




[python] view plain copy

#!/usr/bin/env python  

#coding=utf-8  

import urllib  

import http.cookiejar   

import re  

import socket  

class OpenUrl:  

    def __init__(self):  

        self.result          = ""  

          

          

    def openpage(self):                                                                                # url為相對路徑   

        try:  

            url = "http://www.baidu.com"  

            self.result = self.opener.open(url).read().decode("gb2312")  

        except urllib.error.HTTPError as ex :             

            self.mute.release()  

            self.result = "openpage error: %s"%ex    

            return False  

        except ssl.SSLError as ex:  

            self.mute.release()  

            self.result = "openage  error: %s"%ex  

            return False  

          

        return self.result.find("京ICP證030173號")      

              

              

      

    def getHtmlTdInfo(self, context):  

        result  = []  

        p = re.compile("/d{1,3}/./d{1,3}/./d{1,3}/./d{1,3}[/w|/W]+?<td width=/"60/">/d{2,4}")  

        ret = p.findall(str(context))  

          

        if ret is None:  

            return None  

          

        for x in ret:  

            element = []  

            q = re.compile("^(/d{1,3}/./d{1,3}/./d{1,3}/./d{1,3})[/w|/W]+?(/d{2,4})$")  

            subRet = q.search(x);  

            if subRet==None:  

                continue  

            element.append(subRet.group(1))  

            element.append(subRet.group(2))  

            result.append(element)  

        return result              

      

      

    def getAgencyIP(self,url=""):  

        socket.setdefaulttimeout(20)                       #20秒內沒有打開web頁面,就算超時   

            

        url="http://5uproxy.net/http_fast.html"            #獲取最新代理服務器  

        self.result = str(urllib.request.urlopen(url).read())  

        if self.result==None:  

            return  

        AgencyIP = self.getHtmlTdInfo(self.result)  

        print(len(AgencyIP))  

          

        url="http://5uproxy.net/http_anonymous.html"       #獲取匿名訪問代理服務器  

        self.result = str(urllib.request.urlopen(url).read())  

        if self.result==None:  

            return  

        AgencyIP += self.getHtmlTdInfo(self.result)  

        print(len(AgencyIP))  

          

        url="http://5uproxy.net/http_non_anonymous.html"   #獲取透明訪問代理服務器  

        self.result = str(urllib.request.urlopen(url).read())  

        if self.result==None:  

            return  

        AgencyIP += self.getHtmlTdInfo(self.result)  

        print(len(AgencyIP))  

          

        socket.setdefaulttimeout(5)                       #5內沒有打開web頁面,就算超時   

        if len(AgencyIP)==0:  

            print("獲取代理服務器失敗")  

            return  

        for x in AgencyIP:  

            if x[0]=="":  

                continue  

            try:  

                proxy_support = urllib.request.ProxyHandler({'http':'http://'+str(x[0])+':'+str(x[1])})  

                self.opener = urllib.request.build_opener(proxy_support, urllib.request.HTTPHandler)  

                urllib.request.install_opener(self.opener)         

                if self.openpage()==False:  

                    x[0] = ""  

                    x[1] = ""  

                    continue  

                print("有效的代理服務器"+str(x[0])+":"+str(x[1]))  

                print("等待2秒")  

                time.sleep(2)         

            except:  

                continue  

                  

                  

if __name__ == "__main__":  

    test = OpenUrl()  

    test.getAgencyIP()  


本文来自电脑技术网www.it892.com),转载本文请注明来源.
本文链接:http://www.it892.com/content/ip/2018/0728/107640.html

无觅相关文章插件,快速提升流量