-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
150 lines (91 loc) · 4.82 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
139
140
141
142
143
144
145
146
147
148
149
150
# <ChaThon: A CLI Chat Application>
# Copyright (C) <2022> <Cromega>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from _thread import *
from select import select
import socket
import subprocess
import sys
import random
class stream:
def __init__(self, host = socket.gethostbyname(socket.gethostname()), port = None) -> None:
try:
self.stream = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.host = host
self.port = port if port != None else self.get_avaible()
except: print("An error ocurred with socket creation")
def create_server(self, num_clients: int) -> None:
try:
print(self.host, self.port, sep="\n", end="\n")
self.stream.bind((self.host, self.port))
self.stream.listen(int(num_clients) + 1)
self.i = False
self_created = False
self.clients = []
self.close = False
while True:
if self.close: break
connection, client = self.stream.accept()
self.clients.append(connection)
start_new_thread(self.each_client, (connection, client[0]))
self.send_message(bytes(f"[{client[0]}] connected to the server", "utf-8"), connection)
if not self_created: start_new_thread(self.self_client, ()); self_created = True
finally:
self.stream.close()
def each_client(self, connection:socket.socket, client:str) -> None:
connection.sendall(bytes(f"\nYour connection with: {socket.gethostbyname(socket.gethostname())} was successful", "utf-8"))
while True:
try:
message = connection.recv(2022)
if message != b"":
to_send = f"[{client}]: {message.decode('utf-8')}"
self.send_message(bytes(to_send, "utf-8"), connection)
else: self.clients.remove(connection); connection.close(); break
except: continue
if len(self.clients) == 0: self.kill()
def send_message(self, message:bytes, connection:socket.socket) -> None:
for client in self.clients:
if client != connection or self.i == True:
try: client.sendall(message)
except: client.close(); self.clients.remove(client)
def create_client(self) -> None:
try:
self.stream.connect((self.host, self.port))
while True:
sockets_list = [sys.stdin, self.stream]
read_sockets, write_sockets, error_sockets = select(sockets_list, [], [])
for sockets in read_sockets:
if sockets == self.stream:
message = sockets.recv(2022)
if message == b"": break
print(f"{message.decode('utf-8')}\n")
else:
message = input("-> ")
self.stream.sendall(bytes(message, "utf-8"))
if message == b"": break
finally: self.stream.close(); print(f"Connection with {self.host} was finished"); sys.exit()
def self_client(self) -> None:
self.i = True
subprocess.call(["xterm", "-e", f"chathon c -ip {self.host} -p {self.port}"])
def get_avaible(self) -> int:
port_n = random.randint(1, 65535)
try:
stream = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
stream.connect(("localhost", port_n))
connected = True
port_n += 1
except: connected = False
finally:
stream.close()
return port_n if not connected else self.get_avaible()
def kill(self) -> None:
self.close = True; self.self_client()