-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrawler_with_coroutines.py
284 lines (234 loc) · 8.18 KB
/
crawler_with_coroutines.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import socket
from selectors import DefaultSelector, EVENT_WRITE, EVENT_READ
import ssl
import re
from bs4 import BeautifulSoup
import time
selector = DefaultSelector()
host_address = 'xkcd.com'
host_port = 443
urls_todo = {'/'}
seen_urls = {'/'}
stopped = False
class Link:
protocol_pattern = re.compile(r'(https+)')
host_name_pattern = re.compile(r'(https?)?:?//([\w.-]+)/?')
def __init__(self, url):
self.url = url if url else '/'
def get_host_name(self):
host_name = False
match = re.match(self.host_name_pattern, self.url)
if match:
host_name = match.group(2)
return host_name
def get_protocol(self):
protocol = False
match = re.match(self.protocol_pattern, self.url)
if match:
protocol = match.group()
return protocol
def get_path(self):
path = ''
return path
def is_url(self):
result = False
has_protocol = re.match(self.protocol_pattern, self.url)
has_host_name = re.match(self.host_name_pattern, self.url)
result = True if has_host_name or has_protocol else False
return result
def is_path_only(self):
host_name = self.get_host_name()
return not (self.is_url() or self.is_fragment_only() or host_name)
def is_fragment_only(self):
return self.url[0] == '#'
def __str__(self):
return f"{self.url}"
def __repr__(self) -> str:
return f"{type(self).__name__}({self.url})"
class Task:
def __init__(self, coro):
self.coro = coro
f = Future()
f.set_result(None)
self.step(f)
def step(self, future):
try:
next_future = self.coro.send(future.result)
except StopIteration:
return
except Exception as e:
raise e
next_future.add_done_callback(self.step)
class Future:
def __init__(self):
self.result = None
self._callbacks = []
def add_done_callback(self, fn):
self._callbacks.append(fn)
def set_result(self, result):
self.result = result
for fn in self._callbacks:
fn(self)
def __iter__(self):
yield self
return self.result
class Fetcher:
def __init__(self, url, host_address, host_port):
self.response = b''
self.url = url
self.sock = None
self.host_address = host_address
self.host_port = host_port
def fetch(self):
self.ssl_context = ssl.create_default_context()
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.setblocking(False)
try:
self.sock.connect((self.host_address, self.host_port))
except BlockingIOError:
pass
except Exception as e:
raise e
f = Future()
def on_connected():
f.set_result(None)
selector.register(self.sock.fileno(), EVENT_WRITE, on_connected)
yield from f
selector.unregister(self.sock.fileno())
# print('Connected')
# handle ssl handshake here
f = Future()
def on_handshaked():
f.set_result('Hand Shaked.')
def try_handshake():
try:
self.sock.do_handshake()
if self.sock.getpeercert():
on_handshaked()
except ssl.SSLWantReadError:
try:
selector.modify(self.sock.fileno(),
EVENT_READ, try_handshake)
except:
selector.register(self.sock.fileno(),
EVENT_READ, try_handshake)
except ssl.SSLWantWriteError:
try:
selector.modify(self.sock.fileno(),
EVENT_WRITE, try_handshake)
except:
selector.register(self.sock.fileno(),
EVENT_WRITE, try_handshake)
except Exception as e:
raise e
self.sock = self.ssl_context.wrap_socket(
self.sock, server_hostname=self.host_address, do_handshake_on_connect=False)
try_handshake()
status = yield from f
# print(status)
selector.unregister(self.sock.fileno())
request = self.build_request(self.url, self.host_address)
self.sock.send(request)
self.response = yield from self.read_all(self.sock)
print(f'fetched {self.url}')
links = self.parse_links()
self.collect_links(links=links)
def read_all(self, sock):
response = []
chunk = yield from self.read(sock)
while chunk:
response.append(chunk)
chunk = yield from self.read(sock)
return b''.join(response)
def read(self, sock):
f = Future()
def on_readable():
try:
f.set_result(sock.recv(1024))
except ssl.SSLWantReadError:
time.sleep(0.0000000001)
on_readable()
selector.register(sock.fileno(), EVENT_READ, on_readable)
chunk = yield from f
selector.unregister(sock.fileno())
return chunk
try:
self.sock.do_handshake()
selector.unregister(key.fd) # check self.sock.fileno() == key.fd
request = self.build_request(self.url, self.host_address)
self.sock.send(request)
selector.register(key.fd, EVENT_READ, self.read_response)
except ssl.SSLWantReadError:
selector.modify(key.fd, EVENT_READ, self.do_handshake)
except ssl.SSLWantWriteError:
selector.modify(key.fd, EVENT_WRITE, self.do_handshake)
@staticmethod
def build_request(url, host_address):
request_line = f"GET {url} HTTP/1.1\r\n"
headers = [
f"Host: {host_address}",
"Connection: close",
]
request_headers = "\r\n".join(headers)
request = f"{request_line}{request_headers}\r\n\r\n"
encoded_request = request.encode('utf-8')
return encoded_request
global stopped
try:
chunk = self.sock.recv(4096)
# print(chunk.decode('utf-8'))
if chunk != b'':
self.response += chunk
else:
print("successful response")
selector.unregister(key.fd)
links = self.parse_links()
for link in links.difference(seen_urls):
urls_todo.add(link)
new_featcher = Fetcher(link, host_address, host_port)
new_featcher.fetch()
seen_urls.update(links)
urls_todo.remove(self.url)
if not urls_todo:
stopped = True
except ssl.SSLWantReadError:
selector.modify(key.fd, EVENT_READ, self.do_handshake)
except ssl.SSLWantWriteError:
selector.modify(key.fd, EVENT_WRITE, self.do_handshake)
def parse_links(self):
links = set()
response_soup = BeautifulSoup(
self.response.decode('utf-8'), 'html.parser')
all_anchor_tag = response_soup.find_all('a')
for anchor in all_anchor_tag:
link = Link(anchor.get('href'))
if link.is_path_only():
links.add(anchor.get('href'))
return links
def collect_links(self, links):
global stopped
for link in links.difference(seen_urls):
urls_todo.add(link)
new_featcher = Fetcher(link, host_address, host_port)
coro_gen = new_featcher.fetch()
Task(coro_gen)
seen_urls.update(links)
urls_todo.remove(self.url)
if not urls_todo:
stopped = True
fetcher = Fetcher('/', host_address, host_port)
coro_gen = fetcher.fetch()
Task(coro_gen)
def event_loop():
while not stopped:
try:
events = selector.select()
for event_key, event_mask in events:
callback = event_key.data
callback()
except OSError as e:
if e.errno != 22:
raise e
except Exception as e:
raise e
event_loop()