{"library":"thrift-sasl","code":"import os\nfrom thrift.transport import TSocket, TTransport\nfrom thrift.protocol import TBinaryProtocol\nfrom thrift_sasl import TSaslClientTransport\n\n# --- Configuration (replace with your actual values) ---\nSASL_MECHANISM = os.environ.get('SASL_MECHANISM', 'PLAIN') # e.g., 'GSSAPI', 'PLAIN'\nSASL_USERNAME = os.environ.get('SASL_USERNAME', 'user')\nSASL_PASSWORD = os.environ.get('SASL_PASSWORD', 'password') # Only for PLAIN mechanism\nTHRIFT_HOST = os.environ.get('THRIFT_HOST', 'localhost')\nTHRIFT_PORT = int(os.environ.get('THRIFT_PORT', '9090'))\n# For GSSAPI, you might need: SASL_SERVICE_PRINCIPAL = 'thrift/host.example.com@REALM'\n\n# 1. Create a base Thrift socket transport\nsocket = TSocket.TSocket(THRIFT_HOST, THRIFT_PORT)\n\n# 2. Wrap the socket in a buffered transport (often required)\nbuffered_transport = TTransport.TBufferedTransport(socket)\n\n# 3. Create the SASL client transport\n# For PLAIN mechanism:\nsasl_transport = TSaslClientTransport(\n    buffered_transport,\n    SASL_MECHANISM,\n    username=SASL_USERNAME,\n    password=SASL_PASSWORD\n)\n\n# For GSSAPI, you would typically use:\n# sasl_transport = TSaslClientTransport(\n#     buffered_transport,\n#     SASL_MECHANISM,\n#     service_principal=SASL_SERVICE_PRINCIPAL\n# )\n\n# 4. Open the SASL transport (initiates SASL handshake)\ntry:\n    sasl_transport.open()\n    print(f\"Successfully opened SASL transport to {THRIFT_HOST}:{THRIFT_PORT}\")\n\n    # 5. Create a protocol (e.g., TBinaryProtocol) using the SASL transport\n    protocol = TBinaryProtocol.TBinaryProtocol(sasl_transport)\n\n    # At this point, you would typically create a Thrift client\n    # and make remote procedure calls (RPCs).\n    # Example: client = MyThriftService.Client(protocol)\n    #          result = client.my_method()\n    #          print(f\"RPC result: {result}\")\n\nexcept TTransport.TTransportException as e:\n    print(f\"Thrift transport error: {e}\")\nexcept Exception as e:\n    print(f\"An unexpected error occurred: {e}\")\nfinally:\n    # 6. Close the transport\n    if sasl_transport.isOpen():\n        sasl_transport.close()\n        print(\"SASL transport closed.\")","lang":"python","description":"This quickstart demonstrates how to initialize `TSaslClientTransport` using `TSocket` and `TBufferedTransport`, configure it for a SASL mechanism (like PLAIN), and open the connection. This setup is the foundational step before creating a Thrift client and making RPC calls.","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":1},{"runtime":"python:3.10-slim","exit_code":1},{"runtime":"python:3.11-alpine","exit_code":1},{"runtime":"python:3.11-slim","exit_code":1},{"runtime":"python:3.12-alpine","exit_code":1},{"runtime":"python:3.12-slim","exit_code":1},{"runtime":"python:3.13-alpine","exit_code":1},{"runtime":"python:3.13-slim","exit_code":1},{"runtime":"python:3.9-alpine","exit_code":1},{"runtime":"python:3.9-slim","exit_code":1}]}