-
Notifications
You must be signed in to change notification settings - Fork 0
/
url.py
71 lines (60 loc) · 1.98 KB
/
url.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
import socket
import ssl
class URL:
def __init__(self, url):
self.scheme, url = url.split("://", 1)
assert self.scheme in ["http", "https"]
if self.scheme == "http":
self.port = 80
elif self.scheme == "https":
self.port = 443
if "/" not in url:
url = url + "/"
self.host, url = url.split("/", 1)
self.path = "/" + url
if ":" in self.host:
self.host, port = self.host.split(":", 1)
self.port = int(port)
def request(self):
s = socket.socket(
family=socket.AF_INET,
type=socket.SOCK_STREAM,
proto=socket.IPPROTO_TCP,
)
s.connect((self.host, self.port))
if self.scheme == "https":
ctx = ssl.create_default_context()
s = ctx.wrap_socket(s, server_hostname=self.host)
request = "GET {} HTTP/1.0\r\n".format(self.path)
request += "Host: {}\r\n".format(self.host)
request += "\r\n"
s.send(request.encode("utf8"))
response = s.makefile("r", encoding="utf8", newline="\r\n")
statusline = response.readline()
version, status, explanation = statusline.split(" ", 2)
response_headers = {}
while True:
line = response.readline()
if line == "\r\n" : break
header, value = line.split(":", 1)
response_headers[header.casefold()] = value.strip()
assert "transfer-encoding" not in response_headers
assert "content-encoding" not in response_headers
content = response.read()
s.close()
return content
def show(body):
in_tag = False
for c in body:
if c == "<":
int_Tag = True
elif c == ">":
in_tag = False
elif not in_tag:
print(c, end="")
def load(url):
body = url.request()
show(body)
if __name__ == "__main__":
import sys
load(URL(sys.argv[1]))