Documentation Index Fetch the complete documentation index at: https://benzinga-2-mrrancy-patch-1.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Repositorio de GitHub Ver el código fuente y contribuir
Compatible con Python 2.6+ y Python 3
Sin dependencias externas
Admite mensajes grandes
Lógica de reintentos configurable con backoff exponencial
Instala la librería con setup.py:
git clone https://github.com/Benzinga/python-bztcp.git
cd python-bztcp
python setup.py install
Prueba el cliente con la demo integrada:
python -m bztcp USERNAME API_KEY
python -m bztcp USERNAME API_KEY RETRIES DELAY BACKOFF
python -m bztcp.__main__ USERNAME API_KEY
La clase bztcp.client.Client gestiona la conexión y el 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)
Opciones de configuración
Configuración de reintentos
Configura el comportamiento de los reintentos con un backoff exponencial:
from bztcp.client import Client
client = Client(
username = 'USERNAME' ,
key = 'API_KEY' ,
retries = 5 , # Número máximo de intentos de reintento
delay = 90 , # Retraso inicial en segundos
backoff = 2 # Multiplicador de retroceso exponencial
)
for content in client.content_items():
title = content.get( 'title' , None )
print (title)
Parameter Descripción Valor predeterminado usernameTu nombre de usuario TCP de Benzinga Obligatorio keyTu clave de acceso a la API Obligatorio retriesNúmero máximo de reintentos - delayPausa inicial entre reintentos (segundos) - backoffMultiplicador para el backoff exponencial -
Manejo de mensajes de bajo nivel
Para un control detallado sobre el estado de la conexión y los mensajes individuales:
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
Constantes de estado de mensajes
Estado Descripción STATUS_STREAMMensaje de contenido de streaming normal
Método Descripción content_items()Generador que devuelve diccionarios de content next_msg()Devuelve el siguiente objeto de mensaje sin procesar disconnect()Desconecta limpiamente del servidor
La biblioteca lanza la excepción BzException para errores propios de Benzinga:
from bztcp.exceptions import BzException
try :
for content in client.content_items():
process(content)
except BzException as e:
print ( f "Error de Benzinga: { e } " )
except Exception as e:
print ( f "Error inesperado: { e } " )
#!/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():
# Extraer campos clave
content_id = content.get( 'id' )
title = content.get( 'title' , 'No title' )
channels = content.get( 'channels' , [])
tickers = [t[ 'name' ] for t in content.get( 'tickers' , [])]
# Imprimir resumen
print ( f "[ { content_id } ] { title } " )
if channels:
print ( f " Channels: { ', ' .join(channels) } " )
if tickers:
print ( f " Tickers: { ', ' .join(tickers) } " )
print ()
if __name__ == '__main__' :
main()