-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpServer.py
More file actions
213 lines (190 loc) · 8.16 KB
/
HttpServer.py
File metadata and controls
213 lines (190 loc) · 8.16 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
from http.server import BaseHTTPRequestHandler, HTTPServer
import socket
from urllib.parse import urlparse, unquote
import urllib.request
from html.parser import HTMLParser
from urllib.request import HTTPError, URLError
from broker import Broker
from logFormatter import logger
class HtmlParser(HTMLParser):
def __init__(self):
super().__init__()
self.urls = []
self.all_urls = []
def handle_starttag(self, tag, attrs):
if tag == 'a':
for name, value in attrs:
if name == 'href':
if not (value.startswith("https://") or value.startswith("http://")
or value.startswith("file://") or value.startswith("ftp://")):
return
if value in self.all_urls:
return
if value not in self.urls:
self.urls.append(value)
self.all_urls.append(value)
class HTTPHandler(BaseHTTPRequestHandler):
def __init__(self, request, client_address, server):
super().__init__(request, client_address, server)
# f = open("response.txt", 'w')
# f.close()
def __set_get_response(self):
self.send_response(202)
self.send_header('Content-type', 'octet-stream')
self.send_header('Content-Disposition', 'attachment; filename = "response.html"')
self.end_headers()
def __set_response(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def __parse_query__(self, query):
query_components = dict(qc.split('=') for qc in query.split('&'))
search = query_components['search']
depth = query_components['depth']
domain = query_components['domain']
return search, depth, domain
def __get_html_response__(self, search, depth, domain):
return self.server.get_html(url=search ,depth=int(depth) ,domain=domain)
def do_GET(self):
query = urlparse(unquote(self.path)).query
if len(query) > 0:
self.__set_get_response()
search, depth, domain = self.__parse_query__(query)
logger.info(f'GET request for url: {search} in domain: {domain} with depth: {depth}')
s = '<!DOCTYPE html>' \
'<html lang="en">' \
'<head>' \
'<meta charset="UTF-8">' \
'<title>Distributed Scrapy</title>' \
'</head>' \
'<body>' \
'<p>'
try:
self.wfile.write((s).encode('utf-8'))
except BrokenPipeError as e:
logger.error(f'ERROR {e.errno}: {e.strerror}')
for item in self.__get_html_response__(search, depth, domain):
items = "<h1 style = 'color: cornflowerblue'>URL: " + str(item) + " </h1> "
items += str(item)
try:
self.wfile.write((items).encode('utf-8'))
except BrokenPipeError as e:
logger.error(f'ERROR {e.errno}: {e.strerror}')
return
r = '</p>' \
'</form>' \
'</body>' \
'</html>'
try:
logger.info("Scrapping finished")
self.wfile.write((r).encode('utf-8'))
except BrokenPipeError as e:
logger.error(f'ERROR {e.errno}: {e.strerror}')
else:
self.__set_response()
s = '<!DOCTYPE html>' \
'<html lang="en">' \
'<head>' \
'<meta charset="UTF-8">' \
'<title>Distributed Scrapy</title>' \
'</head>' \
'<body>' \
'<h2>Insert URL to scrap</h2>' \
'<form id="MyForm" method="get">' \
'URL: <input style="margin-right: 20px" required = "true" type="url" name="search">' \
'Domain: <input style="margin-right: 20px" type="text" name="domain">' \
'Depth: <input style="margin-right: 20px" required = "true" type="number" name="depth" min="1" max="5">' \
'<input type="submit" value="Submit">' \
'</form>' \
'</body>' \
'</html>'
try:
logger.info("Scrapping finished")
self.wfile.write(s.encode('utf-8'))
except BrokenPipeError as e:
logger.error(f'ERROR {e.errno}: {e.strerror}')
class HttpServer(HTTPServer):
def __init__(self, broker, port=5001, handler=HTTPHandler):
self.broker = broker
host = socket.gethostname()
host = socket.gethostbyname(host)
address = (host, port)
logger.info(f'Running HTTP server at http://{host}:{port}')
HTTPServer.__init__(self, address, handler)
def get_html(self, url, domain, depth):
return self.__scrap_urls__(url=url, domain=domain, nivel=depth)
def __scrap_urls__(self, url, domain, nivel):
answer = {}
parser = HtmlParser()
parser.urls.append(url)
count = 1
while count <= nivel:
level = parser.urls.copy()
for url in level:
logger.info(f"Getting html for {url}")
if not (url.startswith("https://") or url.startswith("http://")
or url.startswith("file://") or url.startswith("ftp://")):
continue
if domain != '' and domain not in url:
answer[url] = "DOMAIN ERROR"
logger.debug("URL not in domain")
yield "DOMAIN ERROR"
continue
html = self.broker.__get_cache_data__(url)
if html != "Empty" and not html is None:
logger.info("CACHE RETURNED HTML")
answer[url] = html
yield html
if count < nivel:
parser.feed(html)
else:
try:
logger.info("Scrapping internet")
with urllib.request.urlopen(url, timeout=15) as r:
x = r.read().decode('utf-8')
parser.feed(x)
answer[url] = x
yield x
self.broker.__save_cache_data__(url, x)
except HTTPError as e:
answer[url] = "HTTP ERROR" + str(e.getcode())
yield "HTTP ERROR" + str(e.getcode())
logger.error(e.getcode())
except URLError as e:
answer[url] = "URL ERROR: " + str(e.reason)
yield "URL ERROR: " + str(e.reason)
logger.error(e.reason)
except Exception as e:
answer[url] = "ERROR: " + str(e.args)
yield "ERROR: " + str(e.args)
logger.error(e.args)
count += 1
def main():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--portin', type=int, default=5000, required=False,
help='Port for incoming communications on node')
parser.add_argument('-a', '--address', type=str, required=False,
help='Address of node to connect to')
parser.add_argument('--httpport', type=int, default=5001, required=False,
help='Port for HTTP server')
parser.add_argument('--nbits', type=int, default=5, required=False,
help='Number of bits of the chord model')
# It should have a bigger default value
args = parser.parse_args()
port1 = args.portin
port2 = args.httpport
nbits = args.nbits
if args.address:
host, port = args.address.split(':')
address = (host, int(port))
node = Broker(port1, address, nbits=nbits)
else:
node = Broker(port1, nbits=nbits)
server = HttpServer(node, port2)
try:
server.serve_forever()
except KeyboardInterrupt:
server.server_close()
if __name__ == '__main__':
main()