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