''' Created based on the information from: https://docs.python.org/3/library/http.client.html Xiannong Meng 2015-09-12 ''' import http.client import sys class MyHttpClient(): def __init__(self, s, p = 80): """ Creates the client object with server url 's'. Parameters: s: host name in the form of www.abc.com p: port number, default is 80 Pre: s is a valid www host name Post: a connection is established so the client can retrieve information from the server. If the response code is not 200 (OK), the client simply quits. """ # print("host : port : ", s, p) self.conn = http.client.HTTPConnection(s, p) def request_HEAD(self, page): """ Request header information. Pre: a connection has been established with the server. Post: the header information is retrieved. """ self.conn.request("HEAD", page) self.res = None try: self.res = self.conn.getresponse() if (self.res.status != 200): print("Response code error."); sys.exit(1) else: data = self.res.getheaders() print(data) except http.client.ResponseNotReady: print("sorry, server is not ready.") except http.client.BadStatusLine: print("sorry, bad status line.") def request_page(self, page): """ Request a particular web page Pre: a connection has been established with the server. Post: the specified page is retrieved. Parameters: page: the web page to be retrieved """ self.conn.request("GET", page) self.res = None try: self.res = self.conn.getresponse() if (self.res.status != 200): print("Response code error."); sys.exit(1) else: data = self.res.read() self.data_type = self.parse_header(self.res.msg) if self.data_type.cat == "text": print(data) elif self.data_type.cat == "image": wf_name = "outf." + self.data_type.type f = open(wf_name, "wb") f.write(data); f.close except http.client.ResponseNotReady: print("sorry, server is not ready.") except http.client.BadStatusLine: print("sorry, bad status line.") def parse_header(self, msg): """ Parse the information from HTTP header (at this moment, only the category and type are extracted) Parameters: msg: the HTTP header Return: an object with two fields, 'cat' and 'type' """ c_type = self.res.getheader("Content-type") content = type('typeclass', (object,), {'cat':'', 'type':''})() content.cat = "text" # default! content.type = "html" if c_type.find("image") != -1: content.cat = "image" if c_type.find("jpeg") != -1: content.type = "jpeg" elif c_type.find("png") != -1: content.type = "png" return content def print_info(self): """ Print informatoin about this object """ print("Hi I am here.") if __name__ == "__main__": """ sys.argv[1]: host in the form of "www.host.com" sys.argv[2]: port, default 80 sys.argv[3]: web page, in the form of "/index.html" """ if len(sys.argv) != 4: print("usage: python httpclient.py host port path") sys.exit(1) client = MyHttpClient(sys.argv[1], sys.argv[2]) # client.print_info() client.request_HEAD(sys.argv[3]) # client.request_page(sys.argv[3])