# coding=utf-8 # httpclient.py Lab 3 -- HTTP client # Team members: Name1 (username1), Name2 (username2) # import the "socket" module -- not using "from socket import *" in order to selectively use items with "socket." prefix import socket # import the "regular expressions" module import re def main(): # this resource request should result in "chunked" data transfer get_http_resource('http://seprof.sebern.com/', 'index.html') # this resource request should result in "Content-Length" data transfer get_http_resource('http://seprof.sebern.com/sebern1.jpg', 'sebern1.jpg') get_http_resource('http://seprof.sebern.com:8080/sebern1.jpg', 'sebern2.jpg') # another resource to try for a little larger and more complex entity # get_http_resource('http://seprof.sebern.com/courses/cs2910-2014-2015/sched.md','sched-file.md') # Get an HTTP resource from a server # Parse the URL and call function to actually make the request. # url -- full URL of the resource to get # file_name -- name of file in which to store the retrieved resource # (do not modify this function) def get_http_resource(url, file_name): # Parse the URL into its component parts using a regular expression. url_match = re.search('http://([^/:]*)(:\d*)?(/.*)', url) url_match_groups = url_match.groups() if url_match else [] # print 'url_match_groups=',url_match_groups if len(url_match_groups) == 3: host_name = url_match_groups[0] host_port = int(url_match_groups[1][1:]) if url_match_groups[1] else 80 host_resource = url_match_groups[2] print('host name = {0}, port = {1}, resource = {2}'.format(host_name, host_port, host_resource)) status_string = make_http_request(host_name.encode(), host_port, host_resource.encode(), file_name) print('get_http_resource: URL="{0}", status="{1}"'.format(url, status_string)) else: print('get_http_resource: URL parse failed, request not sent') # Get an HTTP resource from a server # host -- bytes object with the ASCII domain name or IP address of the server machine (i.e., host) to connect to # port -- integer port number to connect to on host # resource -- bytes object with the ASCII path/name of resource to get. This is everything in the URL after the domain name, including the # first /. # file_name -- string (str) containing name of file in which to store the retrieved resource # # Returns the status code as an integer def make_http_request(host, port, resource, file_name): return 500 # Replace this "server error" with the actual status code main()