-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpage.py
73 lines (56 loc) · 1.61 KB
/
webpage.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
# Northcott Monitor
# Written by Matthew Northcott
# 20-08-2016
# Python 3.4.3
__author__ = "Matthew Northcott"
# IMPORTS
import urllib.request, urllib.error
import re
import threading
import time
import queue
# GLOBALS
HEADERS = { 'User-Agent': 'Mozilla/5.0' }
# MAIN
class Webpage(object):
def __init__(self, url):
self.url = url
self.response = None
def __str__(self):
return str(self.response)
def __enter__(self):
self.open()
return self
def __exit__(self, type, value, traceback):
del self
def _open(self, request, q):
try:
response = urllib.request.urlopen(request).read()
except urllib.error.URLError:
response = None
q.put(response)
def open(self, timeout=10.0):
q = queue.Queue()
request = urllib.request.Request(self.url, headers=HEADERS)
thr = threading.Thread(target=self._open, args=(request, q))
thr.start()
t = 0
while t < timeout and thr.is_alive():
time.sleep(0.10)
t += 0.10
if thr.is_alive():
del thr
return False
response = q.get()
if response is None:
return False
self.response = response
return True
def search(self, regex_term):
try:
result = re.search(regex_term, str(self)).group(0)
except AttributeError:
return None
return result
def find_all(self, regex_term):
return re.findall(regex_term, str(self))