-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient_email.py
234 lines (209 loc) · 7.12 KB
/
client_email.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import socket
import sys
import hashlib
class socket_client():
def __init__(self, ip, port):
self.ip = ip
self.port = port
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server_address = (self.ip, int(self.port))
self.logged_on = False # Used for logging on and off (state variable)
self.ID = 0 # Keeps track of session ID
def connect(self, user):
"""
Handles the handshake to the server
Input: byte string of username:password
"""
self.sock.connect(self.server_address)
message = user
self.sock.send(message)
try:
recieved = self.sock.recv(1024)
except not recieved:
self.sock.send(message)
print("No data recieved. Resending Handshake")
finally:
pass
#Following section either receives the session id from the server or
#closes connection if an error occured
recieved = recieved.split(":")
if recieved[0] == "OK":
self.ID = recieved[1]
return(self.ID)
else:
print("Error: Exiting and Closing Socket")
self.sock.close()
exit()
def check_sum(self, message): #Help from Wade Rutherford who used this method with MD5
return hashlib.sha1(message).hexdigest() #returns a string of double length, containing only hexadecimal digits
def session(self, ID):
"""
The session class handles most of the session
"""
self.ID = ID
print("Session started")
self.logged_on = True #Switched on until user logs off
while self.logged_on == True:
cmd = self.get_cmd()
package = self.mng_cmd(cmd)
# New connection has to be created each time the client attempts a new
# communcation with the server
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect(self.server_address)
self.sock.send(package)
receaved = self.sock.recv(1024)
print receaved
#try and resend package 3 times
try_counter = 0
while receaved == "KO":
if try_counter < 4:
self.sock.send(package)
receaved = self.sock.recv(1024)
receaved.decode("utf-8")
try_counter += 1
break
self.sock.close()
return
def get_cmd(self):
"""
Takes command from the user to be sent to the server
Input: None
Output: user input <type: string>
"""
cmd = raw_input("Enter cmd: ")
cmd = str(cmd)
return cmd
def mng_cmd(self, cmd):
"""
Creates package to be sent to the server
Input: command (e.g. delmsg)
Output: package for server (e.g. "<ID>:'getmsg':<USERNAME>")
"""
print("CMD " + str(cmd))
if cmd == "email":
package = self.email_cmd()
elif cmd == "getmsg":
package = self.getmsg_cmd()
elif cmd == "count":
package = self.count_cmd()
elif cmd == "delmsg":
package = self.deleat_cmd()
elif cmd == "dump":
package = self.dump_cmd()
elif cmd == "logoff":
package = self.logoff_cmd()
elif cmd == "help":
package = self.help_cmd()
elif cmd == "shutdown":
print("Shutting down...")
self.shutdown(self.ID)
else:
print("Command does not exist.")
package = "ERROR"
if package == "ERROR":
new_cmd = self.get_cmd()
package = self.mng_cmd(new_cmd)
return package
else:
return package
def email_cmd(self):
"""
Composes the email command
Input: None
Output: package for server (in the form: "<ID>:'email':<USERNAME>:<SUBJECT>:<MSG>")
"""
recipient = raw_input("Enter email recipient: ")
subject = raw_input("Enter email subject: ")
msg = raw_input("Enter email message: ")
package = "{0}:{1}:{2}:{3}:{4}".format(self.ID, "email", recipient, subject, msg)
return self.encode(package)
def getmsg_cmd(self):
"""
Composes the get message command
Input: None
Output: package for server (in the form: "<ID>:'getmsg':<USERNAME>")
"""
sender_un = raw_input("Enter email sender: ")
package = "{0}:{1}:{2}".format(self.ID, "getmsg", sender_un)
return self.encode(package)
def count_cmd(self):
"""
Composes the command to count the number of emails in account
Input: None
Output: package for server (in the form: "<ID>:'count'")
"""
package = "{0}:{1}".format(self.ID, "count")
return self.encode(package)
def deleat_cmd(self):
"""
Composes the command to delete a message from an account
Input: None
Output: package for server (in the form: "<ID>:'delmsg':<USERNAME>:<SUBJECT>")
"""
sender_un = raw_input("Enter sender user name: ")
subject = raw_input("Enter email subject: ")
package = "{0}:{1}:{2}:{3}".format(self.ID, "delmsg", sender_un, subject)
return self.encode(package)
def dump_cmd(self):
"""
Composes the command to display all the messages from an account
Input: None
Output: package for server (in the form: "<ID>:'dump'")
"""
package = "{0}:{1}".format(self.ID, "dump")
return self.encode(package)
def logoff_cmd(self):
"""
Composes the command to tell the server that it is logging off
Input: None
Output: package for server (in the form: "<ID>:'logoff'")
"""
package = "{0}:{1}".format(self.ID, "logoff")
print("logoff function called")
self.logged_on = False # Switching off to break out of session
return self.encode(package)
def encode(self, package):
"""
Turns string commands into byte commands to be sent to server
Input: Package to be sent to be server <type: string>
Output: Package with checksum appended and then converted to byte string
"""
checksum = self.check_sum(package)
package = bytes("{0}#{1}".format(package, checksum))
return package
def help_cmd(self):
"""
Called by user if they want to know what commands are available to them
"""
package = "{0}:{1}".format(self.ID, "help")
return self.encode(package)
def shutdown(self):
"""
Gracefully shuts down the client
"""
package = "{0}:{1}".format(self.ID, "logoff")
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect(self.server_address)
print("Closing")
package = self.encode(package)
self.sock.send(package)
sys.exit()
def main():
sockip = "localhost"
sockport = 1503
#for handshake and creating session
try:
while True:
socketObj = socket_client(sockip, sockport)
un = raw_input("Enter user name: ")
pw = raw_input("Enter password: ")
print("Client Socket Opened. Handshake Sent.")
msg = bytes("{0}:{1}".format(un, pw))
ID = socketObj.connect(msg)
#for using session
sessionObj = socket_client(sockip, sockport)
sessionObj.session(ID)
except: # If anything goes wrong, the client catches the error and shuts down gracefully
print("\nSomething went wrong")
socketObj.shutdown()
main()