-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection.py
More file actions
25 lines (20 loc) · 776 Bytes
/
connection.py
File metadata and controls
25 lines (20 loc) · 776 Bytes
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
import urllib.request as urlreq
from urllib.parse import urlparse
import http.client
class Connection:
HTTP_PORT = 80
HTTPS_PORT = 443
def __init__(self, host):
self.connection = http.client.HTTPSConnection(host, Connection.HTTPS_PORT)
def getFromConnection(self, url):
#making relative url from passed absolute url
urlParts = urlparse(url)
relUrl = url.replace(urlParts.scheme, "").replace(urlParts.netloc, "")[3:]
self.connection.request("GET", relUrl, headers={
"Connection" : "Keep-Alive"
})
resp = self.connection.getresponse()
return resp.read().decode('utf-8')
@staticmethod
def getUrlContentsAsUtf8(url):
return urlreq.urlopen(url).read().decode('utf-8')