-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary.py
97 lines (85 loc) · 2.45 KB
/
library.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""
All the main atributes classes are defined here
"""
import requests, socket
import pymongo
import itertools, string
from pyquery import PyQuery
def hostname_resolves(hostname):
try:
socket.gethostbyname(hostname)
return 0
except socket.error:
return 1
def gen_domain(lenght):
"""
Generate domain
"""
letters = string.ascii_lowercase
first_group = itertools.product(letters, repeat=lenght)
for elem in itertools.product(first_group):
yield [''.join(k) for k in elem]
def match_tld(func, tld):
for l in func:
yield l[0]+'.'+tld
class MongoObj:
"""
Connect and read/write to mongo
"""
def __init__(self, host="mongodb://127.0.0.1:27017", user=None,
password=None, out=None, db="pyindex",
col="test_mongo", insert=None):
self.host = host
self.user = user
self.password = password
self.db = db
self.col = col
self.out = out
self.insert = insert
def mon_con(self):
"""
Establsih connection to mongodb
"""
try:
return pymongo.MongoClient(self.host, username=self.user,
password=self.password)
except:
self.out = "Could not connect to {}".format(self.host)
def mon_list(self):
"""
List all collections
"""
try:
self.out = str(self.mon_con()[self.db].collection_names())
except:
self.out = "Could not list collections"
def mon_write(self):
try:
self.mon_con()[self.db][self.col].insert_one(self.insert)
except:
self.out = "Could not write to mongodb"
def __str__(self):
return self.out
def gen_req(url):
for l in url:
if hostname_resolves(l) == 0:
try:
result = requests.get("https://"+l, timeout=3)
except:
continue
else:
continue
if result.status_code == 200:
try:
query = PyQuery(result.text)
text = query.text()
except:
pass
else:
text = "HTTP code: {}".format(result.status_code)
try:
yield { 'status': result.status_code, 'url': l, 'data': text}
except:
yield { 'status': result.status_code, 'url': l, 'data': text}
else:
continue