-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.py
66 lines (54 loc) · 1.56 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
#Lucas Cunha Peres Rodrigues 83481
#Denis Luciano Lopes 85279
import socket
import threading
import sys
address1='localhost'
class Client:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
id = 0
def __init__(self,address):
try:
address1=address
self.sock.sendto(my_username.encode(),(address, 20000))
except:
print("IP inexistente ou indisponível")
return
cThread = threading.Thread(target=self.sendMsg)
cThread.daemon = True
cThread.start()
print("Conectado com sucesso, envie sua mensagem:\n")
while True:
try:
data = self.sock.recvfrom(1024)
except:
break
#print (data)
data_string = data[0].decode('utf-8')
if not data:
print('n entrou')
break
print(data_string)
def sendMsg(self):
while True:
msg = input('')
if msg[:5]=="/file":
self.sock.sendto(msg.encode(),(address1, 20000))
sock2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock2.connect((address1, 20000))
sock2.send(msg.encode())
sock2.close()
elif msg[:4]=="/get":
self.sock.sendto(msg.encode(),(address1, 20000))
sock2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock2.connect((address1, 20000))
sock2.send(msg.encode())
sock2.close()
elif msg[:4]=="/bye":
self.sock.sendto(msg.encode(),(address1, 20000))
self.sock.close()
else:
self.sock.sendto(msg.encode(),(address1, 20000))
my_username = "Username: "+ input("Username: ")
ip = input('Escreva o endereco IP do servidor no qual voce deseja se conectar (localhost): ')
client = Client(ip)