Created
January 3, 2013 13:40
-
-
Save moolex/4443534 to your computer and use it in GitHub Desktop.
自动检测代理速度的Python脚本
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
# From: ubuntu.org.cn Copyright: GPLv2 | |
import urllib | |
import re | |
from datetime import datetime | |
import socket | |
def findporxy(): | |
url = "http://www.proxycn.com/html_proxy/http-1.html" | |
f = urllib.urlopen(url) | |
html = f.read() | |
p = re.compile('clip\(\'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\:\d{1,4})\'\);') | |
ips=set(p.findall(html)) | |
socket.setdefaulttimeout(5) | |
a={} | |
for ip in ips: | |
start=datetime.now() | |
flag = checkproxy(ip) | |
end=datetime.now() | |
if flag: | |
print ip,'花费',end-start,'时间'; | |
a[end-start]=ip | |
b=sorted(a) | |
print '速度排序 :' | |
i = 0 | |
for ix in a: | |
print 'No.',i,': ',a[b[i]] | |
i = i + 1 | |
def checkproxy(ip): | |
try: | |
proxies = {'http': 'http://'+ip} | |
filehandle = urllib.urlopen("http://blog.uuland.org/moyo/speed_x.jpg", proxies=proxies) | |
html=filehandle.read() | |
if len(html)==80996: | |
return True | |
else: | |
return False | |
except: | |
return False | |
if __name__ == '__main__': | |
findporxy() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment