Ross Wan's World!

Python, Ajax, PHP and Linux.

Python:测试代理服务器的程序

Posted by Ross Wan 于 2008/03/02

       写了一个简单测试代理服务器速度的程序。下面是源代码:

#!/usr/bin/python
# _*_ coding:utf-8 _*_
# Copyright: GPLv2
# From: RossWan(mrwlwan@gmail.com)

import sys
import urllib
import socket
import re
import time

def getProxies():
    “””Get all proxies from web site”””
    sock = urllib.urlopen(‘http://www.proxycn.com/html_proxy/30fastproxy-1.html’)
    html = sock.read()
    sock.close()
    proxyRe = re.compile(r’d{1,3}.d{1,3}.d{1,3}.d{1,3}:d{1,4}’)
    proxies = set(proxyRe.findall(html))
    return list(proxies)

def testProxy(targetUrl,proxy):
    “””Test a proxy”””
    try:
        proxies = {‘http’:’http://’+proxy}
        sock = urllib.urlopen(targetUrl,proxies=proxies)
        html = sock.read()
        sock.close()
        if len(html):
            return True
    except:
        return False

if __name__ == ‘__main__’:
    targetUrl = len(sys.argv)==1 and ‘http://www.google.cn’ or sys.argv[1]
    print ‘Target Url: ‘,targetUrl
    proxyList = []
    proxies = getProxies()
    socket.setdefaulttimeout(5.0)
    for proxy in proxies:
        start = time.time()
        result = testProxy(targetUrl,proxy)
        end = time.time()
        if result:
            print proxy,’ spends ‘,end-start,’ seconds.’
            proxyList.append((end-start,proxy))
    proxyList.sort()
    print ‘nnThe  fast proxies:’
    for i in proxyList:
        print i[1]

      
        或者通过 svn 来 Checkout 源代码:

     
         使用也很简单,将代码保存为 testproxy.py,执行:

python testproxy.py http://www.baidu.com

      
        “http://www.baidu.com” 为指定的目标测试网址,你也可以指定为其它的,如果没有指定,默认是“http://www.google.cn”。

       程序通过抓取代理中国(ProxyCN)每日最快50个HTTP代理列表,以用户指定的目标测试网址为基准,来测试每个有效代理服务器的速度。最后对有效服务器进行排序。

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

 
%d 博主赞过: