- Server
- import ssl
- import socket
- # Server configuration
- HOST = 'localhost'
- PORT = 12345
- CERTIFICATE_FILE = 'server.crt'
- PRIVATE_KEY_FILE = 'server.key'
- # Create SSL context
- context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
- context.load_cert_chain(certfile=CERTIFICATE_FILE, keyfile=PRIVATE_KEY_FILE)
- # Create socket
- with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket:
- server_socket.bind((HOST, PORT))
- server_socket.listen(1)
- # Wrap socket with SSL
- with context.wrap_socket(server_socket, server_side=True) as ssl_server_socket:
- print("Server is listening...")
- # Accept incoming connection
- conn, addr = ssl_server_socket.accept()
- with conn:
- print('Connected by', addr)
- while True:
- data = conn.recv(1024)
- if not data:
- break
- print('Received:', data.decode())
- conn.sendall(data)
- Client
- import ssl
- import socket
- # Client configuration
- HOST = 'localhost'
- PORT = 12345
- CERTIFICATE_FILE = 'server.crt'
- # Create SSL context
- context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
- context.load_verify_locations(CERTIFICATE_FILE)
- # Create socket
- with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client_socket:
- # Wrap socket with SSL
- with context.wrap_socket(client_socket, server_hostname=HOST) as ssl_client_socket:
- ssl_client_socket.connect((HOST, PORT))
- print("Connected to server.")
- # Send data to server
- message = "Hello, server!"
- ssl_client_socket.sendall(message.encode())
- # Receive response from server
- data = ssl_client_socket.recv(1024)
- print('Received:', data.decode())
[text] ssl
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Editor
You can edit this paste and save as new: