#!/usr/bin/python
#-*- coding:gb18030 -*-
'''
Created on 2013-7-14
#判断文件中的ip是否能ping通,并且将通与不通的ip分别写到两个文件中
#文件中的ip一行一个
#QQ:136354553
'''
import time,os
start_Time=int(time.time()) #记录开始时间
def ping_Test():
ips=open('host.txt','r')
ip_True = open('ip_True.txt','w')
ip_False = open('ip_False.txt','w')
count_True,count_False=0,0
for ip in ips.readlines():
ip = ip.replace('\n','') #替换掉换行符
return1=os.system('ping -n 2 -w 1 %s'%ip) #每个ip ping2次,等待时间为1s
if return1:
print 'ping %s is fail'%ip
ip_False.write(ip) #把ping不通的写到ip_False.txt中
count_False += 1
else:
print 'ping %s is ok'%ip
ip_True.write(ip) #把ping通的ip写到ip_True.txt中
count_True += 1
ip_True.close()
ip_False.close()
ips.close()
end_Time = int(time.time()) #记录结束时间
print "time(秒):",end_Time - start_Time,"s" #打印并计算用的时间
print "ping通数:",count_True," ping不通的ip数:",count_False
ping_Test()
初学者,如有好的建议请留言指教,谢谢~
python通过代理刷网页点击量
分类: python
2013-07-14 23:08
420人阅读
收藏
举报
#!/usr/bin/python
#-*- coding:utf-8 -*-
'''
此脚本主要实现网页的点击量,除了实现次功能点外,还有三个知识点:
1、随机获取代理ip,通过代理ip访问指定站点,其目的是防止ip被封
2、访问一个页面后,随机休息几秒,再访问,其目的是防止网站前面有4-7层过滤设备拦截
3、修改http的user agent字段,有些网站和4-7层设备会检查
Created on 2013-7-14
@author: QQ136354553
'''
import urllib2,re,time,urllib,proxyIP,random,user_agents
def getHtml(url):
proxy_ip =random.choice(proxyIP.proxy_list) #在proxy_list中随机取一个ip
print proxy_ip
proxy_support = urllib2.ProxyHandler(proxy_ip)
opener = urllib2.build_opener(proxy_support,urllib2.HTTPHandler)
urllib2.install_opener(opener)
request = urllib2.Request(url)
user_agent = random.choice(user_agents.user_agents) #在user_agents中随机取一个做user_agent
request.add_header('User-Agent',user_agent) #修改user-Agent字段
print user_agent
html = urllib2.urlopen(request).read()
return html
urls = ['http://www.25shiyan.com/?fromuid=16',' http://www.25shiyan.com/forum.php?mod=viewthread&tid=37840&extra=page%3D1',' http://www.25shiyan.com/forum.php?mod=viewthread&tid=36786&extra=page%3D1']
count = 0
while count < 10000:
for url in urls:
getHtml(url)
count += 1
randomTime = random.uniform(1,10) #取1-10之间的随机浮点数
time.sleep(randomTime) #随机等待时间
print count
######################
上面引入模块中:proxyIP、user_agents内容如下:
######################
proxyIP.py
#!/usr/bin/python
#-*- conding:utf-8 -*-
proxy_list = [
{'http':"http://59.53.67.215:80"},
{'http':"http://60.161.14.77:8001"},
{'http':"http://61.144.14.68:80"},
{'http':"http://61.144.68.180:9999"},
{'http':"http://61.164.108.84:8844"},
{'http':"http://61.166.55.153:11808"}
]
###########################
user_agents.py
#!/usr/bin/python
#-*- coding:utf-8 -*-
import random
user_agents = [
'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11',
'Opera/9.25 (Windows NT 5.1; U; en)',
'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)',
'Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.5 (like Gecko) (Kubuntu)',
'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.12) Gecko/20070731 Ubuntu/dapper-security Firefox/1.5.0.12',
'Lynx/2.8.5rel.1 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/1.2.9'
]
####################################
1、目前脚本有一个缺陷,如果代理ip中有访问不成功的,脚本就会异常终止,还没有实现异常处理,稍后会考虑
2、urls没有处理好,最初是想从一个主战点,抓去子链接的,现在还没有实现
有很多地方没有想好怎么处理,如果有好的建议请赐教~