Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
121 lines (96 loc) · 3.7 KB

File metadata and controls

121 lines (96 loc) · 3.7 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import datetime
import socket
from random import randrange
serverAddressPort = ("127.0.0.1", 20001)
bufferSize = 1024
# Create a UDP socket at client side
UDPClientSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
def common_initial_prompt():
sequence_number = str(randrange(65536))
user_src = input("Introduce tu nombre: \n >")
user_password = input("Introduce tu contraseña: \n >")
return sequence_number, user_password, user_src
def send_data(msg_from_client):
bytes_to_send = str.encode(msg_from_client)
UDPClientSocket.sendto(bytes_to_send, serverAddressPort)
def capture_response(sequence):
msg_from_server = UDPClientSocket.recvfrom(bufferSize)
validate_response(msg_from_server, sequence)
def create_send_data_and_capture_response(msg_from_client, sequence: str):
send_data(msg_from_client)
capture_response(sequence)
def register():
sequence_str, user_password, user_name = common_initial_prompt()
msg_from_client = "REG$@%{}$@%{}$@%{}".format(
sequence_str,
user_name,
user_password
)
create_send_data_and_capture_response(msg_from_client, sequence_str)
def put_message():
sequence_str, user_password, user_src = common_initial_prompt()
user_dst = input("Introduce el usuario destinatario: \n >")
text = input("Introduce el mensaje: \n >")
msg_from_client = "PUT$@%{}$@%{}$@%{}$@%{}$@%{}".format(
sequence_str,
text,
user_src,
user_dst,
user_password
)
create_send_data_and_capture_response(msg_from_client, sequence_str)
def get_messages():
sequence_str, user_password, user_src = common_initial_prompt()
msg_from_client = "GET$@%{}$@%{}$@%{}".format(
sequence_str,
user_src,
user_password
)
create_send_data_and_capture_response(msg_from_client, sequence_str)
def show_generic_message(decoded_message):
print(datetime.datetime.now(), 'Mensaje recibido del servidor: {}'.format(decoded_message.split('$@%')[2]))
def validate_response(msg_from_server, sequence:str):
decoded_message = msg_from_server[0].decode()
sequence_received = decoded_message.split("$@%", 2)[1]
operation_received = decoded_message.split("$@%", 1)[0]
if 'ANS' == operation_received and sequence == sequence_received:
if len(decoded_message.split('$@%')) >= 6:
show_generic_message(decoded_message)
list_messages = decoded_message.split('$@%')
count = 0
while count < 3:
del list_messages[0]
count += 1
for sub_list_count in range(0, len(list_messages), 3):
print('- Mensaje: "{}"'
' - de: {}'
' - fecha: {}'.format(
list_messages[sub_list_count:sub_list_count + 3][2],
list_messages[sub_list_count:sub_list_count + 3][0],
list_messages[sub_list_count:sub_list_count + 3][1]
))
else:
show_generic_message(decoded_message)
def main():
initial_msg = "Selecciona: \n" \
"1 => Regístrate\n" \
"2 => Enviar mensaje\n" \
"3 => Leer mensajes\n" \
"4 => Salir\n" \
">"
exit = False
while not exit:
entrada = input(initial_msg)
if entrada == "4":
print("Adios")
UDPClientSocket.close()
exit = True
elif entrada == "1":
register()
elif entrada == "2":
put_message()
elif entrada == "3":
get_messages()
else:
print("Valor incorrecto, vuelve a intentarlo, gracias.")
main()
Morty Proxy This is a proxified and sanitized view of the page, visit original site.