一个比较独特的"HelloWorld"
作者:互联网
为什么说是特殊的helloworld呢
给全世界的服务器发送一个请求,这就是我"打招呼"的方式
核心内容就是:向所有的ipv4地址发送一个请求
即x.x.x.x,如果其是web服务,就拿到其title标签里的内容,
然后看看这个IP(或者说网站)是做什么的,
你问我这么做的意义是什么?
文章结尾我再谈谈做这件事情的意义.
接下来谈谈过滤条件
IPV4地址有非常多个,从0.0.0.0~255.255.255.255,共有4,294,967,296个
将近43亿个,当然,这里面的IP大部分是无效的,无法访问的,
我将不会处理包含以下任意条件的IP地址,因为无意义
1.五秒内没有响应的
2.状态码不是200的
3.状态码是200,但没有title标签的.
4.其它无法访问的异常.
即使加了这些条件,要走完这43亿服务器,也必然是一个漫长的过程,
好在是机器帮我来做这件事情,我只要开着程序,过一段时间看看就好.
理一理思路
要想实现这个看似简单的需求,其实也并不是那么容易的
.要有一个IP生成器.按顺序生成0.0.0.0到255.255.255.255的IP字符串
生成出来的IP还不能保存,即生即用,不然分分钟爆内存啦.
要有计数器,统计一共请求过多少IP,失败过多少次?成功过多少次?失败原因等等
要开启多线程,不能单线程去请求,不然这辈子都别想把这个程序跑完.
要考虑到程序因为意外原因被关掉以后,不要又从头开始,
例如第一个请求是0.0.0.0,第二个是0.0.0.1,当你请求到数千万时,中断了,不能又从0.0.0.0开始.
开始撸代码
首先,关于IP生成器的想法,我的想法是映射
即0.0.0.0映射为0,代表着它是第0个IP地址
0.0.0.1映射为1,代表着它是第1个IP地址
0.0.1.0映射为256,代表第256个IP地址,
妥妥的256进制嘛,
假如我传入一个0~4294967295之间的整数,
是不是意味着这里面的每一个整数都可以映射为一个IP地址呢,
下面我用python来实现这个函数,
def ipv4(n): if n >= 16777216: ip_1 = int(n/16777216) ip_1s = n%16777216 ip_2 = int(ip_1s/65536) ip_2s = ip_1s%65536 ip_3 = int(ip_2s/256) ip_3s = ip_2s%256 ip_4 = ip_3s # print(ip_1,ip_2,ip_3,ip_4,sep=".") return "{}.{}.{}.{}".format(ip_1,ip_2,ip_3,ip_4) elif n<16777216 and n>=65536: ip_1 = 0 ip_2 = int(n/65536) ip_2s = n%65536 ip_3 = int(ip_2s/256) ip_3s = ip_2s%256 ip_4 = ip_3s # print(ip_1,ip_2,ip_3,ip_4,sep=".") return "{}.{}.{}.{}".format(ip_1,ip_2,ip_3,ip_4) elif n<65536 and n>=256: ip_1 = 0 ip_2 = 0 ip_3 = int(n/256) ip_3s = n%256 ip_4 = ip_3s # print(ip_1,ip_2,ip_3,ip_4,sep=".") return "{}.{}.{}.{}".format(ip_1,ip_2,ip_3,ip_4) else: # print("0.0.0.{}".format(n)) return "0.0.0.{}".format(n)
随便输个数字测一下
>>>ipv4(2888889999) 172.48.246.143 >>>ipv4(256) 0.0.1.0
接下来,准备一个巨大的整数(42,9496,7295)
它代表着255.255.255.255.
没错,只是一个网关地址,它肯定无法被请求,但我还是决定从它开始,
每发送一个请求,这个数字就减去一,来个死循环
流程总体来说就是这样的:
1.取到count,这个值(42,9496,7295)当计数器
2.把这个值解析为IP.(255.255.255.255),
3.向这个IP地址发送请求
4.count-=1
循环以上操作.
第1次执行的时候,请求地址是255.255.255.255
第2次:255.255.255.254
第3次:255.255.255.253
第4次:255.255.255.252
…
下面是具体请求的代码
节选自Class的代码
代码写得比较糙,因为这只是个小demo,写着玩,敬请谅解,
取到的数据直接保存成txt了,后面有空再优化.
def getdata(self,ip): try: r = requests.get(url=ip, headers=self.headers ,timeout=self.timeout) r_code = r.status_code r_text = r.content.decode("utf8") title = re.findall("<title>(.*?)</title>", r_text) if r_code!=200: self.bad_status_count+=1 return if len(title)==0: self.not_title_count+=1 return if title[0]=="": self.not_title_count+=1 return c_ip = ip.replace("http://","") with open("f://hello_world/"+c_ip+title[0]+".txt","w",encoding="utf8")as f: pass except: self.time_out_count+=1 return
全部代码:
我开了1000个线程.
没有做MT4教程线程锁之类的东西,测试了一下不影响,就没做了
数据以光有名字没内容的txt文件存在f盘hello world文件夹里,
如果有copy代码运行的,可以自己改路径
比较糙,有空再优化
#!/usr/bin/env python # -*- coding:utf-8 -*- # author: kcc time:2019/11/16 import requests,re,random,threading,time,os class SpiderWorld(): def __init__(self): self.headers = { "User-Agent":"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36" ,"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3" } self.timeout = 5 self.ip_count = 4294967295 #2642648575 #4294967295 self.time_out_count = 0 self.bad_status_count = 0 self.not_title_count = 0 def getdata(self,ip): #发送请求 try: r = requests.get(url=ip, headers=self.headers ,timeout=self.timeout) r_code = r.status_code r_text = r.content.decode("utf8") title = re.findall("<title>(.*?)</title>", r_text) if r_code!=200: self.bad_status_count+=1 return if len(title)==0: self.not_title_count+=1 return if title[0]=="": self.not_title_count+=1 return c_ip = ip.replace("http://","") with open("f://hello_world/"+c_ip+title[0]+".txt","w",encoding="utf8")as f: pass except: self.time_out_count+=1 return def ipv4(self,n): #把n解析为IP地址字符串 if n >= 16777216: ip_1 = int(n/16777216) ip_1s = n%16777216 ip_2 = int(ip_1s/65536) ip_2s = ip_1s%65536 ip_3 = int(ip_2s/256) ip_3s = ip_2s%256 ip_4 = ip_3s # print(ip_1,ip_2,ip_3,ip_4,sep=".") return "{}.{}.{}.{}".format(ip_1,ip_2,ip_3,ip_4) elif n<16777216 and n>=65536: ip_1 = 0 ip_2 = int(n/65536) ip_2s = n%65536 ip_3 = int(ip_2s/256) ip_3s = ip_2s%256 ip_4 = ip_3s # print(ip_1,ip_2,ip_3,ip_4,sep=".") return "{}.{}.{}.{}".format(ip_1,ip_2,ip_3,ip_4) elif n<65536 and n>=256: ip_1 = 0 ip_2 = 0 ip_3 = int(n/256) ip_3s = n%256 ip_4 = ip_3s # print(ip_1,ip_2,ip_3,ip_4,sep=".") return "{}.{}.{}.{}".format(ip_1,ip_2,ip_3,ip_4) else: # print("0.0.0.{}".format(n)) return "0.0.0.{}".format(n) def main(self): #主函数循环 while True: req_ip = "http://"+self.ipv4(self.ip_count) self.ip_count-=1 self.getdata(req_ip) if self.ip_count<100:return def state(self): #每隔十秒输出一下日志 while True: with open("a.txt","w",encoding="utf8")as f: f.write("当前轮询IP地址{}".format(self.ipv4(self.ip_count))+"\n") f.write("当前剩余IP地址数{}".format(self.ip_count)+"\n") f.write("超时数量{}".format(self.time_out_count)+"\n") f.write("错误状态码数{}".format(self.bad_status_count)+"\n") f.write("无效标题数{}".format(self.not_title_count)+"\n") time.sleep(10) if __name__ == "__main__": #开启程序1000线程 a = SpiderWorld() for i in range(1000): t = threading.Thread(target=a.main) t.start() #开启一个输出日志的线程 a = threading.Thread(target=a.state) a.start()
标签:count,return,ip,self,HelloWorld,0.0,256,比较,独特 来源: https://www.cnblogs.com/benming/p/11898756.html