-
Notifications
You must be signed in to change notification settings - Fork 37
/
load.py
72 lines (60 loc) · 1.94 KB
/
load.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""
从文件中加载 IP 列表
"""
import asyncio
import os
import sys
import aiohttp
from src.app.ip_get import IPGet
from src.app.main import Logger
async def main():
argv = None
if len(sys.argv) > 1:
argv = sys.argv[1]
if argv and argv.find('://') > 0:
return await load_from_url(argv)
res = os.listdir('.')
ip_file_lists = [name for name in res if name.find('.ip.txt') > 0]
if argv:
if argv not in ip_file_lists:
Logger.error('file %s doesn\'t exists' % argv)
return
else:
ip_file_lists = [argv]
for fn in ip_file_lists:
await load_file(fn)
async def load_file(f_path):
with open(f_path) as f:
ips = []
for ip in f.readlines():
if ip and ip.find(':') and ip.find('#') < 0:
ip = ip.strip()
ips.append(ip)
if ips:
Logger.info('Find ip count %d' % len(ips))
await IPGet.push_to_pool(ips)
async def load_from_url(url: str):
import re
headers = {
'User-Agent': get_user_agent()
}
async with aiohttp.ClientSession(headers=headers) as session:
async with session.get(url) as resp:
text = await resp.text()
matched = re.findall(r'(?:\d{1,3}\.){3}\d{1,3}:\d+', text)
ips = []
for ip in matched:
if ip and ip.find(':') and ip.find('#') < 0:
ip = ip.strip()
ips.append(ip)
if ips:
Logger.info('Find ip count %d' % len(ips))
await IPGet.push_to_pool(ips)
def get_user_agent() -> str:
import random
return 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%d.0.3770.80 Safari/537.36' % random.randint(
70, 76)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
tasks = [main()]
loop.run_until_complete(asyncio.wait(tasks))