Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Echo Server Completion #3

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 62 additions & 6 deletions echo_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,99 @@


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 = None
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.

# 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
# when we are finished with it
try:
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
# 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)
except Exception as e:
while True:
chunk = sock.recv(buffer_size)

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)
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)
return received_message

def port_range(upper, lower):
'''
lists the services provided by a given range of ports
'''

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 = upper
upper = lower
lower = place_holder
print("Upper = {0}, Lower= {1}".format(upper, lower))

#Determine if Ports are reserved (numbered 0 - 1023)
if upper < 1023:
print("These ports are reserved, Do Not Use")
#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():
'''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(int(upper), int(lower))
return

if __name__ == '__main__':
if len(sys.argv) != 2:
Expand Down
22 changes: 15 additions & 7 deletions echo_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,18 +22,21 @@ 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)
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)

# 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)

Expand All @@ -46,12 +49,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.
# 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
Expand All @@ -62,7 +67,9 @@ 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.
# :)
except Exception as e:
if len(data) < buffer_size:
break
except Exception:
traceback.print_exc()
sys.exit(1)
finally:
Expand All @@ -72,14 +79,15 @@ 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
# 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__':
Expand Down