-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
182 lines (159 loc) · 4.34 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
# Messaging Client
# @author: Matt Jadud
# @editors: Emmanuel & Javier
import socket, sys, time, atexit
from Exceptions import Loggedin, Registered, CRCException
from pycrc.crc_algorithms import Crc
# Globals | (MSGLEN) might be unnecessary
crc = Crc(width=16, poly=0x8005,
reflect_in = True, xor_in = 0x0000,
reflect_out = True, xor_out = 0x0000)
MSGLEN = 1
def printInstructions():
print("---------Welcome to Message Server--------")
print("These are your options:")
print("LOGOUT")
print("MESSAGE")
print("STORE")
print("COUNT")
print("DELMSG")
print("GETMSG")
print("DUMP")
print("-------------------------------------------")
print("Click Ctrl-C to stop sending messages.")
return
def exit_handler():
print("\nStopped Client")
def generateCRC(message):
code = crc.bit_by_bit(str(message))
return hex(code)
# CONTRACT
# get_message : socket -> string
# Takes a socket and loops until it receives a complete message
# from a client. Returns the string we were sent.
# No error handling whatsoever.
def receive_message (sock):
message = ''
try:
while True:
char = sock.recv(1)
if char == b'\0':
break
if char == b'':
break
else:
message += char.decode("utf-8")
finally:
return message
def send (msg):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
if msg == "REGISTER":
x_codemsg = " ".join([msg, username, password, email])
code = generateCRC(x_codemsg)
length = sock.send(bytes(" ".join([code, msg, username, password, email]) + "\0"))
print ("SENT MSG: '{0}'".format(msg))
print ("CHARACTERS SENT: [{0}]".format(length))
return sock
else:
x_codemsg = " ".join([msg, username, password])
code = generateCRC(x_codemsg)
length = sock.send(bytes(" ".join([code, msg, username, password]) + "\0"))
print ("SENT MSG: '{0}'".format(msg))
print ("CHARACTERS SENT: [{0}]".format(length))
return sock
def recv (sock):
response = receive_message(sock)
if response == "LOGIN SUCCESS":
print("RESPONSE: [{0}]".format(response))
raise Loggedin(1)
elif response == "LOGIN FAILED":
print("RESPONSE: [{0}]".format(response))
raise Loggedin(0)
elif response == "REGISTER SUCCESS":
print("RESPONSE: [{0}]".format(response))
raise Registered(1)
elif response == "REGISTER FAILED":
print("RESPONSE: [{0}]".format(response))
raise Registered(0)
elif response == "LOGGED OUT":
print("RESPONSE: [{0}]".format(response))
raise Loggedin(-1)
elif response == "RETRY":
print("RESPONSE: [{0}]".format(response))
raise CRCException(-1)
print("RESPONSE: [{0}]".format(response))
def send_recv (msg):
recv(send(msg))
if __name__ == "__main__":
# Check if the user provided all of the
# arguments. The script name counts
# as one of the elements, so we need at
# least three, not fewer.
if len(sys.argv) < 3:
print ("Usage:")
print (" python client.py <host> <port>")
print (" For example:")
print (" python client.py localhost 8888")
print
sys.exit()
global username
global password
global email
atexit.register(exit_handler)
host = sys.argv[1]
port = int(sys.argv[2])
# Registration or Login
regornah = int(raw_input("Enter 0 if you have an account. Enter 1 if you need to register: "))
if regornah == 0:
while True:
username = raw_input("Please enter your username: ")
password = raw_input("Please enter your password: ")
try:
send_recv("LOGIN")
except KeyboardInterrupt:
sys.exit()
except Loggedin as e:
if e.message == 0:
continue
else:
break
except CRCException:
continue
else:
username = raw_input("Create a new username: ")
email = raw_input("Enter your e-mail: ")
password = raw_input("Create a new password: ")
while True:
try:
send_recv("REGISTER")
except KeyboardInterrupt:
sys.exit()
except Registered:
break
except CRCException:
continue
while True:
try:
send_recv("LOGIN")
except KeyboardInterrupt:
sys.exit()
except Loggedin:
break
except CRCException:
continue
printInstructions()
while True:
try:
selection = raw_input(">>> ")
send_recv(selection)
except KeyboardInterrupt:
sys.exit()
except Loggedin as e:
if e.message == 0 or e.message == 1:
continue
elif e.message == -1:
print("LOGIN AGAIN")
except CRCException:
print("RESENDING PREVIOUS COMMAND")
send_recv(selection)