时间:2021-07-01 10:21:17 帮助过:36人阅读
1、创建文件:checkproxy.py
#coding=utf-8
import urllib2
import urllib
import time
import socket
ip_check_url = 'http://www.google.com.hk/'
user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0'
socket_timeout = 30
# Check proxy
def check_proxy(protocol, pip):
try:
proxy_handler = urllib2.ProxyHandler({protocol:pip})
opener = urllib2.build_opener(proxy_handler)
# opener.addheaders = [('User-agent', user_agent)] #这句加上以后无法正常检测,不知道是什么原因。
urllib2.install_opener(opener)
req = urllib2.Request(ip_check_url)
time_start = time.time()
conn = urllib2.urlopen(req)
# conn = urllib2.urlopen(ip_check_url)
time_end = time.time()
detected_pip = conn.read()
proxy_detected = True
except urllib2.HTTPError, e:
print "ERROR: Code ", e.code
return False
except Exception, detail:
print "ERROR: ", detail
return False
return proxy_detected
def main():
socket.setdefaulttimeout(socket_timeout)
print
protocol = "http"
current_proxy = "212.82.126.32:80"
proxy_detected = check_proxy(protocol, current_proxy)
if proxy_detected:
print (" WORKING: " + current_proxy)
else:
print " FAILED: %s " % ( current_proxy, )
if __name__ == '__main__':
main()
2、测试:
[root@bogon proxyipspider]# python checkproxy.py WORKING: 212.82.126.32:80
当然,这只是程序的一个原型,真正检测的程序还需要结合数据库或文件操作来完成。代理IP检测出来了,那么剩下的就是设置了。设置好以后,尽情地gg吧。1024你想看多久就看多久,不过还是不要看多为好,你懂的。如果你想上脸谱、油土鳖和推特,这些就靠你自己了,这里只是gg而已。
程序猿嘛,总是想通过自己的双手来解决点问题。那颗change the world的心没有变,就像博客园的口号那样“代码改变世界”。看到哪个东西不爽,自己造一个吧。IT界这样的例子太多了,天天用的Vi、github等等。好了,就到这,1024去,走起。。。
那道墙着实可恨!