-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
183 lines (157 loc) · 5.46 KB
/
client.py
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import errno
import random
import socket
import threading
import re
import time
import sys
import os
running = True
auth = False
prompt = '\nEnter one of the following commands (/msgto, /activeuser, /creategroup, /joingroup, /groupmsg, /logout, /p2pvideo):\n'
def UDP_send(receive_addr, filename, sender):
BUF_SIZE = 1024
# print(address, port)
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
count = 0
if not os.path.exists(filename):
print(f'No such file or directory: {filename}')
return
f = open(filename, 'rb')
while True:
if count == 0:
data = "UDP%%" + sender + "_" + filename
client.sendto(data.encode('utf-8'), receive_addr)
data = f.read(BUF_SIZE)
if str(data) != "b''":
client.sendto(data, receive_addr)
else:
client.sendto('end'.encode('utf-8'), receive_addr) # end for the file and send a speical "end" flag
print(filename + " has been uploaded.")
print(prompt)
break
f.close()
client.close()
time.sleep(0.1)
def UDP_recv(server_ip, udp_port):
server_addr = (server_ip, udp_port)
try:
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server.bind(server_addr)
server.setblocking(False)
count = 0
f = None
filename = ""
while running:
try:
data, addr = server.recvfrom(1024)
if count == 0:
count += 1
recv_data_array = data.decode('utf-8').split("%%")
if len(recv_data_array) > 1:
udp_type, filename = recv_data_array
if udp_type == "UDP":
f = open(filename, 'wb')
elif str(data) != "b'end'":
count += 1
f.write(data)
else:
f.close()
count = 0
print("\nReceived " + filename.split("_")[1] + " from " + filename.split("_")[0])
print(prompt)
except socket.error as e:
if e.errno == errno.EWOULDBLOCK:
pass
else:
print(":", e)
except:
print('udp port already in use!')
os._exit(0)
def read_server(s):
global running, auth
while running:
try:
content = s.recv(2048).decode('utf-8')
print(content)
if not content or 'Bye' in content or 'blocked' in content or 'error' in content:
disconnect(s)
running = False
break
if 'Invalid password' not in content:
print(prompt)
if 'Welcome' in content:
auth = True
elif 'p2pvideo' in content:
_, addr, receive_port, file_name, sender = re.split(r'\s', content)
UDP_send((addr, int(receive_port)), file_name, sender)
except OSError as e:
print(f"Error reading from server: {e}")
running = False
break
def disconnect(s):
if s:
s.shutdown(socket.SHUT_RD)
s.close()
def execute_command(s, udp_port):
while running:
if not auth:
username = input("Username: ")
password = input("Password: ")
if not username.strip() or not password.strip():
print('Error: pls input valid smth otherwise close ur connection!')
disconnect(s)
break
s.send(f'{username} {password} {udp_port}'.encode('utf-8'))
else:
line = input()
if not line:
continue
pattern = ""
command = re.split(r'\s', line)[0]
if 'msgto' == command:
pattern = r'msgto (\S+) (\S+)'
elif 'joingroup' == command:
pattern = r'joingroup (\S+)'
elif 'creategroup' == command:
pattern = r'creategroup (\S+) (\S+)'
elif 'activeuser' == command:
pattern = r'activeuser'
elif 'p2pvideo' == command:
pattern = r'p2pvideo (\S+) (\S+)'
elif 'groupmsg' == command:
pattern = r'groupmsg (\S+) (\S+)'
match = re.search(pattern, line)
if match:
send_msg(s, f'{line}')
else:
print('Error: Invalid command!')
print(prompt)
continue
if command == 'logout':
break
time.sleep(0.1)
def send_msg(s, msg):
s.send(msg.encode('utf-8'))
def main(server_ip, tcp_port, udp_port):
# global udp_port, server_ip
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# udp_port = udp_port_
# server_ip = server_ip_
try:
s.connect((server_ip, tcp_port))
print('Please login')
threading.Thread(target=UDP_recv, args=(server_ip, udp_port)).start()
threading.Thread(target=read_server, args=(s,)).start()
execute_command(s, udp_port)
except Exception as e:
print(f'Error: {e}')
print('Connection refused!')
if __name__ == '__main__':
if len(sys.argv) != 4:
print("Usage: python client.py <server_ip> <tcp_port> <udp_port>")
sys.exit(1)
server_ip = sys.argv[1]
tcp_port = int(sys.argv[2])
udp_port = int(sys.argv[3])
main(server_ip, tcp_port, udp_port)