Why kafka-python fails to connect to Bluemix message hub service?
<p>I'm trying to connect to a Bluemix Message Hub instance on <a href="http://bluemix.net" rel="nofollow">http://bluemix.net</a>. This simple script</p>

<pre><code>#!/usr/bin/env python 

from kafka import KafkaProducer 
from kafka.errors import KafkaError 

kafka_brokers_sasl = [
  "kafka01-prod01.messagehub.services.us-south.bluemix.net:9093",
  "kafka02-prod01.messagehub.services.us-south.bluemix.net:9093",
  "kafka03-prod01.messagehub.services.us-south.bluemix.net:9093",
  "kafka04-prod01.messagehub.services.us-south.bluemix.net:9093",
  "kafka05-prod01.messagehub.services.us-south.bluemix.net:9093" ] 
sasl_plain_username = "xxxxxxxxxxxxxxx" 
sasl_plain_password = "xxxxxxxxxxxxxxxxxxxxxxxxx" 
sasl_mechanism = 'SASL_PLAINTEXT' 

producer = KafkaProducer(bootstrap_servers = kafka_brokers_sasl,
                         sasl_plain_username = sasl_plain_username,
                         sasl_plain_password = sasl_plain_password,
                         sasl_mechanism = sasl_mechanism ) 
</code></pre>

<p>ends with the exception below:</p>

<pre><code>Traceback (most recent call last): 
  File "./test-mh.py", line 12, in &lt;module&gt; 
    producer = KafkaProducer(bootstrap_servers = kafka_brokers_sasl, sasl_plain_username = sasl_plain_username, sasl_plain_password = sasl_plain_password, sasl_mechanism = sasl_mechanism ) 
  File "/usr/local/lib/python2.7/dist-packages/kafka/producer/kafka.py", line 328, in __init__ 
    **self.config) 
  File "/usr/local/lib/python2.7/dist-packages/kafka/client_async.py", line 202, in __init__ 
    self.config['api_version'] = self.check_version(timeout=check_timeout) 
  File "/usr/local/lib/python2.7/dist-packages/kafka/client_async.py", line 791, in check_version 
    raise Errors.NoBrokersAvailable() 
kafka.errors.NoBrokersAvailable: NoBrokersAvailable
</code></pre>

<p>I've got kafka_brokers_sasl, sasl_plain_username, and sasl_plain_password from messagehub service credentials object. 
I'm using kafka-python 1.3.1, which seems supporting SASL authentication mechanism.
Any idea of what am I doing wrong? Thanks.</p>

<p>Message Hub requires that clients connect using a TLS 1.2 connection.  This means specifying a <code>security_protocol</code> parameter to <code>KafkaProducer</code> and also a <code>ssl.SSLContext</code> via the <code>ssl_context</code> parameter - as it appears that the Python Kafka client creates a <code>SSLv23</code> context by default.</p>

<p>Here are the changes required to connect:</p>

<pre><code>import ssl
from kafka import KafkaProducer 
from kafka.errors import KafkaError 

kafka_brokers_sasl = [
    "kafka01-prod01.messagehub.services.us-south.bluemix.net:9093",
    "kafka02-prod01.messagehub.services.us-south.bluemix.net:9093",
    "kafka03-prod01.messagehub.services.us-south.bluemix.net:9093",
    "kafka04-prod01.messagehub.services.us-south.bluemix.net:9093",
    "kafka05-prod01.messagehub.services.us-south.bluemix.net:9093" ] 
sasl_plain_username = "xxxxxxxxxxxxxxx" 
sasl_plain_password = "xxxxxxxxxxxxxxxxxxxxxxxxx" 

sasl_mechanism = 'PLAIN'       # &lt;-- changed from 'SASL_PLAINTEXT'
security_protocol = 'SASL_SSL'

# Create a new context using system defaults, disable all but TLS1.2
context = ssl.create_default_context()
context.options &amp;= ssl.OP_NO_TLSv1
context.options &amp;= ssl.OP_NO_TLSv1_1

producer = KafkaProducer(bootstrap_servers = kafka_brokers_sasl,
                         sasl_plain_username = sasl_plain_username,
                         sasl_plain_password = sasl_plain_password,
                         security_protocol = security_protocol,
                         ssl_context = context,
                         sasl_mechanism = sasl_mechanism)
</code></pre>

python apache-kafka ibm-bluemix message-hub