-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.py
More file actions
76 lines (64 loc) · 2.15 KB
/
Copy pathClient.py
File metadata and controls
76 lines (64 loc) · 2.15 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""
-------------------------------------------------------
Client.
-------------------------------------------------------
Author: Daniyal Naqvi & Omar Hamza
ID: 169057430 & 169073034
Email: naqv7430@wlu.ca & hamz3034@wlu.ca
__updated__ = "2025-10-20"
-------------------------------------------------------
"""
#Imports
import socket
import os
#Set local host and port
HOST = '127.0.0.1'
PORT = 12345
def start_client():
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((HOST, PORT))
print(f"Connected to {HOST}:{PORT}")
#Check if the server is full
client.settimeout(1.0) # Wait to see if server sends anything
try:
initial_msg = client.recv(1024).decode()
#Print message if the server is full
if "Server full" in initial_msg:
print(initial_msg)
client.close()
return #Exit
except socket.timeout:
pass #Pass if the server is open
client.settimeout(None)
#Main loop
while True:
msg = input("Enter message: ")
client.send(msg.encode())
if msg.lower() == 'exit':
data = client.recv(1024).decode()
print("Server:", data)
break
# Receive data from the server
data = client.recv(4096)
#If user asked for a file (e.g., Daniyal.txt)
if '.' in msg:
filename = "downloaded_" + msg
with open(filename, 'wb') as f:
f.write(data)
print(f"File '{msg}' downloaded and saved as '{filename}'")
#Try to read and display the file contents (for text files)
try:
with open(filename, 'r', encoding='utf-8') as f:
contents = f.read()
print(contents)
except Exception as e:
#Print the error trapped message
print("(File saved but not printable — likely binary data)")
else:
try:
print("Server:", data.decode())
except UnicodeDecodeError:
print("(Received non-text data)")
client.close()
if __name__ == "__main__":
start_client()