Create function to connect to every server socket.
This commit is contained in:
1 parent
527ff24d64
commit
b57d50e7b0
3 files changed
+35
-29
No files matched your search
+26
-9
@@ -1,16 +1,33 @@
|
||||
# client.py
|
||||
import socket
|
||||
|
||||
HOST = "127.0.0.1"
|
||||
PORT = 5000
|
||||
ALL_IPS = [
|
||||
"172.22.156.3",
|
||||
"172.22.158.2",
|
||||
"172.22.94.2",
|
||||
"172.22.156.4",
|
||||
"172.22.158.3",
|
||||
"172.22.94.3",
|
||||
"172.22.156.5",
|
||||
"172.22.158.4",
|
||||
"172.22.94.4",
|
||||
"172.22.156.6"
|
||||
]
|
||||
|
||||
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
def grep_all_ips(ips):
|
||||
for ip in ips:
|
||||
try:
|
||||
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
client_socket.connect((ip, PORT))
|
||||
print(f"Connected to server at {ip}:{PORT}")
|
||||
|
||||
client_socket.connect((HOST, PORT))
|
||||
client_socket.sendall(b"Hello from client!")
|
||||
data = client_socket.recv(1024)
|
||||
print("Received:", data.decode())
|
||||
except Exception as e:
|
||||
print(f"Failed to connect to {ip}:{PORT} - {e}")
|
||||
finally:
|
||||
client_socket.close()
|
||||
|
||||
client_socket.sendall(b"Hello from client!")
|
||||
|
||||
data = client_socket.recv(1024)
|
||||
print("Received:", data.decode())
|
||||
|
||||
client_socket.close()
|
||||
grep_all_ips(ALL_IPS)
|
||||
-13
@@ -1,13 +0,0 @@
|
||||
PORT=5000
|
||||
|
||||
IP1=172.22.156.3
|
||||
IP2=172.22.158.2
|
||||
IP3=172.22.94.2
|
||||
IP4=172.22.156.4
|
||||
IP5=172.22.158.3
|
||||
|
||||
IP6=172.22.94.3
|
||||
IP7=172.22.156.5
|
||||
IP8=172.22.158.4
|
||||
IP9=172.22.94.4
|
||||
IP10=172.22.156.6
|
||||
+9
-7
@@ -1,7 +1,7 @@
|
||||
# server.py
|
||||
import socket
|
||||
|
||||
HOST = "127.0.0.1"
|
||||
HOST = "0.0.0.0"
|
||||
PORT = 5000
|
||||
|
||||
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
@@ -11,13 +11,15 @@ server_socket.listen()
|
||||
|
||||
print(f"Server listening on {HOST}:{PORT}")
|
||||
|
||||
conn, addr = server_socket.accept()
|
||||
print(f"Connected by {addr}")
|
||||
while True:
|
||||
conn, addr = server_socket.accept()
|
||||
print(f"Connected by {addr}")
|
||||
|
||||
data = conn.recv(1024)
|
||||
print("Received:", data.decode())
|
||||
data = conn.recv(1024)
|
||||
print("Received:", data.decode())
|
||||
|
||||
conn.sendall(b"Hello from server!")
|
||||
conn.sendall(b"Hello from server!")
|
||||
|
||||
conn.close()
|
||||
|
||||
conn.close()
|
||||
server_socket.close()
|
||||
Reference in new issue
Block a user