Compare commits
2 commits
f3f5c2dd72
...
53e74fdd79
Author | SHA1 | Date | |
---|---|---|---|
53e74fdd79 | |||
a1b31fea9d |
|
@ -30,14 +30,15 @@ def _parse_args():
|
|||
return args
|
||||
|
||||
|
||||
def answer():
|
||||
def answer(addr):
|
||||
"""
|
||||
Returns an answer to the connection.
|
||||
|
||||
:return: A string.
|
||||
"""
|
||||
|
||||
return "Gotcha.\n"
|
||||
out = f"Hello {addr[0]}! You are using port {addr[1]}. Have a nice day!\n"
|
||||
|
||||
return out
|
||||
|
||||
|
||||
def server(port: int=4444):
|
||||
|
@ -54,7 +55,7 @@ def server(port: int=4444):
|
|||
while True:
|
||||
c, addr = s.accept()
|
||||
print('Got connection from', addr)
|
||||
c.send(answer().encode('utf-8'))
|
||||
c.send(answer(addr).encode('utf-8'))
|
||||
c.close()
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
|
@ -33,6 +33,8 @@ def _parse_args():
|
|||
print("Please provide a port for the connection.")
|
||||
print(parser.print_help())
|
||||
sys.exit(1)
|
||||
else:
|
||||
print("Got port option set to:", args.port)
|
||||
|
||||
return args
|
||||
|
||||
|
@ -47,7 +49,7 @@ def answer():
|
|||
return "Gotcha.\n"
|
||||
|
||||
|
||||
def server(port: int=4444):
|
||||
def server(port: int):
|
||||
"""
|
||||
Opens a socket on a given port and listens to incoming
|
||||
requests. When a request comes in, accepts the connection,
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
"""
|
||||
This module plays with sockets.
|
||||
|
||||
More info.
|
||||
Fill in more info, explaining what this module does and how to use it.
|
||||
|
||||
"""
|
||||
|
||||
|
@ -23,10 +23,8 @@ def _parse_args():
|
|||
"""
|
||||
Parses arguments from command line.
|
||||
"""
|
||||
parser = argparse.ArgumentParser(description="Utilities to use network sockets.")
|
||||
parser.add_argument('-p', '--port',
|
||||
help='Port number to open')
|
||||
args = parser.parse_args()
|
||||
#parser = ...
|
||||
|
||||
return args
|
||||
|
||||
|
||||
|
@ -40,7 +38,7 @@ def answer():
|
|||
return "Gotcha.\n"
|
||||
|
||||
|
||||
def server(port: int=4444):
|
||||
def server(port: int):
|
||||
"""
|
||||
Opens a socket on a given port and listens to incoming
|
||||
requests. When a request comes in, accepts the connection,
|
||||
|
@ -48,14 +46,15 @@ def server(port: int=4444):
|
|||
|
||||
:param port: Port number to listen on.
|
||||
"""
|
||||
print("Starting server..")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
# Parse command-line arguments
|
||||
args = _parse_args()
|
||||
|
||||
|
||||
# Start our server
|
||||
server(int(args.port))
|
||||
#server(...)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue