From 41d761c853b6c2ab149269c6e963ae20f49e1a43 Mon Sep 17 00:00:00 2001 From: Ian Mayther Date: Sat, 17 Apr 2021 16:52:38 -0700 Subject: [PATCH 1/8] Completed echo_server --- echo_server.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/echo_server.py b/echo_server.py index 299f009..e686bb7 100644 --- a/echo_server.py +++ b/echo_server.py @@ -8,7 +8,7 @@ def server(log_buffer=sys.stderr): address = ('127.0.0.1', 10000) # TODO: Replace the following line with your code which will instantiate # a TCP socket with IPv4 Addressing, call the socket you make 'sock' - sock = None + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP) # TODO: You may find that if you repeatedly run the server script it fails, # claiming that the port is already used. You can set an option on # your socket that will fix this problem. We DID NOT talk about this @@ -22,18 +22,20 @@ def server(log_buffer=sys.stderr): # TODO: bind your new sock 'sock' to the address above and begin to listen # for incoming connections + sock.bind(address) + try: # the outer loop controls the creation of new connection sockets. The # server will handle each incoming connection one at a time. while True: print('waiting for a connection', file=log_buffer) - + sock.listen(1) # TODO: make a new socket when a client connects, call it 'conn', # at the same time you should be able to get the address of # the client so we can report it below. Replace the # following line with your code. It is only here to prevent # syntax errors - conn, addr = ('foo', ('bar', 'baz')) + conn, addr = sock.accept() try: print('connection - {0}:{1}'.format(*addr), file=log_buffer) @@ -46,12 +48,14 @@ def server(log_buffer=sys.stderr): # following line with your code. It's only here as # a placeholder to prevent an error in string # formatting - data = b'' + buffer_size = 16 + data = conn.recv(buffer_size) print('received "{0}"'.format(data.decode('utf8'))) # TODO: Send the data you received back to the client, log # the fact using the print statement here. It will help in # debugging problems. + conn.sendall(data.encode('utf8')) print('sent "{0}"'.format(data.decode('utf8'))) # TODO: Check here to see whether you have received the end @@ -62,6 +66,8 @@ def server(log_buffer=sys.stderr): # message is a trick we learned in the lesson: if you don't # remember then ask your classmates or instructor for a clue. # :) + if len(data) < buffer_size: + break except Exception as e: traceback.print_exc() sys.exit(1) From 6b11347772c25613cf8570f28123dc58b02448ab Mon Sep 17 00:00:00 2001 From: Ian Mayther Date: Sat, 17 Apr 2021 17:04:06 -0700 Subject: [PATCH 2/8] Completed echo_client --- echo_client.py | 20 ++++++++++++++------ echo_server.py | 3 ++- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/echo_client.py b/echo_client.py index a2e9b29..ae4280b 100644 --- a/echo_client.py +++ b/echo_client.py @@ -7,12 +7,13 @@ def client(msg, log_buffer=sys.stderr): server_address = ('localhost', 10000) # TODO: Replace the following line with your code which will instantiate # a TCP socket with IPv4 Addressing, call the socket you make 'sock' - sock = None + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_IP) print('connecting to {0} port {1}'.format(*server_address), file=log_buffer) # TODO: connect your socket to the server here. # you can use this variable to accumulate the entire message received back # from the server + buffer_size = 16 received_message = '' # this try/finally block exists purely to allow us to close the socket @@ -20,7 +21,8 @@ def client(msg, log_buffer=sys.stderr): try: print('sending "{0}"'.format(msg), file=log_buffer) # TODO: send your message to the server here. - + my_message = input("> ") + sock.sendall(my_message.encode('utf-8')) # TODO: the server should be sending you back your message as a series # of 16-byte chunks. Accumulate the chunks you get to build the # entire reply from the server. Make sure that you have received @@ -28,18 +30,24 @@ def client(msg, log_buffer=sys.stderr): # # Log each chunk you receive. Use the print statement below to # do it. This will help in debugging problems - chunk = '' - print('received "{0}"'.format(chunk.decode('utf8')), file=log_buffer) - except Exception as e: + while True: + chunk = sock.recv(buffer_size) + if len(chunk) < buffer_size: + break + + print('received "{0}"'.format(chunk.decode('utf8')), file=log_buffer) + received_message += chunk.decode('utf8') + except Exception: traceback.print_exc() sys.exit(1) finally: # TODO: after you break out of the loop receiving echoed chunks from # the server you will want to close your client socket. print('closing socket', file=log_buffer) - + sock.close() # TODO: when all is said and done, you should return the entire reply # you received from the server as the return value of this function. + print(received_message) if __name__ == '__main__': diff --git a/echo_server.py b/echo_server.py index e686bb7..26d39e9 100644 --- a/echo_server.py +++ b/echo_server.py @@ -68,7 +68,7 @@ def server(log_buffer=sys.stderr): # :) if len(data) < buffer_size: break - except Exception as e: + except Exception: traceback.print_exc() sys.exit(1) finally: @@ -78,6 +78,7 @@ def server(log_buffer=sys.stderr): print( 'echo complete, client connection closed', file=log_buffer ) + conn.close() except KeyboardInterrupt: # TODO: Use the python KeyboardInterrupt exception as a signal to From ecf18117a0768814ad268394050bdac18bf51d31 Mon Sep 17 00:00:00 2001 From: Ian Mayther Date: Mon, 19 Apr 2021 06:03:42 -0700 Subject: [PATCH 3/8] Servers will talk, some echos --- echo_client.py | 3 ++- echo_server.py | 9 +++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/echo_client.py b/echo_client.py index ae4280b..a1e93b3 100644 --- a/echo_client.py +++ b/echo_client.py @@ -4,10 +4,11 @@ def client(msg, log_buffer=sys.stderr): - server_address = ('localhost', 10000) + server_address = ('127.0.0.1', 10000) # TODO: Replace the following line with your code which will instantiate # a TCP socket with IPv4 Addressing, call the socket you make 'sock' sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_IP) + sock.connect(server_address) print('connecting to {0} port {1}'.format(*server_address), file=log_buffer) # TODO: connect your socket to the server here. diff --git a/echo_server.py b/echo_server.py index 26d39e9..38237f8 100644 --- a/echo_server.py +++ b/echo_server.py @@ -23,13 +23,14 @@ def server(log_buffer=sys.stderr): # for incoming connections sock.bind(address) + sock.listen() try: # the outer loop controls the creation of new connection sockets. The # server will handle each incoming connection one at a time. while True: print('waiting for a connection', file=log_buffer) - sock.listen(1) + # TODO: make a new socket when a client connects, call it 'conn', # at the same time you should be able to get the address of # the client so we can report it below. Replace the @@ -54,8 +55,8 @@ def server(log_buffer=sys.stderr): # TODO: Send the data you received back to the client, log # the fact using the print statement here. It will help in - # debugging problems. - conn.sendall(data.encode('utf8')) + # debugging problems..encode('utf8') + conn.sendall(data) print('sent "{0}"'.format(data.decode('utf8'))) # TODO: Check here to see whether you have received the end @@ -85,8 +86,8 @@ def server(log_buffer=sys.stderr): # close the server socket and exit from the server function. # Replace the call to `pass` below, which is only there to # prevent syntax problems - pass print('quitting echo server', file=log_buffer) + conn.close() if __name__ == '__main__': From a73b5b51f313d23ed6ba8e1a3e88d750b05cc7e2 Mon Sep 17 00:00:00 2001 From: Ian Mayther Date: Mon, 19 Apr 2021 20:42:27 -0700 Subject: [PATCH 4/8] Relocated break on while loop in client --- echo_client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/echo_client.py b/echo_client.py index a1e93b3..0beff01 100644 --- a/echo_client.py +++ b/echo_client.py @@ -33,11 +33,11 @@ def client(msg, log_buffer=sys.stderr): # do it. This will help in debugging problems while True: chunk = sock.recv(buffer_size) - if len(chunk) < buffer_size: - break print('received "{0}"'.format(chunk.decode('utf8')), file=log_buffer) received_message += chunk.decode('utf8') + if len(chunk) < buffer_size: + break except Exception: traceback.print_exc() sys.exit(1) From ec007d7e847340f7709f13d34ca01627eb9686dc Mon Sep 17 00:00:00 2001 From: Ian Mayther Date: Mon, 19 Apr 2021 21:03:56 -0700 Subject: [PATCH 5/8] Tests.py passed --- echo_client.py | 1 + 1 file changed, 1 insertion(+) diff --git a/echo_client.py b/echo_client.py index 0beff01..5c17784 100644 --- a/echo_client.py +++ b/echo_client.py @@ -49,6 +49,7 @@ def client(msg, log_buffer=sys.stderr): # TODO: when all is said and done, you should return the entire reply # you received from the server as the return value of this function. print(received_message) + return received_message if __name__ == '__main__': From 16f0d531de4eef6507764fb8279c76b6b7f6524b Mon Sep 17 00:00:00 2001 From: Ian Mayther Date: Tue, 20 Apr 2021 06:01:43 -0700 Subject: [PATCH 6/8] Creating list of ports --- echo_client.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/echo_client.py b/echo_client.py index 5c17784..6ddbc18 100644 --- a/echo_client.py +++ b/echo_client.py @@ -23,6 +23,8 @@ def client(msg, log_buffer=sys.stderr): print('sending "{0}"'.format(msg), file=log_buffer) # TODO: send your message to the server here. my_message = input("> ") + if my_message.lower() == "Port Range": + port_range_setup() sock.sendall(my_message.encode('utf-8')) # TODO: the server should be sending you back your message as a series # of 16-byte chunks. Accumulate the chunks you get to build the @@ -51,6 +53,40 @@ def client(msg, log_buffer=sys.stderr): print(received_message) return received_message +def port_range(upper, lower): + ''' + lists the services provided by a given range of ports + ''' + + # Ports numbered 0 - 1023 are reserved + # Ports numbered 1024 - 65535 are open + # Ports numbered 1024 - 49151 may be registered + # Ports numbered 49152 - 65535 are called ephemera + if upper is not int or lower is not int: + print("Inputs must be integers") + return + + if upper < 0 or lower < 0: + print("Inputs can not be negative") + return + + if upper > 65535 or lower > 65535: + print("Inputs must be less than 65535") + return + + if lower > upper: + place_holder = lower + upper = lower + lower = place_holder + + pass + +def port_range_setup(): + '''setup inputs for port range''' + upper = input('Select an upper limit for port list: ') + lower = input('Select a lower limit for port list: ') + port_range(upper, lower) + return if __name__ == '__main__': if len(sys.argv) != 2: From a3b826866b0bd42567e9986e335f7baa0116250e Mon Sep 17 00:00:00 2001 From: Ian Mayther Date: Wed, 21 Apr 2021 06:01:52 -0700 Subject: [PATCH 7/8] Control for upper and lower put inplace --- echo_client.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/echo_client.py b/echo_client.py index 6ddbc18..de79ad7 100644 --- a/echo_client.py +++ b/echo_client.py @@ -23,8 +23,9 @@ def client(msg, log_buffer=sys.stderr): print('sending "{0}"'.format(msg), file=log_buffer) # TODO: send your message to the server here. my_message = input("> ") - if my_message.lower() == "Port Range": + if my_message.lower() == "port range": port_range_setup() + print(my_message.lower()) sock.sendall(my_message.encode('utf-8')) # TODO: the server should be sending you back your message as a series # of 16-byte chunks. Accumulate the chunks you get to build the @@ -62,9 +63,6 @@ def port_range(upper, lower): # Ports numbered 1024 - 65535 are open # Ports numbered 1024 - 49151 may be registered # Ports numbered 49152 - 65535 are called ephemera - if upper is not int or lower is not int: - print("Inputs must be integers") - return if upper < 0 or lower < 0: print("Inputs can not be negative") @@ -75,17 +73,25 @@ def port_range(upper, lower): return if lower > upper: - place_holder = lower + place_holder = upper upper = lower lower = place_holder + print("Upper = {0}, Lower= {1}".format(upper, lower)) + #Determine Bucket "A" + if upper < 1023: + print("These ports are reserved, Do Not Use") + #Determine Bucket "B" + if upper < 49151: + print(upper) + #Determine Bucket "C" pass def port_range_setup(): '''setup inputs for port range''' upper = input('Select an upper limit for port list: ') lower = input('Select a lower limit for port list: ') - port_range(upper, lower) + port_range(int(upper), int(lower)) return if __name__ == '__main__': From 56eadf90bcc87af786680c38f8ea3ec743cd72ef Mon Sep 17 00:00:00 2001 From: Ian Mayther Date: Thu, 22 Apr 2021 05:48:15 -0700 Subject: [PATCH 8/8] Completed port range optional task --- echo_client.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/echo_client.py b/echo_client.py index de79ad7..e71bc67 100644 --- a/echo_client.py +++ b/echo_client.py @@ -25,7 +25,7 @@ def client(msg, log_buffer=sys.stderr): my_message = input("> ") if my_message.lower() == "port range": port_range_setup() - print(my_message.lower()) + sock.sendall(my_message.encode('utf-8')) # TODO: the server should be sending you back your message as a series # of 16-byte chunks. Accumulate the chunks you get to build the @@ -59,11 +59,6 @@ def port_range(upper, lower): lists the services provided by a given range of ports ''' - # Ports numbered 0 - 1023 are reserved - # Ports numbered 1024 - 65535 are open - # Ports numbered 1024 - 49151 may be registered - # Ports numbered 49152 - 65535 are called ephemera - if upper < 0 or lower < 0: print("Inputs can not be negative") return @@ -78,13 +73,22 @@ def port_range(upper, lower): lower = place_holder print("Upper = {0}, Lower= {1}".format(upper, lower)) - #Determine Bucket "A" + #Determine if Ports are reserved (numbered 0 - 1023) if upper < 1023: print("These ports are reserved, Do Not Use") - #Determine Bucket "B" - if upper < 49151: - print(upper) - #Determine Bucket "C" + #Determine if Ports may be registered (numbered 1024 - 49151) + elif upper < 49151 and lower < 1024: + print("This range contains reserved and registerd ports") + elif upper < 49151 and lower > 1023: + print("This range contains registerd ports") + #Determine if Ports are called ephemera (numbered 49152 - 65535) + elif upper < 65535 and lower < 1024: + print("This range contains ephemera and reserved ports") + elif upper < 65535 and lower < 49152: + print("This range contains registerd and ephemera ports") + elif upper < 65535 and lower > 49151: + print("This range contains ephemera ports") + pass def port_range_setup():