Free cookie consent management tool by TermsFeed The recv() method of SSLsocket class in Python | Pythontic.com

The recv() method of SSLsocket class in Python

Method Name:

recv

Method Signature:

recv(bufsize[, flags])

Parameters:

bufsize  Number of bytes to read

flags    The flag parameter of recv() function as defined in the Unix manual. Default value is zero.

Return Value:

The data received over an SSLSocket instance as bytes.

Overview:

  • The receive method returns the decrypted data from the socket as a bytes object.
  • In one go, it reads up to bufsize bytes.

Example:

# Example Python program that reads data from
# an HTTPS server using SSLSocket.recv() method

import socket
import ssl
import platform

# SSL Context creation
contextInstance                 = ssl.create_default_context()
contextInstance.verify_mode     = ssl.CERT_REQUIRED

 

# Check for OS X platform
if platform.system().lower() == 'darwin':
    import certifi
    import os

    # Load the CA certificates used for validating the certificate of the server
    contextInstance.load_verify_locations(cafile=os.path.relpath(certifi.where()),
                                     capath=None,
                                     cadata=None)

# Create a socket
socketInstance = socket.socket()

# Making the socket TLS compliant
secureSocketInstance  = contextInstance.wrap_socket(socketInstance, 
                                                    server_hostname="example.com",
                                                    do_handshake_on_connect=False)

# Connect to the HTTPS server
secureSocketInstance.connect(("example.com", 443))

# Complete the TLS handshake
secureSocketInstance.do_handshake()

# Form a HTTP request and send it to the server
HTTPMessage = 'GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n'
bytes       = str.encode(HTTPMessage)

secureSocketInstance.sendall(bytes)

# Receive data from server
while(True):
    data = ''
    data = secureSocketInstance.recv(1024)
    if(data==b''):
        print("Connection closed")
        break;

    print(data)

# TLS shutdown handshake
activeSocket = secureSocketInstance.unwrap()

# Close the sockets
secureSocketInstance.close()
socketInstance.close()
activeSocket.close()

 

Output:

b'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nETag: "84238dfc8092e5d9c0dac8ef93371a07:1736799080.121134"\r\nLast-Modified: Mon, 13 Jan 2025 20:11:20 GMT\r\nCache-Control: max-age=86000\r\nDate: Sat, 27 Sep 2025 09:45:41 GMT\r\nContent-Length: 1256\r\nConnection: close\r\nAlt-Svc: h3=":443"; ma=93600\r\n\r\n<!doctype html>\n<html>\n<head>\n    <title>Example Domain</title>\n\n    <meta charset="utf-8" />\n    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />\n    <meta name="viewport" content="width=device-width, initial-scale=1" />\n    <style type="text/css">\n    body {\n        background-color: #f0f0f2;\n        margin: 0;\n        padding: 0;\n        font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;\n        \n    }\n    div {\n        width: 600px;\n        margin: 5em auto;\n        padding: 2em;\n        background-color: #fdfdff;\n        border-radius: 0.5em;\n        box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);\n    }\n    a:link, a:visite'
b'd {\n        color: #38488f;\n        text-decoration: none;\n    }\n    @media (max-width: 700px) {\n        div {\n            margin: 0 auto;\n            width: auto;\n        }\n    }\n    </style>    \n</head>\n\n<body>\n<div>\n    <h1>Example Domain</h1>\n    <p>This domain is for use in illustrative examples in documents. You may use this\n    domain in literature without prior coordination or asking for permission.</p>\n    <p><a href="https://www.iana.org/domains/example">More information...</a></p>\n</div>\n</body>\n</html>\n'
Connection closed


Copyright 2025 © pythontic.com