管理网络通信
在 Python 中管理网络通信
正如“网络通信”页面中所解释的,SmartEDB 7.1.1795 及更高版本允许使用 IPv6 地址协议来指定节点地址。以下部分描述了对于将使用了 SmartEDB 早期版本网络通信的应用程序移植到新版本的开发人员来说可能重要的问题。OpenSSL 部分介绍了如何为网络通信启用 SSL 安全性。
IPv4 与 IPv6 的问题
对于将应用程序从 SmartEDB 7.1.1795 版本之前的版本移植过来的开发人员,应注意以下 API 更改:
- MasterConnection 方法 getReplicaAddress() 会返回一个字典,其中包含元素 ip(字符串)和 port(整数)。
- SqlServer 构造函数允许使用新参数 net_interface
OpenSSL 支持
要启用 SSL 支持,应用程序必须首先通过以下调用初始化 SSL 层并设置验证位置:
>>>exdb.ssl_init()
>>>exdb. ssl_load_verify_locations(ca_file, ca_path)
SSL 层的参数在一个 Python 字典中定义。例如:
ssl_params=
{
'verify_mode' : self.exdb.SSLVerifyMode.VerifyPeer |
self.exdb.SSLVerifyMode.VerifyFailIfNoPeerCert,
'cert_file_pem': "../../thlib/certs/client.crt",
'pkey_file_pem': "../../thlib/certs/client.key"
}
有关 verify_mode、cert_file_pem 和 pkey_file_pem 的值的设置详情,请参阅 OpenSSL 参数页面。
SQL 中的 SSL 安全性
connect()
方法接受 sql_login
、sql_password
和 ssl_params
这些参数来创建一个 RemoteSQL SSL 连接。connect()
方法的定义如下:
def connect( dbname = None, port = 0, nodes = None, nReplicas = 1,
replType = ReplicationType.SQL_REPLICATION,
maxConnectAttempts = 10, local = False,
useConnectionPool=False, recover = False,
txBufSize = 64*1024,
connectTimeout=2000L, readTimeout=1200*1000L,
sql_login=None, sql_password=None, ssl_params=None):
对于 SSL 连接,ssl_params 是必需的,而 sql_login 和 sql_password 是可选的。例如,要创建与 xSQL 服务器的 SSL 连接,可以指定以下连接参数:
>>>conn = exdb.connect('localhost', 5001, sql_login='login',
sql_password='password', ssl_params=ssl_params)
与上述 RemoteSQL 客户端连接类似,DistributedSQL 客户端连接可以按如下方式创建:
>>>conn = exdb.connect( nodes=('localhost:5010', 'localhost:5011'),
sql_login='login', sql_password='password',
ssl_params=ssl_params)
通过指定 SqlServer 构造函数参数 ssl_params 和 auth_required,可以实例化一个启用 SSL 的 SQL 服务器。SqlServer 构造函数定义如下:
exdb.SqlServer(engine, port, bufsize, nThreads, listen_queue_size,
local_domain, ssl_params, auth_required, interrupt_timeout)
若要在 SqlServer 中使用身份验证,应用程序必须创建一个名为“Users”的表,并插入用户名(登录名)及其相应的密码(密码)。例如:
sql_statements =
[
"create table Users (login string primary key, password string)",
"insert into Users values ('user1', 'mypass')",
"insert into Users values ('user2', 'hYfjdKK2')"
]
server = exdb.SqlServer(engine, port,ssl_params=ssl_params, auth_required=True)
for stmt in sql_statements:
self.engine.execute(stmt)