From 0195d652571d58bf11564ffbd166e4c370cdcf4c Mon Sep 17 00:00:00 2001 From: Dan K Date: Mon, 16 Apr 2018 15:49:05 -0700 Subject: [PATCH 1/5] ScriptUpdates Made updates to echo client and echo server. These do not work yet. --- echo_client.py | 31 ++++++++++++++++++++----------- echo_server.py | 19 +++++++++++++------ 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/echo_client.py b/echo_client.py index 6b2f047..49838c0 100644 --- a/echo_client.py +++ b/echo_client.py @@ -6,8 +6,10 @@ 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) + print('connecting to {0} port {1}'.format(*server_address), file=log_buffer) + sock.connect(server_address) # TODO: connect your socket to the server here. # you can use this variable to accumulate the entire message received back @@ -19,21 +21,28 @@ def client(msg, log_buffer=sys.stderr): try: print('sending "{0}"'.format(msg), file=log_buffer) # TODO: send your message to the server here. + sock.sendall(msg) + while True: + chunk = sock.recv(16, file=log_buffer) + if not chunk: + print("No Data Received") + break + # 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 + # the entire message and then you can break the loop. + # + # Log each chunk you receive. Use the print statement below to + # do it. This will help in debugging problems + received_message = received_message + chunk + print('received "{0}"'.format(chunk.decode('utf8')), file=log_buffer) - # 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 - # the entire message and then you can break the loop. - # - # 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) finally: # TODO: after you break out of the loop receiving echoed chunks from # the server you will want to close your client socket. + sock.close() print('closing socket', file=log_buffer) - + print("Entire Recieved Message: {}".format(received_message)) # 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. diff --git a/echo_server.py b/echo_server.py index 44f853a..72a67b3 100644 --- a/echo_server.py +++ b/echo_server.py @@ -7,7 +7,8 @@ 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) + # 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 @@ -17,7 +18,8 @@ def server(log_buffer=sys.stderr): # log that we are building a server print("making a server on {0}:{1}".format(*address), file=log_buffer) - + sock.bind(address) + sock.listen(3) # TODO: bind your new sock 'sock' to the address above and begin to listen # for incoming connections @@ -32,7 +34,7 @@ def server(log_buffer=sys.stderr): # 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) @@ -45,9 +47,13 @@ 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'' + data = conn.recv(16) #b'' + if not data: + print("No data was received") + break + print('received "{0}"'.format(data.decode('utf8'))) - + conn.sendall(data, file=log_buffer) # I am guessing on that log buffer... # TODO: Send the data you received back to the client, log # the fact using the print statement here. It will help in # debugging problems. @@ -63,6 +69,7 @@ def server(log_buffer=sys.stderr): # :) finally: + sock.close() # TODO: When the inner loop exits, this 'finally' clause will # be hit. Use that opportunity to close the socket you # created above when a client connected. @@ -75,7 +82,7 @@ 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 + sock.close() print('quitting echo server', file=log_buffer) From 30aa77151a0716e2257d754435ad87e618674996 Mon Sep 17 00:00:00 2001 From: Dan K Date: Mon, 16 Apr 2018 16:34:27 -0700 Subject: [PATCH 2/5] More updates The echo client now sends correctly and the server gets the first 16 bytes. Everything else is borked still. --- echo_client.py | 17 +++++---- echo_server.py | 60 ++++++++--------------------- echo_server_clone.py | 91 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+), 52 deletions(-) create mode 100644 echo_server_clone.py diff --git a/echo_client.py b/echo_client.py index 49838c0..7e99799 100644 --- a/echo_client.py +++ b/echo_client.py @@ -21,7 +21,7 @@ def client(msg, log_buffer=sys.stderr): try: print('sending "{0}"'.format(msg), file=log_buffer) # TODO: send your message to the server here. - sock.sendall(msg) + sock.sendall(msg.encode('utf8')) while True: chunk = sock.recv(16, file=log_buffer) if not chunk: @@ -47,11 +47,12 @@ def client(msg, log_buffer=sys.stderr): # you received from the server as the return value of this function. -if __name__ == '__main__': - if len(sys.argv) != 2: - usage = '\nusage: python echo_client.py "this is my message"\n' - print(usage, file=sys.stderr) - sys.exit(1) +#if __name__ == '__main__': + # if len(sys.argv) != 2: + # usage = '\nusage: python echo_client.py "this is my message"\n' + # print(usage, file=sys.stderr) + # sys.exit(1) - msg = sys.argv[1] - client(msg) + # msg = sys.argv[1] + #client(msg) +client('hello my name is dan and I work in a buttonfactory') \ No newline at end of file diff --git a/echo_server.py b/echo_server.py index 72a67b3..f9cea22 100644 --- a/echo_server.py +++ b/echo_server.py @@ -8,55 +8,33 @@ def server(log_buffer=sys.stderr): # 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) - - # 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 - # in class. Find the correct option by reading the very end of the - # socket library documentation: - # http://docs.python.org/3/library/socket.html#example # log that we are building a server print("making a server on {0}:{1}".format(*address), file=log_buffer) - sock.bind(address) - sock.listen(3) - # TODO: bind your new sock 'sock' to the address above and begin to listen - # for incoming connections + try: + sock.bind(address) + except socket.error: #aborts program if there is an error + print("Unable to create socket.") + sock.close() + exit() + sock.listen(1) + print("Socket is listening for connections.") 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) - - # 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 + print('Waiting for a connection...', file=log_buffer) conn, addr = sock.accept() try: - print('connection - {0}:{1}'.format(*addr), file=log_buffer) - - # the inner loop will receive messages sent by the client in - # buffers. When a complete message has been received, the - # loop will exit + print('Connection - {0}:{1}'.format(*addr), file=log_buffer) while True: - # TODO: receive 16 bytes of data from the client. Store - # the data you receive as 'data'. Replace the - # following line with your code. It's only here as - # a placeholder to prevent an error in string - # formatting data = conn.recv(16) #b'' - if not data: - print("No data was received") - break - + #if not data: + # print("No data was received") + # break print('received "{0}"'.format(data.decode('utf8'))) conn.sendall(data, file=log_buffer) # I am guessing on that log buffer... - # TODO: Send the data you received back to the client, log - # the fact using the print statement here. It will help in - # debugging problems. print('sent "{0}"'.format(data.decode('utf8'))) # TODO: Check here to see whether you have received the end @@ -70,20 +48,14 @@ def server(log_buffer=sys.stderr): finally: sock.close() - # TODO: When the inner loop exits, this 'finally' clause will - # be hit. Use that opportunity to close the socket you - # created above when a client connected. print( - 'echo complete, client connection closed', file=log_buffer + 'Echo complete, client connection closed', file=log_buffer ) except KeyboardInterrupt: - # TODO: Use the python KeyboardInterrupt exception as a signal to - # close the server socket and exit from the server function. - # Replace the call to `pass` below, which is only there to - # prevent syntax problems sock.close() - print('quitting echo server', file=log_buffer) + print() + print('User Inturrupt: Closing Socket.', file=log_buffer) if __name__ == '__main__': diff --git a/echo_server_clone.py b/echo_server_clone.py new file mode 100644 index 0000000..72a67b3 --- /dev/null +++ b/echo_server_clone.py @@ -0,0 +1,91 @@ +import socket +import sys + + +def server(log_buffer=sys.stderr): + # set an address for our 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) + + # 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 + # in class. Find the correct option by reading the very end of the + # socket library documentation: + # http://docs.python.org/3/library/socket.html#example + + # log that we are building a server + print("making a server on {0}:{1}".format(*address), file=log_buffer) + sock.bind(address) + sock.listen(3) + # TODO: bind your new sock 'sock' to the address above and begin to listen + # for incoming connections + + 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) + + # 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 = sock.accept() + try: + print('connection - {0}:{1}'.format(*addr), file=log_buffer) + + # the inner loop will receive messages sent by the client in + # buffers. When a complete message has been received, the + # loop will exit + while True: + # TODO: receive 16 bytes of data from the client. Store + # the data you receive as 'data'. Replace the + # following line with your code. It's only here as + # a placeholder to prevent an error in string + # formatting + data = conn.recv(16) #b'' + if not data: + print("No data was received") + break + + print('received "{0}"'.format(data.decode('utf8'))) + conn.sendall(data, file=log_buffer) # I am guessing on that log buffer... + # TODO: Send the data you received back to the client, log + # the fact using the print statement here. It will help in + # debugging problems. + print('sent "{0}"'.format(data.decode('utf8'))) + + # TODO: Check here to see whether you have received the end + # of the message. If you have, then break from the `while True` + # loop. + # + # Figuring out whether or not you have received the end of the + # message is a trick we learned in the lesson: if you don't + # remember then ask your classmates or instructor for a clue. + # :) + + finally: + sock.close() + # TODO: When the inner loop exits, this 'finally' clause will + # be hit. Use that opportunity to close the socket you + # created above when a client connected. + print( + 'echo complete, client connection closed', file=log_buffer + ) + + except KeyboardInterrupt: + # TODO: Use the python KeyboardInterrupt exception as a signal to + # close the server socket and exit from the server function. + # Replace the call to `pass` below, which is only there to + # prevent syntax problems + sock.close() + print('quitting echo server', file=log_buffer) + + +if __name__ == '__main__': + server() + sys.exit(0) From 0208e0bc7fe78a7758ceca88fecd147956973a2d Mon Sep 17 00:00:00 2001 From: Dan K Date: Mon, 16 Apr 2018 17:32:31 -0700 Subject: [PATCH 3/5] semifunctional client and server will communicate with a custom message in client but tests fail. --- __pycache__/echo_client.cpython-36.pyc | Bin 0 -> 1039 bytes echo_client.py | 40 ++++++++++--------------- echo_server.py | 25 +++++----------- 3 files changed, 22 insertions(+), 43 deletions(-) create mode 100644 __pycache__/echo_client.cpython-36.pyc diff --git a/__pycache__/echo_client.cpython-36.pyc b/__pycache__/echo_client.cpython-36.pyc new file mode 100644 index 0000000000000000000000000000000000000000..b8a8f2130a70fdf3b7bd5eae38bb147be9e82495 GIT binary patch literal 1039 zcmYjQOK;Oa5Z<-dk2p;T4~fUgDo7{?2&fl82&hVVRV}CmftG{idMAk!$1b~@s7aGk zxWfP7-*DmLE2sPgPRu4P)bh-ZXTF*FW~|+))q>AYfAM{fkl*Co!_a<)qS~kg5i}!w zby7}&_N+D)maxB4ZUYmnZ^=V&(C^NvaQe1C$qAf1U6E zYzT};&;!z}EU*{ZODuEXPNq~a(`Rb; zoT;^}>de?gBEm%knbUI!cS?Gm@aELuj^AL8Z@A5zDVsXT6C+p2jA3pCb5A}0E9hYA z;G}+^NRF804DZMn{*ipdEn&6loKS+1>RC^;*RT>>b^L@VoVdPMRgEqH&v$ z$&IAQb4YZWcS2o+C--LIQ6Y7Ba(~vOC2OY{lw?vJk!2 zM<7G>1Z+9p4t*lTc?Vw2dpeI*$L8-U{;XWiOVA7-TWY)B#`2*5M)m$rG zwZw97^F{P}Z~Hy39=zS!i4NZHZ*T6F&XG)WUApZ;4r0x{Q}*051mi3#UC5I{0B6W+ zRJsDpS7~REmrjCgpy`(OKy`SK6`kmC*lvU5bp>(+GKz(e$dEI`fj7=Ziv}y&SfM-2 z`#*@hh+O|XY<<8y)!G>*-8gSOFOuN^a;;h~Q~hdq*n*^6+*ws#+EJ3F7{7ZoF5Q*s zU>S7_6`{;-&`UIM7_;zfP>Tk%POs55>eGPvc*@I&EBz=M#AzNy3$wJzMlduBeH0#z zb+^bv)AN6ya9wv(72-P>ho?iV2Tdx?bz4wW&8qZGSzC=2u3ig}@?WDo9zYaHb0o}(_nXvzOP>5j$PfQzSTt4;f`b{6lVkm(X(7)Y=4I=;m literal 0 HcmV?d00001 diff --git a/echo_client.py b/echo_client.py index 7e99799..7ef776a 100644 --- a/echo_client.py +++ b/echo_client.py @@ -7,7 +7,6 @@ def client(msg, log_buffer=sys.stderr): # 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) - print('connecting to {0} port {1}'.format(*server_address), file=log_buffer) sock.connect(server_address) # TODO: connect your socket to the server here. @@ -20,39 +19,30 @@ def client(msg, log_buffer=sys.stderr): # when we are finished with it try: print('sending "{0}"'.format(msg), file=log_buffer) - # TODO: send your message to the server here. sock.sendall(msg.encode('utf8')) while True: - chunk = sock.recv(16, file=log_buffer) - if not chunk: - print("No Data Received") - break - # 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 - # the entire message and then you can break the loop. - # - # Log each chunk you receive. Use the print statement below to - # do it. This will help in debugging problems - received_message = received_message + chunk + chunk = sock.recv(16) + received_message = received_message + chunk.decode('utf8') print('received "{0}"'.format(chunk.decode('utf8')), file=log_buffer) - + if len(chunk) < 16: + print("Reached EOM") + break finally: # TODO: after you break out of the loop receiving echoed chunks from # the server you will want to close your client socket. sock.close() - print('closing socket', file=log_buffer) - print("Entire Recieved Message: {}".format(received_message)) + print('Closing Socket', file=log_buffer) + print("Message Recieved: {}".format(received_message)) # 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. -#if __name__ == '__main__': - # if len(sys.argv) != 2: - # usage = '\nusage: python echo_client.py "this is my message"\n' - # print(usage, file=sys.stderr) - # sys.exit(1) +if __name__ == '__main__': + if len(sys.argv) != 2: + usage = '\nusage: python echo_client.py "this is my message"\n' + print(usage, file=sys.stderr) + sys.exit(1) - # msg = sys.argv[1] - #client(msg) -client('hello my name is dan and I work in a buttonfactory') \ No newline at end of file + msg = sys.argv[1] + client(msg) +#client('hello my name is dan and I work in a button factory') \ No newline at end of file diff --git a/echo_server.py b/echo_server.py index f9cea22..2881eaa 100644 --- a/echo_server.py +++ b/echo_server.py @@ -13,7 +13,7 @@ def server(log_buffer=sys.stderr): print("making a server on {0}:{1}".format(*address), file=log_buffer) try: sock.bind(address) - except socket.error: #aborts program if there is an error + except socket.error: # aborts program if there is an error print("Unable to create socket.") sock.close() exit() @@ -29,29 +29,18 @@ def server(log_buffer=sys.stderr): try: print('Connection - {0}:{1}'.format(*addr), file=log_buffer) while True: - data = conn.recv(16) #b'' - #if not data: - # print("No data was received") - # break + data = conn.recv(16) print('received "{0}"'.format(data.decode('utf8'))) - conn.sendall(data, file=log_buffer) # I am guessing on that log buffer... + conn.sendall(data) print('sent "{0}"'.format(data.decode('utf8'))) - - # TODO: Check here to see whether you have received the end - # of the message. If you have, then break from the `while True` - # loop. - # - # Figuring out whether or not you have received the end of the - # 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) < 16: + print("Reached EOM") + break + sock.close() finally: - sock.close() print( 'Echo complete, client connection closed', file=log_buffer ) - except KeyboardInterrupt: sock.close() print() From 35bab36a121e83e38a63c7e02d456a842170cf60 Mon Sep 17 00:00:00 2001 From: Dan K Date: Mon, 16 Apr 2018 17:48:54 -0700 Subject: [PATCH 4/5] Assignment Complete I think I am done. Both tests passed. I was hitting my head against the wall before realizing I never returned any info from client. --- __pycache__/echo_client.cpython-36.pyc | Bin 1039 -> 1041 bytes echo_client.py | 1 + 2 files changed, 1 insertion(+) diff --git a/__pycache__/echo_client.cpython-36.pyc b/__pycache__/echo_client.cpython-36.pyc index b8a8f2130a70fdf3b7bd5eae38bb147be9e82495..e6d04519a22f81a3bf5d487690a9e26411a30a04 100644 GIT binary patch delta 53 zcmeC@n8?9t%*)HQ$N6eh-bT(sMy48;%{7cO7#Y Date: Mon, 16 Apr 2018 18:22:58 -0700 Subject: [PATCH 5/5] cleaning cleaned up client a tad --- echo_client.py | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/echo_client.py b/echo_client.py index 7363050..6344f1e 100644 --- a/echo_client.py +++ b/echo_client.py @@ -9,14 +9,7 @@ def client(msg, log_buffer=sys.stderr): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) print('connecting to {0} port {1}'.format(*server_address), file=log_buffer) sock.connect(server_address) - # TODO: connect your socket to the server here. - - # you can use this variable to accumulate the entire message received back - # from the server received_message = '' - - # this try/finally block exists purely to allow us to close the socket - # when we are finished with it try: print('sending "{0}"'.format(msg), file=log_buffer) sock.sendall(msg.encode('utf8')) @@ -28,16 +21,11 @@ def client(msg, log_buffer=sys.stderr): print("Reached EOM") break finally: - # TODO: after you break out of the loop receiving echoed chunks from - # the server you will want to close your client socket. sock.close() print('Closing Socket', file=log_buffer) print("Message Recieved: {}".format(received_message)) - # 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. return received_message - if __name__ == '__main__': if len(sys.argv) != 2: usage = '\nusage: python echo_client.py "this is my message"\n'