-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
185 lines (141 loc) · 5.08 KB
/
client.py
File metadata and controls
185 lines (141 loc) · 5.08 KB
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
import socket
import threading
import hashlib
import sqlite3
def signup():
connection = sqlite3.connect("database_mine.db", timeout=10)
cursor = connection.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS database_mine (
id INTEGER PRIMARY KEY,
username VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL
)
""")
username = input("Enter a username: ")
password = input("Enter a password: ")
username1, password1 = "Treveen", hashlib.sha256("123".encode()).hexdigest()
username2, password2 = "John", hashlib.sha256("lovehacking".encode()).hexdigest()
username3, password3 = "Harold", hashlib.sha256("ipython".encode()).hexdigest()
username4, password4 = username, hashlib.sha256(password.encode()).hexdigest()
cursor.execute("INSERT INTO database_mine (username, password) VALUES (?, ?)", (username1, password1))
cursor.execute("INSERT INTO database_mine (username, password) VALUES (?, ?)", (username2, password2))
cursor.execute("INSERT INTO database_mine (username, password) VALUES (?, ?)", (username3, password3))
cursor.execute("INSERT INTO database_mine (username, password) VALUES (?, ?)", (username4, password4))
connection.commit()
def login(username, password):
password_hash = hashlib.sha256(password).hexdigest()
con_db = sqlite3.connect("database_mine.db")
cursor = con_db.cursor()
cursor.execute("SELECT * FROM database_mine WHERE username = ? AND password = ?", (username, password_hash))
if cursor.fetchall():
print(f"Logged in as {username}")
dashboard()
else:
print(f"{username} or {password} was incorrect")
def join_group():
ip = str(input("Enter the ip of the group: "))
port = int(input("Enter it's port: "))
nickname = input("Enter a nickname: ")
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((ip, port))
def receive():
while True:
try:
message = client.recv(1024).decode('ascii')
if message == 'NICK':
client.send(nickname.encode('ascii'))
else:
print(message)
except:
print("There was an error in the server x010")
client.close()
break
def write():
while True:
message = f'{nickname}: {input("Type a message: ")}'
client.send(message.encode('ascii'))
receive_thread = threading.Thread(target=receive)
receive_thread.start()
write_thread = threading.Thread(target=write)
write_thread.start()
def new_group():
host = str(input("Enter your ip: "))
port = int(input("Enter a port: "))
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((host, port))
server.listen()
clients = []
nicknames = []
def show(message):
for client in clients:
client.send(message)
def handle(client):
while True:
try:
message = client.recv(1024)
show(message)
except:
index = clients.index(client)
clients.remove(client)
client.close()
nickname = nicknames[index]
show(f'{nickname} left the chat due to an error'.encode('ascii'))
nicknames.remove(nickname)
break
def receive():
while True:
client, address = server.accept()
print(f'Connected with {str(address)}')
print('===============================')
client.send('NICK'.encode('ascii'))
nickname = client.recv(1024).decode('ascii')
nicknames.append(nickname)
clients.append(client)
print(f'Nickname of the client is {nickname}')
show(f'{nickname} joined the chat!'.encode('ascii'))
client.send('Connected to the server!'.encode('ascii'))
thread = threading.Thread(target=handle, args=(client,))
thread.start()
print("Server is listening ...")
print("You are hosting this group")
print("Join it using your ip.")
print(f"Invite your friends with this ip: {host}")
receive()
def dashboard():
dashboard = """
[ * - * - * - * - * - * - ]
[ ~ THB NET ~ ]
[ ]
[ 1 - New group ]
[ 2 - Join group ]
[ ]
[ * - * - * - * - * - * - ]
"""
print(dashboard)
option = int(input("Enter an option: "))
if option == 1:
new_group()
elif option == 2:
join_group()
def main():
main = """
[ * - * - * - * - * - * - ]
[ ~ THB NET ~ ]
[ ]
[ 1 - Signup ]
[ 2 - Login ]
[ ]
[ * - * - * - * - * - * - ]
"""
print(main)
option = int(input("Enter an option: "))
if option == 1:
signup()
elif option == 2:
username = input("Enter an username: ")
password = input("Enter a password: ").encode()
login(username=username, password=password)
else:
print("Option undefined")
main()