-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
102 lines (87 loc) · 3.61 KB
/
client.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
import socket
import ssl
import configparser
import logging
import time
from typing import Optional
# Load configuration settings
config = configparser.ConfigParser()
config.read("config.ini")
# Get server host, port, and SSL configuration
HOST = config.get("Server", "host", fallback="localhost")
PORT = config.getint("Server", "port", fallback=12345)
SSL_ENABLED = config.getboolean("Server", "ssl_enabled", fallback=True)
# Set up logging to track client activity
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger()
class FileSearchClient:
def __init__(
self, host: str = HOST, port: int = PORT, ssl_enabled: bool = SSL_ENABLED
):
"""
Initialize the FileSearchClient.
:param host: The server host.
:param port: The server port.
:param ssl_enabled: Whether to use SSL for the connection.
"""
self.host = host
self.port = port
self.ssl_enabled = ssl_enabled
self.ssl_context: Optional[ssl.SSLContext] = None
def setup_ssl(self) -> None:
"""Set up SSL context if SSL is enabled."""
if self.ssl_enabled:
self.ssl_context = ssl.create_default_context()
if self.ssl_context: # Ensure ssl_context is not None
self.ssl_context.check_hostname = False
self.ssl_context.verify_mode = ssl.CERT_NONE
logger.info("SSL enabled: Using SSL for secure connections")
else:
logger.info("SSL not enabled: Using plain TCP connections")
def send_query(self, query: str) -> str:
"""
Send a query to the server and return the response.
:param query: The query string to send.
:return: The server response.
"""
if not query.strip():
logger.error("Error: Empty query")
return "ERROR: Empty query"
try:
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
start_time = time.time() # Start timing
if self.ssl_enabled and self.ssl_context:
with self.ssl_context.wrap_socket(
client_socket, server_hostname=self.host
) as ssl_socket:
ssl_socket.connect((self.host, self.port))
logger.info("Connected to server at %s:%d", self.host, self.port)
ssl_socket.sendall(query.encode("utf-8"))
response = ssl_socket.recv(1024).decode("utf-8")
logger.info("Received response: %s", response)
else:
client_socket.connect((self.host, self.port))
logger.info("Connected to server at %s:%d", self.host, self.port)
client_socket.sendall(query.encode("utf-8"))
response = client_socket.recv(1024).decode("utf-8")
logger.info("Received response: %s", response)
end_time = time.time() # End timing
execution_time = end_time - start_time
logger.info("Query execution time: %.4f seconds", execution_time)
return response
except Exception as e:
logger.error("Error communicating with server: %s", e)
return "ERROR: Communication failed\n"
finally:
client_socket.close()
if __name__ == "__main__":
client = FileSearchClient()
client.setup_ssl()
while True:
query = input("Enter the string to search for: ")
if query.lower() == "exit":
break
response = client.send_query(query)
print(f"Server response: {response}")