-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp-client.py
76 lines (57 loc) · 1.95 KB
/
http-client.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
#!/usr/bin/env python3
# -*- coding:utf8 -*-
"""Dowloading data from an HTTP server"""
import os
import argparse
import logging
import timeit
import urllib.request
import http.client
# Default host server
REMOTE_SERVER_HOST = "https://www.python.org"
# Cleaning terminal
os.system("clear")
# Setting the logging configurations
httpclient_logger = logging.getLogger("http.client")
logging.basicConfig(format='%(asctime)s - http-client.py - %(message)s', level=logging.DEBUG)
def httpclient_logging(level=logging.DEBUG):
"""Enable HTTPConnection debug logging to the logging framework"""
def httpclient_log(*args):
httpclient_logger.log(level, " ".join(args))
http.client.print = httpclient_log
http.client.HTTPConnection.debuglevel = 1
# Get start time
starttime = timeit.default_timer()
logging.debug(f'Start time: {starttime}')
class HTTPClient:
"""Class for implementation of
the http conection.
"""
def __init__(self, host):
self.host = host
def get_data_from_host(self):
"""This function make a http request
with the urlopen method from urllib, from
get the data from http server.
Returns:
str: return the page content.
"""
request_data = urllib.request.urlopen(self.host)
data = request_data.read()
#text = data.decode('utf-8')
return data
if __name__ == "__main__":
# Using http logging
httpclient_logging()
# Setting the arguments
parser = argparse.ArgumentParser(description="HTTP client example")
parser.add_argument("--host", action="store",
help="host url",
dest="host", default=REMOTE_SERVER_HOST)
given_args = parser.parse_args()
host = given_args.host
# Using the class and get data from host
client = HTTPClient(host)
logging.debug(client.get_data_from_host())
# Show the the time of script
logging.debug(f'End time: {timeit.default_timer() - starttime}')