| 
 | 1 | +import socket  | 
 | 2 | +import sys  | 
 | 3 | + | 
 | 4 | + | 
 | 5 | +def server(log_buffer=sys.stderr):  | 
 | 6 | +    # set an address for our server  | 
 | 7 | +    address = ('127.0.0.1', 10000)  | 
 | 8 | +    # TODO: Replace the following line with your code which will instantiate  | 
 | 9 | +    #       a TCP socket with IPv4 Addressing, call the socket you make 'sock'  | 
 | 10 | +    sock = None  | 
 | 11 | +    # TODO: You may find that if you repeatedly run the server script it fails,  | 
 | 12 | +    #       claiming that the port is already used.  You can set an option on  | 
 | 13 | +    #       your socket that will fix this problem. We DID NOT talk about this  | 
 | 14 | +    #       in class. Find the correct option by reading the very end of the  | 
 | 15 | +    #       socket library documentation:  | 
 | 16 | +    #       http://docs.python.org/3/library/socket.html#example  | 
 | 17 | + | 
 | 18 | +    # log that we are building a server  | 
 | 19 | +    print("making a server on {0}:{1}".format(*address), file=log_buffer)  | 
 | 20 | + | 
 | 21 | +    # TODO: bind your new sock 'sock' to the address above and begin to listen  | 
 | 22 | +    #       for incoming connections  | 
 | 23 | + | 
 | 24 | +    try:  | 
 | 25 | +        # the outer loop controls the creation of new connection sockets. The  | 
 | 26 | +        # server will handle each incoming connection one at a time.  | 
 | 27 | +        while True:  | 
 | 28 | +            print('waiting for a connection', file=log_buffer)  | 
 | 29 | + | 
 | 30 | +            # TODO: make a new socket when a client connects, call it 'conn',  | 
 | 31 | +            #       at the same time you should be able to get the address of  | 
 | 32 | +            #       the client so we can report it below.  Replace the  | 
 | 33 | +            #       following line with your code. It is only here to prevent  | 
 | 34 | +            #       syntax errors  | 
 | 35 | +            conn, addr = ('foo', ('bar', 'baz'))  | 
 | 36 | +            try:  | 
 | 37 | +                print('connection - {0}:{1}'.format(*addr), file=log_buffer)  | 
 | 38 | + | 
 | 39 | +                # the inner loop will receive messages sent by the client in  | 
 | 40 | +                # buffers.  When a complete message has been received, the  | 
 | 41 | +                # loop will exit  | 
 | 42 | +                while True:  | 
 | 43 | +                    # TODO: receive 16 bytes of data from the client. Store  | 
 | 44 | +                    #       the data you receive as 'data'.  Replace the  | 
 | 45 | +                    #       following line with your code.  It's only here as  | 
 | 46 | +                    #       a placeholder to prevent an error in string  | 
 | 47 | +                    #       formatting  | 
 | 48 | +                    data = b''  | 
 | 49 | +                    print('received "{0}"'.format(data.decode('utf8')))  | 
 | 50 | +                    # TODO: Send the data you received back to the client, log  | 
 | 51 | +                    # the fact using the print statement here.  It will help in  | 
 | 52 | +                    # debugging problems.  | 
 | 53 | +                    print('sent "{0}"'.format(data.decode('utf8')))  | 
 | 54 | +                    # TODO: Check here to see if the message you've received is  | 
 | 55 | +                    # complete.  If it is, break out of this inner loop.  | 
 | 56 | + | 
 | 57 | +            finally:  | 
 | 58 | +                # TODO: When the inner loop exits, this 'finally' clause will  | 
 | 59 | +                #       be hit. Use that opportunity to close the socket you  | 
 | 60 | +                #       created above when a client connected.  | 
 | 61 | +                print(  | 
 | 62 | +                    'echo complete, client connection closed', file=log_buffer  | 
 | 63 | +                )  | 
 | 64 | + | 
 | 65 | +    except KeyboardInterrupt:  | 
 | 66 | +        # TODO: Use the python KeyboardInterrupt exception as a signal to  | 
 | 67 | +        #       close the server socket and exit from the server function.  | 
 | 68 | +        #       Replace the call to `pass` below, which is only there to  | 
 | 69 | +        #       prevent syntax problems  | 
 | 70 | +        pass  | 
 | 71 | +        print('quitting echo server', file=log_buffer)  | 
 | 72 | + | 
 | 73 | + | 
 | 74 | +if __name__ == '__main__':  | 
 | 75 | +    server()  | 
 | 76 | +    sys.exit(0)  | 
0 commit comments