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.SSLContext();

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, 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\nAccept-Ranges: bytes\r\nAge: 322835\r\nCache-Control: max-age=604800\r\nContent-Type: text/html; charset=UTF-8\r\nDate: Wed, 08 Apr 2020 10:38:23 GMT\r\nEtag: "3147526947"\r\nExpires: Wed, 15 Apr 2020 10:38:23 GMT\r\nLast-Modified: Thu, 17 Oct 2019 07:18:26 GMT\r\nServer: ECS (nyb/1D2C)\r\nVary: Accept-Encoding\r\nX-Cache: HIT\r\nContent-Length: 1256\r\nConnection: close\r\n\r\n'

b'<!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:visited {\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 illustra'

b'tive 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 2024 © pythontic.com