-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbctl.py
executable file
·57 lines (42 loc) · 1.26 KB
/
dbctl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python3
import http.client
import json
import requests
import requests.adapters
import socket
import sys
import urllib3
type = sys.argv[1]
query = sys.argv[2::]
if type == "script":
body = "\n".join(query).encode("UTF-8")
path = "script"
else:
body = json.dumps(query).encode("UTF-8")
path = ""
class DbConnection(http.client.HTTPConnection):
def __init__(self):
super().__init__("localhost")
def connect(self):
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.sock.connect("db.socket")
class DbConnectionPool(urllib3.connectionpool.HTTPConnectionPool):
def __init__(self):
super().__init__("localhost")
def _new_conn(self):
return DbConnection()
class DbAdapter(requests.adapters.HTTPAdapter):
def get_connection(self, url, proxies=None):
return DbConnectionPool()
session = requests.Session()
session.mount("http://db/", DbAdapter())
response = session.post(f"http://db/{path}", body)
if type == "script" or response.status_code != 200:
print(response.text, end="")
else:
print(
*["\t".join([str(j).replace("\t", " ") for j in i]) for i in response.json()],
sep="\n",
end="",
)
exit(int(response.status_code != 200))