Skip to content

Commit 65d68db

Browse files
author
Alex Gaetano Padula
committed
Completion of client class and non tls implementation of CursusDB protocol
1 parent 3e4b8c7 commit 65d68db

File tree

7 files changed

+87
-19
lines changed

7 files changed

+87
-19
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dist
2+
build
3+
*.egg-info

cursusdb-py/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .main import Client

cursusdb-py/main.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#
2+
# CursusDB
3+
# Python Native Client Module
4+
# ******************************************************************
5+
# Copyright (C) 2023 CursusDB
6+
#
7+
# This program is free software: you can redistribute it and/or modify
8+
# it under the terms of the GNU General Public License as published by
9+
# the Free Software Foundation, either version 3 of the License, or
10+
# (at your option) any later version.
11+
#
12+
# This program is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU General Public License for more details.
16+
#
17+
# You should have received a copy of the GNU General Public License
18+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
20+
import base64
21+
import socket
22+
23+
# CursusDB Cluster Client Class
24+
class Client:
25+
def __init__(self, host, port, username, password, tls):
26+
self.sock = None
27+
self.host = host
28+
self.port = port
29+
self.username = username
30+
self.password = password
31+
self.tls = tls
32+
33+
# Instance method
34+
def connect(self):
35+
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
36+
self.sock.connect((self.host, self.port))
37+
auth_str = f"{self.username}\\0{self.password}".encode("utf8")
38+
39+
encoded_auth_str = base64.b64encode(auth_str)
40+
41+
self.sock.sendall(b"Authentication: " + encoded_auth_str + b"\r\n")
42+
43+
auth_response = self.sock.recv(2048)
44+
45+
if auth_response.decode("utf-8").startswith("0"):
46+
return "Connected to cluster successfully."
47+
else:
48+
return auth_response
49+
50+
def query(self, q):
51+
query_str = f"{q}\r\n".encode("utf8")
52+
self.sock.sendall(query_str)
53+
query_response = self.sock.recv(2097152)
54+
return query_response.decode("utf-8")
55+
56+
def close(self):
57+
self.sock.close()

main.py

Lines changed: 0 additions & 19 deletions
This file was deleted.

readme.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
11
## CursusDB Python Native Client Module
2+
3+
Importing package
4+
```
5+
from cursusdb-py import Client
6+
```
7+
8+
Using
9+
```
10+
# host/fqdn, port, db username, db password, TLS ?
11+
c = Client("0.0.0.0", 7681, "username", "password", False)
12+
print(c.connect())
13+
14+
print(c.query("select count from tweets;"))
15+
c.close()
16+
```

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
setuptools
2+
twine
3+
wheel

setup.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from setuptools import setup, find_packages
2+
3+
setup(
4+
name='cursusdb-py',
5+
version='1.0',
6+
packages=find_packages(),
7+
install_requires=[]
8+
)

0 commit comments

Comments
 (0)