我一直无法让ssl与Python Cassandra驱动程序和eventlet一起使用。
我们正在使用Python 3.4,eventlet 18.3和Cassandra驱动程序3.0.0。不带SSL的Eventlet和不带Eventlet的ssl都可以工作。
有没有人可以将ssl,cassandra和eventlet结合在一起使用Python?如果是这样,什么版本?
一个代码示例可能要求太多,但将非常有帮助。
跟进2016年2月18日:很抱歉。这是一些代码,全部使用Python3:
首先,最简单的Cassandra客户端。没有事件,没有SSL。有用:
from cassandra.cluster import Cluster
cluster = Cluster(contact_points=['<ip>'],
connection_class=None)
session = cluster.connect('<keyspace>')
print("OK, session:", session)
接下来,eventlet。没有线程,因此eventlet在这里毫无意义。但它有效:
from cassandra.cluster import Cluster
from cassandra.io.eventletreactor import EventletConnection
cluster = Cluster(contact_points=['<ip>'],
connection_class=EventletConnection)
session = cluster.connect('<keyspace>')
print("OK, session:", session)
接下来,ssl,没有eventlet。这也适用:
import ssl
from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider
cluster = Cluster(contact_points=['<ip>'],
connection_class=None,
ssl_options=dict(ca_certs='<certfile>',
cert_reqs=ssl.CERT_REQUIRED,
ssl_version=ssl.PROTOCOL_TLSv1),
auth_provider=PlainTextAuthProvider(username='<user>',
password='<pass>'))
session = cluster.connect('<keyspace>')
print("OK, session:", session)
最后是ssl和eventlet。这将失败:
import ssl
from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider
from cassandra.io.eventletreactor import EventletConnection
cluster = Cluster(contact_points=['<ip>'],
connection_class=EventletConnection,
ssl_options=dict(ca_certs='<certfile>',
cert_reqs=ssl.CERT_REQUIRED,
ssl_version=ssl.PROTOCOL_TLSv1),
auth_provider=PlainTextAuthProvider(username='<user>',
password='<pass>'))
session = cluster.connect('<keyspace>')
print("OK, session:", session)
回溯显示我们正在使用eventlet.green.ssl:
Traceback (most recent call last):
File "/home/jk/eventlet/venv3/lib/python3.5/site-packages/eventlet/hubs/poll.py", line 115, in wait
listener.cb(fileno)
File "/home/jk/eventlet/venv3/lib/python3.5/site-packages/eventlet/green/select.py", line 55, in on_read
current.switch(([original], [], []))
File "/home/jk/eventlet/venv3/lib/python3.5/site-packages/eventlet/greenthread.py", line 214, in main
result = function(*args, **kwargs)
File "/home/jk/eventlet/venv3/lib/python3.5/site-packages/cassandra/io/eventletreactor.py", line 98, in <lambda>
self._read_watcher = eventlet.spawn(lambda: self.handle_read())
File "/home/jk/eventlet/venv3/lib/python3.5/site-packages/cassandra/io/eventletreactor.py", line 153, in handle_read
buf = self._socket.recv(self.in_buffer_size)
File "/home/jk/eventlet/venv3/lib/python3.5/site-packages/eventlet/green/ssl.py", line 198, in recv
read = self.read(buflen)
File "/home/jk/eventlet/venv3/lib/python3.5/site-packages/eventlet/green/ssl.py", line 138, in read
super(GreenSSLSocket, self).read, *args, **kwargs)
File "/home/jk/eventlet/venv3/lib/python3.5/site-packages/eventlet/green/ssl.py", line 112, in _call_trampolining
return func(*a, **kw)
File "/home/jk/python-org/dst/python-3.5.1/lib/python3.5/ssl.py", line 786, in read
return self._sslobj.read(len, buffer)
TypeError: must be read-write bytes-like object, not None
Removing descriptor: 5
Traceback (most recent call last):
File "so4.py", line 13, in <module>
session = cluster.connect('<keyspace>')
File "cassandra/cluster.py", line 824, in cassandra.cluster.Cluster.connect (cassandra/cluster.c:11354)
File "cassandra/cluster.py", line 850, in cassandra.cluster.Cluster.connect (cassandra/cluster.c:11176)
File "cassandra/cluster.py", line 844, in cassandra.cluster.Cluster.connect (cassandra/cluster.c:11056)
File "cassandra/cluster.py", line 2041, in cassandra.cluster.ControlConnection.connect (cassandra/cluster.c:36224)
File "cassandra/cluster.py", line 2076, in cassandra.cluster.ControlConnection._reconnect_internal (cassandra/cluster.c:37080)
cassandra.cluster.NoHostAvailable: ('Unable to connect to any servers', {'<ip>': OperationTimedOut('errors=Timed out creating connection (5 seconds), last_host=None',)})
同样,没有实际线程。但是我们的真实系统确实使用它们。
如果在安装程序中包含eventlet.monkey_patch(),则没有任何区别。
真实的系统可以。
最佳答案
这似乎是由ssl套接字上使用select.select()的cassandra-drivers eventlet代码引起的。这将导致问题,如下所述:select and ssl in python
此问题已在https://github.com/datastax/python-driver/pull/485中修复
关于python - 有人让ssl使用Python Cassandra驱动程序和eventlet吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35410914/