-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathserver.py
63 lines (57 loc) · 1.99 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
import socket
import os
import threading
import hashlib
# Create Socket (TCP) Connection
ServerSocket = socket.socket(family = socket.AF_INET, type = socket.SOCK_STREAM)
host = '127.0.0.1'
port = 1233
ThreadCount = 0
try:
ServerSocket.bind((host, port))
except socket.error as e:
print(str(e))
print('Waitiing for a Connection..')
ServerSocket.listen(5)
HashTable = {}
# Function : For each client
def threaded_client(connection):
connection.send(str.encode('ENTER USERNAME : ')) # Request Username
name = connection.recv(2048)
connection.send(str.encode('ENTER PASSWORD : ')) # Request Password
password = connection.recv(2048)
password = password.decode()
name = name.decode()
password=hashlib.sha256(str.encode(password)).hexdigest() # Password hash using SHA256
# REGISTERATION PHASE
# If new user, regiter in Hashtable Dictionary
if name not in HashTable:
HashTable[name]=password
connection.send(str.encode('Registeration Successful'))
print('Registered : ',name)
print("{:<8} {:<20}".format('USER','PASSWORD'))
for k, v in HashTable.items():
label, num = k,v
print("{:<8} {:<20}".format(label, num))
print("-------------------------------------------")
else:
# If already existing user, check if the entered password is correct
if(HashTable[name] == password):
connection.send(str.encode('Connection Successful')) # Response Code for Connected Client
print('Connected : ',name)
else:
connection.send(str.encode('Login Failed')) # Response code for login failed
print('Connection denied : ',name)
while True:
break
connection.close()
while True:
Client, address = ServerSocket.accept()
client_handler = threading.Thread(
target=threaded_client,
args=(Client,)
)
client_handler.start()
ThreadCount += 1
print('Connection Request: ' + str(ThreadCount))
ServerSocket.close()