Update 'client.py'
This commit is contained in:
parent
36efbea200
commit
a8511baef9
96
client.py
96
client.py
|
@ -1,48 +1,48 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
from socket import socket, AF_INET, SOCK_STREAM
|
from socket import socket, AF_INET, SOCK_STREAM
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
from time import sleep
|
from time import sleep
|
||||||
from subprocess import check_call
|
from subprocess import check_call
|
||||||
from sys import argv, executable
|
from sys import argv, executable
|
||||||
try:
|
try:
|
||||||
from cryptography.fernet import Fernet
|
from cryptography.fernet import Fernet
|
||||||
except:
|
except:
|
||||||
check_call((executable, '-m', 'pip', 'install', 'cryptography'))
|
check_call((executable, '-m', 'pip', 'install', 'cryptography'))
|
||||||
from cryptography.fernet import Fernet
|
from cryptography.fernet import Fernet
|
||||||
|
|
||||||
class client():
|
class client():
|
||||||
s = socket(AF_INET, SOCK_STREAM)
|
s = socket(AF_INET, SOCK_STREAM)
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
if len(argv) == 4: k = argv[3].encode()
|
if len(argv) == 4: k = argv[3].encode()
|
||||||
else:
|
else:
|
||||||
k = Fernet.generate_key()
|
k = Fernet.generate_key()
|
||||||
print(k)
|
print(k).decode()
|
||||||
self.f = Fernet(k)
|
self.f = Fernet(k)
|
||||||
self.s.connect((argv[1], int(argv[2])))
|
self.s.connect((argv[1], int(argv[2])))
|
||||||
|
|
||||||
def sMsg(self, msg):
|
def sMsg(self, msg):
|
||||||
self.s.send(self.f.encrypt(msg.encode()))
|
self.s.send(self.f.encrypt(msg.encode()))
|
||||||
|
|
||||||
def rMsg(self):
|
def rMsg(self):
|
||||||
while True:
|
while True:
|
||||||
d = ''
|
d = ''
|
||||||
d = self.f.decrypt(self.s.recv(1024)).decode()
|
d = self.f.decrypt(self.s.recv(1024)).decode()
|
||||||
print(end=d)
|
print(end=d)
|
||||||
sleep(0.1)
|
sleep(0.1)
|
||||||
|
|
||||||
def chat(self):
|
def chat(self):
|
||||||
receiving = Thread(target=self.rMsg)
|
receiving = Thread(target=self.rMsg)
|
||||||
receiving.daemon = True
|
receiving.daemon = True
|
||||||
receiving.start()
|
receiving.start()
|
||||||
while True:
|
while True:
|
||||||
msg = input()
|
msg = input()
|
||||||
msg = f"\nClient: {msg}\n"
|
msg = f"\nClient: {msg}\n"
|
||||||
print(msg)
|
print(msg)
|
||||||
self.sMsg(msg)
|
self.sMsg(msg)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
check_call('clear')
|
check_call('clear')
|
||||||
Client = client()
|
Client = client()
|
||||||
Client.chat()
|
Client.chat()
|
||||||
|
|
Loading…
Reference in New Issue