-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
58 lines (43 loc) · 1.54 KB
/
client.py
File metadata and controls
58 lines (43 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import socket
def send_file_to_router(router_ip, router_port, filename):
# Try to resolve both IPv4 and IPv6 addresses
addr_info = socket.getaddrinfo(
router_ip, router_port, socket.AF_UNSPEC, socket.SOCK_STREAM
)
# Select the first valid address (IPv4 or IPv6)
for res in addr_info:
af, socktype, proto, canonname, sa = res
print(res)
try:
client_socket = socket.socket(af, socktype, proto)
client_socket.connect(sa)
print(f"Connected to router at {sa}")
break
except socket.error:
client_socket = None
continue
if client_socket is None:
print("Failed to connect to the server.")
return
try:
with open(filename, "rb") as f:
print(f"Sending '{filename}' to the router...")
while chunk := f.read(1024):
client_socket.sendall(chunk)
print("Sending...")
print(f"File '{filename}' sent to the router successfully.")
except FileNotFoundError:
print(f"File '{filename}' not found.")
except Exception as e:
print(f"An error occurred during file transfer: {e}")
finally:
client_socket.close()
print("Connection to router closed.")
print(f"File '{filename}' sent successfully.")
# Close the socket
client_socket.close()
router_ip = "127.0.0.1"
router_port = 12346
filename = "msg.txt"
send_file_to_router(router_ip, router_port, filename)
#test here and write the params to the test.txt