#!/usr/bin/env python #Basic Connection Example - Chapter 2 - sockets2.py import socket, sys filename = sys.argv[1] # Uses command-line argument for filename print "\nCreating a socket... ", s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Create socket s( Uses IPv4 , Uses TCP ) print "and I am finished!" print "Looking up port number... ", port = socket.getservbyname('ssh', 'tcp') # Finds port for ( Port name, Protocol name ) print "Oh I got that!" print "Connecting that socket to a remote host on port %d... " % port, s.connect(("cs.marlboro.edu", port)) print "and I'm done yet again!\n" print "Connected from", s.getsockname() print "Connected to", s.getpeername() print "" s.sendall("GET %s HTTP/1.0\r\n\r\n" % filename) while(1): try: buf = s.recv(2048) except socket.error, e: print "Error receiving data: %s" % e sys.exit(1) if not len(buf): break sys.stdout.write(buf)