Skip to content

Commit bd34ee0

Browse files
committed
add the ability to use command line arguments for port and id
1 parent 5d4bc83 commit bd34ee0

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

network_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ def main():
1616
"""setup client process and connect to a server defined by user input"""
1717

1818
#get ip and port
19-
host_ip = input("enter the ip to connect to:\n")
20-
port = get_port()
19+
host_ip = get_ip()
20+
port = get_port(CLIENT_LOG_FILE)
2121
log_message(CLIENT_LOG_FILE, f"----------------------\np:{port}||ip:{host_ip}")
2222

2323
#setup socket

network_utilities.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from ClientName import ClientName
33
from configfile import *
44
from socket import SocketType
5-
import os
5+
import os, sys
66

77

88
def load_names(client_names:list[ClientName], filename:str):
@@ -96,9 +96,27 @@ def send_message(to_send:str,client:SocketType,addr:tuple[str, int]):
9696
client.send(to_send.encode('utf-8'))
9797
log_message(LOG_FILE, f"sent message ({ORIG}) to {addr}")
9898

99+
def get_ip() -> str:
100+
"""gets an ip from arguments if available, otherwise user input. has no ip checking"""
101+
if len(sys.argv) >= 2:
102+
return sys.argv[1]
103+
else:
104+
return input("enter the ip to connect to:\n")
99105

100-
def get_port() -> int:
101-
"""gets an integer from user input to use as port"""
106+
def get_port(CLIENT_LOG_FILE) -> int:
107+
"""gets an integer from arguemnts or user input to use as port
108+
tries arguments first, then user input"""
109+
110+
if len(sys.argv) >= 3:
111+
try:
112+
port = int(sys.argv[2])
113+
if port < 0 or port > 65535:
114+
raise ValueError
115+
return port
116+
except ValueError:
117+
#invalid port argument
118+
log_message(CLIENT_LOG_FILE, 'invalid ip from arguments, getting from user input')
119+
pass
102120
while True:
103121
try:
104122
port = input("enter a port number:\n")

0 commit comments

Comments
 (0)