1
0
Fork 0

Change some options to global variables

This commit is contained in:
Ethanell 2019-07-15 22:38:25 +02:00
parent 33ed7d5575
commit 5fa65134a6
2 changed files with 13 additions and 12 deletions

View file

@ -14,6 +14,7 @@ class Socket:
"""Self """Self
Socket service with security system""" Socket service with security system"""
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Set the socket object self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Set the socket object
self.service_id = 0
self.key = None # Set the key encryption self.key = None # Set the key encryption
self.fernet = None # Set the encryption object self.fernet = None # Set the encryption object
@ -113,31 +114,31 @@ class Socket:
raise ConnectionError("Invalid key !") raise ConnectionError("Invalid key !")
return True return True
def connect_server(self, sock: socket.socket, host: str, port: int, service_id: int, encryption=True): def connect_server(self, host: str, port: int, encryption=True):
"""self, sock (socket), host (str), port (int), service_id (int), encryption (bool) """self, sock (socket), host (str), port (int), service_id (int), encryption (bool)
Connect a socket to a socket server""" Connect to a socket server"""
try: # Try to connect, else raise a custom error try: # Try to connect, else raise a custom error
sock.connect((host, port)) self.socket.connect((host, port))
except socket.error: except socket.error:
raise ConnectionError("Can't connect to server !") raise ConnectionError("Can't connect to server !")
else: else:
if encryption: # Set secure connexion is asked if encryption: # Set secure connexion is asked
self.get_secure_connexion(self.socket) self.get_secure_connexion(self.socket)
self.send(sock, f"Client service ID: {service_id}") # Check if service id match self.send(self.socket, f"Client service ID: {self.service_id}") # Check if service id match
if self.receive(sock) != f"Server service ID: {service_id}": if self.receive(self.socket) != f"Server service ID: {self.service_id}":
raise ConnectionError("Server service ID") raise ConnectionError("Server service ID")
return True return True
def connect_client(self, sock: socket, service_id: int, encryption=True): def connect_client(self, encryption=True):
"""self, sock (socket), service_id (int), encryption (bool) """self, sock (socket), service_id (int), encryption (bool)
Connect a socket server to a socket""" Connect a socket client to the server"""
connexion, address = sock.accept() # Await for connexion connexion, address = self.socket.accept() # Await for connexion
if encryption: if encryption:
self.set_secure_connexion(connexion) self.set_secure_connexion(connexion)
if self.receive(connexion) == f"Client service ID: {service_id}": # Check if service id match if self.receive(connexion) == f"Client service ID: {self.service_id}": # Check if service id match
self.send(connexion, f"Server service ID: {service_id}") self.send(connexion, f"Server service ID: {self.service_id}")
return connexion return connexion, address
else: else:
raise ConnectionError("Invalid client service ID !") raise ConnectionError("Invalid client service ID !")

View file

@ -7,7 +7,7 @@ def readme():
setup(name="SecureSocketService", setup(name="SecureSocketService",
version="1.0.0", version="1.0.1",
description="A socket service with secure connexion", description="A socket service with secure connexion",
long_description=readme(), long_description=readme(),
classifiers=[ classifiers=[