Line | |
---|
1 |
|
---|
2 | import http.client
|
---|
3 | from urllib.parse import urlparse, urlencode, quote_plus
|
---|
4 |
|
---|
5 |
|
---|
6 | class HTTPClient(object):
|
---|
7 |
|
---|
8 | def __init__(self, base_url):
|
---|
9 | self.parsed_url = urlparse(base_url)
|
---|
10 | if self.parsed_url.scheme == 'https':
|
---|
11 | self.conn = http.client.HTTPSConnection(self.parsed_url.netloc)
|
---|
12 | else:
|
---|
13 | self.conn = http.client.HTTPConnection(self.parsed_url.netloc)
|
---|
14 |
|
---|
15 | def get(self, url):
|
---|
16 | self.conn.request('GET', url)
|
---|
17 | response = conn.getresponse()
|
---|
18 | return response.read()
|
---|
19 |
|
---|
20 | def post(self, url, data):
|
---|
21 | encoded_data = urlencode(data, quote_via=quote_plus)
|
---|
22 | headers = {'Content-type': 'application/x-www-form-urlencoded',
|
---|
23 | 'Accept': 'text/plain'}
|
---|
24 | self.conn.request('POST', url, encoded_data, headers)
|
---|
25 | response = conn.getresponse()
|
---|
26 | return response.read()
|
---|
Note:
See
TracBrowser
for help on using the repository browser.