-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient-ssl.py
More file actions
53 lines (40 loc) · 1.51 KB
/
client-ssl.py
File metadata and controls
53 lines (40 loc) · 1.51 KB
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
#!/usr/bin/env python3
""" client """
import socket
import ssl
import sys
from typing import Dict, Union, Optional
from utils import parse_config_file
config_type = Dict[str, Union[str, int]] | None
def start_client() -> None:
"""
Starts the client to connect to the server and send a message.
"""
# Parse configuration file
configs: config_type = parse_config_file("./config/config.txt")
if not configs:
print("Start client: server configurations missing")
return
# Extract server address and payload size from configurations
server_address = (configs.get("HOST"), configs.get("PORT"))
payload_size: Optional[Union[int, str, None]] = configs.get("PAYLOAD_SIZE")
if payload_size is None:
print("Error: Payload size is missing.")
return
try:
payload_size = int(payload_size)
except (ValueError, TypeError):
print("Error: Payload size must be an integer.")
return
try:
context = ssl.create_default_context()
with socket.create_connection(server_address) as sock:
with context.wrap_socket(socket, server_hostname=configs.get("HOST")) as ssock:
# Connect to the server
ssock.sendall(bytes(sys.argv[1], 'utf-8'))
response = ssock.recv(payload_size)
print(f"Server response: {response.decode('utf-8')}")
except Exception as e:
print(f"Error starting the client: {e}")
if __name__ == "__main__":
start_client()