You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.3 KiB
Python

12 months ago
#!/usr/bin/env python
from socket import socket, AF_INET, SOCK_STREAM
from threading import Thread
from time import sleep
from subprocess import check_call
from sys import argv, executable
try:
from cryptography.fernet import Fernet
except:
check_call((executable, '-m', 'pip', 'install', 'cryptography'))
from cryptography.fernet import Fernet
12 months ago
class Server:
s = socket(AF_INET, SOCK_STREAM)
12 months ago
def __init__(self):
if len(argv) == 4: k = argv[3].encode()
else:
k = Fernet.generate_key()
print(k)
self.f = Fernet(k)
self.s.bind((argv[1], int(argv[2])))
self.s.listen(3)
12 months ago
self.client, self.addr = self.s.accept()
def rMsg(self):
while True:
d = ''
d = self.f.decrypt(self.client.recv(1024)).decode()
12 months ago
print(end=d)
sleep(0.1)
12 months ago
def chat(self):
self.receiving = Thread(target=self.rMsg)
12 months ago
self.receiving.daemon = True
self.receiving.start()
while True:
msg = input()
msg = f"\nADMIN: {msg}\n"
print(msg)
self.client.send(self.f.encrypt(msg.encode()))
12 months ago
if __name__ == '__main__':
check_call('clear')
12 months ago
Server_m = Server()
Server_m.chat()
Server_m.receiving.join()
Server_m.s.close()