-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
48 lines (39 loc) · 1.09 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
import socket
import threading
HEADER = 64
PORT = 80
FORMAT = 'utf-8'
CONNECT_MESSAGE = "CONNECT"
DISCONNECT_MESSAGE = "!DISCONNECT"
SERVER = "put your public IP"
ADDR = (SERVER, PORT)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)
def send(msg):
message = msg.encode(FORMAT)
msg_length = len(message)
send_length = str(msg_length).encode(FORMAT)
send_length += b' ' * (HEADER - len(send_length))
client.send(send_length)
client.send(message)
def recive():
while(enSesion):
print("OTRO: " + client.recv(2048).decode(FORMAT))
def sesion():
global enSesion
enSesion = True
send(CONNECT_MESSAGE)
print("ESCRIBI UN MENSAJE Y ENTER")
print("0 - SALIR")
#hilo para responder mensajes
thread = threading.Thread(target=recive, args=())
thread.start()
while(enSesion):
message = input()
if message == "0":
send(DISCONNECT_MESSAGE)
client.close()
enSesion = False
else:
send(message)
sesion()