Documentation Index Fetch the complete documentation index at: https://benzinga-2-locadex-parallel-main.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
GitHub Repository View source code and contribute
Features
Compatible with Python 2.6+ and Python 3
No external dependencies
Supports large messages
Configurable retry logic with exponential backoff
Installation
Install the library using setup.py:
git clone https://github.com/Benzinga/python-bztcp.git
cd python-bztcp
python setup.py install
Quick Start
Test the client using the built-in demo:
Python 3 / 2.7+
With Retry Config
Python 2.6
python -m bztcp USERNAME API_KEY
python -m bztcp USERNAME API_KEY RETRIES DELAY BACKOFF
python -m bztcp.__main__ USERNAME API_KEY
Basic Usage
The bztcp.client.Client class handles the connection and streaming:
from __future__ import print_function
from bztcp.client import Client
client = Client( username = 'USERNAME' , key = 'API_KEY' )
for content in client.content_items():
title = content.get( 'title' , None )
print (title)
Configuration Options
Retry Configuration
Configure retry behavior with exponential backoff:
from bztcp.client import Client
client = Client(
username = 'USERNAME' ,
key = 'API_KEY' ,
retries = 5 , # Maximum retry attempts
delay = 90 , # Initial delay in seconds
backoff = 2 # Backoff multiplier
)
for content in client.content_items():
title = content.get( 'title' , None )
print (title)
Parameter Description Default usernameYour Benzinga TCP username Required keyYour API access key Required retriesMaximum number of retry attempts - delayInitial delay between retries (seconds) - backoffMultiplier for exponential backoff -
Advanced Usage
Low-Level Message Handling
For granular control over connection status and individual messages:
from bztcp.client import Client, STATUS_STREAM
from bztcp.exceptions import BzException
client = Client( username = 'USERNAME' , key = 'API_KEY' )
while True :
try :
msg = client.next_msg()
if msg.status == STATUS_STREAM :
print ( f "Content item: { msg.data } " )
else :
print ( f "Status: { msg.status } " )
except KeyboardInterrupt :
print ( "Cancelled, disconnecting." )
client.disconnect()
break
except BzException as bze:
print ( f "BZ Error: { bze } " )
break
Message Status Constants
Status Description STATUS_STREAMNormal streaming content message
Key Methods
Method Description content_items()Generator that yields content dictionaries next_msg()Returns the next raw message object disconnect()Gracefully disconnects from the server
Error Handling
The library raises BzException for Benzinga-specific errors:
from bztcp.exceptions import BzException
try :
for content in client.content_items():
process(content)
except BzException as e:
print ( f "Benzinga error: { e } " )
except Exception as e:
print ( f "Unexpected error: { e } " )
Complete Example
#!/usr/bin/env python
from __future__ import print_function
import json
from bztcp.client import Client
def main ():
client = Client(
username = 'YOUR_USERNAME' ,
key = 'YOUR_API_KEY' ,
retries = 5 ,
delay = 30 ,
backoff = 2
)
print ( "Starting Benzinga TCP stream..." )
for content in client.content_items():
# Extract key fields
content_id = content.get( 'id' )
title = content.get( 'title' , 'No title' )
channels = content.get( 'channels' , [])
tickers = [t[ 'name' ] for t in content.get( 'tickers' , [])]
# Print summary
print ( f "[ { content_id } ] { title } " )
if channels:
print ( f " Channels: { ', ' .join(channels) } " )
if tickers:
print ( f " Tickers: { ', ' .join(tickers) } " )
print ()
if __name__ == '__main__' :
main()
See Also