# 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-file.md') # 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','sebern1.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,host_port,host_resource,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 -- the domain name or IP address of the server (i.e., host) to connect to # port -- port number to connect to on host # resource -- path/name of resource to get. This is everything in the URL after the domain name, including the first /. # file_name -- name of file in which to store the retrieved resource def make_http_request(host, port, resource, file_name): pass # Replace this line with your cod main()