forked from Kondziowy/PythonBasicTraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuri.py
35 lines (29 loc) · 998 Bytes
/
uri.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
import requests
HTTP = "http"
FILE = "file"
class UriHandler:
def __init__(self, uri):
self.protocol = uri.split(":")[0].lower()
self.resource = uri.split("/")[2]
self.uri = uri
print("URI handler initialized")
def get(self):
raise NotImplementedError()
def get_contents(self):
if self.protocol == HTTP:
return HttpHandler(self.uri).get()
if self.protocol == FILE:
return FileHandler(self.uri).get()
def __eq__(self, right):
return super(UriHandler, self).__eq__(right)
class HttpHandler(UriHandler):
def __init__(self, uri):
super(HttpHandler, self).__init__(uri)
print("HTTP handler initialized")
def get(self):
return requests.get(self.uri).text
class FileHandler(UriHandler):
pass
print(UriHandler("http://wp.pl").get_contents())
print(50 * "=")
print(HttpHandler("http://wp.pl") == HttpHandler("http://wp.pl"))