####################################################################### # fetch a file from an http (web) server over sockets via httplib; # the filename param may have a full directory path, and may name a cgi # script with query parameters on the end to invoke a remote program; # fetched file data or remote program output could be saved to a local # file to mimic ftp, or parsed with str.find or the htmllib module; ####################################################################### import sys, http.client showlines = 6 try: servername, filename = sys.argv[1:] # cmdline args? except: servername, filename = 'www.eg.bucknell.edu', '/~cs479/index.html' print(servername, filename) server = http.client.HTTPConnection(servername) # connect to http site/server server.putrequest('HEAD', filename) # send request and headers #server.putrequest('GET', filename) # send request and headers server.putheader('Accept', 'text/html') # POST requests work here too server.endheaders() # as do cgi script file names response = server.getresponse() print( 'response code: ', response.status ) print( 'response:\n', response.msg ) print( '---end of response---' ) # read reply info headers if response.status != 200: # 200 means success print('Error sending request', response.status) else: data = response.read() print( 'data size : ', len( data ) ) # for line in data[:showlines]: print(line) print( 'data : ', data ) server.close()