Free cookie consent management tool by TermsFeed The accept() method of SSLSocket in Python | Pythontic.com

The accept() method of SSLSocket in Python

Method Name:

accept

Method Signature:

accept()

Parameters:

None

Return Value:

A tuple containing an SSLSocket instance and the IP address of the client. The SSLSocket instance can be used to communicate with the client using TLS.

Overview:

  • The method accept(), accepts a connection request from a client.
  • The method accept() returns a tuple of SSLSocket and the IP address of the client.
  • The first element of the tuple, which is an instance of SSLSocket that can be used for communicating with the client in a secure manner as per TLS protocol.
  • The client program used in the Introduction to SSLSocket, can be used for communicating with the server below.

Example:

# Example Python program that accepts new TLS connections
# from clients through accept() method of SSLSocket class
import socket
import ssl
import platform
import certifi
import os

# Create an SSL context
context                     = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)

# Load CA certificate with which the server will 
# validate the client certificate
context.load_verify_locations("./DemoCA.pem")

# Load server certificate
context.load_cert_chain(certfile="./DemoSvr.pem", 
                        keyfile="./DemoSvr.key")

# Work-around for self-signed certificates as Python3.13 is strict
# about them
context.verify_flags = context.verify_flags & ~ssl.VERIFY_X509_STRICT
context.verify_mode  = ssl.CERT_REQUIRED

# Create a connection oriented server socket
ipAddress       = "127.0.0.1"
portNumber      = 15001
serverSocket    = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)

# Wrap the socket to get an SSLSocket and bind it to an IP address and port
secureServerSocket  = context.wrap_socket(serverSocket,
                                          server_side = True)  
                           
secureServerSocket.bind((ipAddress, portNumber))
secureServerSocket.listen()

print("Listening and accepting connections")
try:
    while(True):
        (clientConnection, clientAddress) = secureServerSocket.accept()     
        print("Serving connection request from:")
        print(clientAddress)
        clientConnection.sendall("hi there".encode(errors='ignore'))
        clientConnection.close()
except KeyboardInterrupt:
    print("Server exiting")
    serverSocket.close()
    secureServerSocket.close()

Output-Server:

Listening and accepting connections
Serving connection request from:
('127.0.0.1', 62333)
^CServer exiting

 

Output-Client:

X.509 certificate of the server DemoServer:
{'subject': ((('countryName', 'IN'),), (('stateOrProvinceName', 'KA'),), (('localityName', 'BLR'),), (('organizationName', 'DemoServerOrg'),), (('organizationalUnitName', 'DemoServerUnit'),), (('commonName', 'DemoServer'),), (('emailAddress', 'demoserver@demoservertesting.com'),)), 'issuer': ((('countryName', 'IN'),), (('stateOrProvinceName', 'KA'),), (('localityName', 'BLR'),), (('organizationName', 'DemoCAOrg'),), (('organizationalUnitName', 'DemoCAUnit'),), (('commonName', 'localca'),), (('emailAddress', 'localca@localcatest.com'),)), 'version': 1, 'serialNumber': '01', 'notBefore': 'Sep 22 10:44:04 2025 GMT', 'notAfter': 'Sep 22 10:44:04 2026 GMT'}
Secure communication received from server:hi there


Copyright 2025 © pythontic.com