This repository has been archived by the owner on Oct 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
138 lines (127 loc) · 5.09 KB
/
server.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
#!/usr/bin/env python
import select, socket, sys
import threading
import urllib.request
user_name = []
socket_list = []
conn_list = []
user_connection = {}
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setblocking(0)
server.bind(("", 5001))
server.listen(5)
inputs = [server, sys.stdin]
newData = 0
conns = 0
isLogin = False
isPrinted = False
## Import the db with updates
def importDB():
db = dict()
f = open("db.txt","r+")
for line in f:
if line != "\n":
userInfo, password = str(line).split(",")
password = password.split("\n")[0]
db[userInfo] = password
return db
## Print out the active users
def printActiveUsers():
for i,user in enumerate(user_connection):
for connection in conn_list:
connection.send((user + " is online. ").encode())
## Print out the disconnected users
def printDisconnections(user):
for connection in conn_list:
connection.send((user + " is offline. ").encode())
## Main Function
while inputs:
db = importDB()
readable, writable, exceptional = select.select(inputs, inputs, inputs)
for s in readable:
if s is server:
print ("Here receives a connect request from a client. ")
print
conn, addr = s.accept()
if conns == 0:
conns = 1
conn1 = conn
else:
conn2 = conn
print ("Connection is {}".format (conn))
conn.setblocking(0)
inputs.append(conn)
conn_list.append(conn)
localConn = conn
elif s is sys.stdin:
newData = 1;
command_string = input()
print ("Received:::: " + command_string)
else:
data = s.recv(1024).decode()
if data:
print ("Reading the data sent from users: " + data)
words = data.split()
print ("Received the data: " + data + "")
for i,word in enumerate(words):
if word == "login":
userID = words[i+1]
password = words[i+2]
if userID in db:
if (password == db[userID]):
isLogin = True
user_connection[userID] = s
greetingMessage = "Let's start to chat.\n"
localConn.send (greetingMessage.encode())
printActiveUsers()
else:
errorMessage = "The password is not correct."
localConn.send (errorMessage.encode())
else:
errorMessage = "The userID does not exist."
localConn.send (errorMessage.encode())
elif word == "msg":
if (isLogin):
receivedUserID = words[i+1]
msg = " ".join(str(x) for x in words[i+2:])
if receivedUserID in user_connection:
send_sock = user_connection[receivedUserID]
send_sock.send (msg.encode())
else:
errorMessage = "The user is not online."
localConn.send (errorMessage.encode())
else:
errorMessage = "Please log in first."
localConn.send (errorMessage.encode())
elif word == "register":
userID = words[i+1]
password = words[i+2]
if not userID in db:
with open("db.txt", "a") as f:
f.write(userID +","+ password +"\n")
greetingMessage = "Ok. "+ userID +" is registered now."
localConn.send (greetingMessage.encode())
else:
errorMessage = "The userID already exists."
localConn.send (errorMessage.encode())
elif word == "logout":
if isLogin:
userID = words[i+1]
conn_list.remove(user_connection[userID])
del user_connection[userID]
isLogin = False
printDisconnections(userID)
else:
errorMessage = "The user is not login."
localConn.send (errorMessage.encode())
if newData == 1:
data = command_string
newData = 0
print ("User-connection: \n", user_connection)
for s in exceptional:
inputs.remove(s)
print(inputs)
if s in outputs:
outputs.remove(s)
s.close()
f.close()