将数据从 POSTGRESQL 触发器发送到 python TCP 服务器
Sending data from POSTGRESQL trigger to a python TCP server
我正在尝试监控 postgres 数据库中的 table。
我想将此 table 上的每个操作(插入或更新或删除)发送到 python tcp 服务器。
所以我尝试关注this tutorial,但没有成功。
这是我正在使用的 SQL 脚本:
CREATE LANGUAGE plpython3u;
CREATE OR REPLACE FUNCTION public.sendsocket(msg character varying, host character varying, port integer)
RETURNS integer AS
$BODY$
import _socket
try:
s = _socket.socket(_socket.AF_INET, _socket.SOCK_STREAM)
s.connect((host, port))
s.sendall(msg)
s.close()
return 1
except:
return 0
$BODY$
LANGUAGE plpython3u VOLATILE
COST 100;
ALTER FUNCTION public.sendsocket(character varying, character varying, integer)
OWNER TO keycloak;
CREATE OR REPLACE FUNCTION public.myTriggerToSendSockets()
RETURNS trigger AS
$BODY$
import json
stmt = plpy.prepare("select public.sendSocket(, , )", ["text", "text", "int"])
rv = plpy.execute(stmt, [json.dumps(TD), "myserver", 8881])
$BODY$
LANGUAGE plpython3u VOLATILE
COST 100;
CREATE TRIGGER myTrigger2
AFTER INSERT OR UPDATE OR DELETE
ON event_entity
FOR EACH ROW
EXECUTE PROCEDURE public.myTriggerToSendSockets();
我正在使用一个简单的 python TCP 服务器来接收数据并简单地打印它:
import socket
# Create a socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Ensure that you can restart your server quickly when it terminates
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# Set the client socket's TCP "well-known port" number
well_known_port = 8881
sock.bind(('', well_known_port))
# Set the number of clients waiting for connection that can be queued
sock.listen(5)
# loop waiting for connections (terminate with Ctrl-C)
try:
while 1:
newSocket, address = sock.accept( )
print ("Connected from", address)
# loop serving the new client
while 1:
receivedData = newSocket.recv(1024)
print(receivedData)
if not receivedData: break
newSocket.close( )
finally:
sock.close( )
我希望在此 table 上获得插入的交易,但是当我在 event_entity
上执行操作时,我收到一条空消息:
Connected from ('172.29.0.3', 56326)
b''
我认为 json.dumps(TD)
中的 TD 对象是空的。
有人可以帮忙解决这个问题吗?
我必须在发送之前对消息进行字节编码:
s.sendall(msg.encode())
我正在尝试监控 postgres 数据库中的 table。
我想将此 table 上的每个操作(插入或更新或删除)发送到 python tcp 服务器。
所以我尝试关注this tutorial,但没有成功。
这是我正在使用的 SQL 脚本:
CREATE LANGUAGE plpython3u;
CREATE OR REPLACE FUNCTION public.sendsocket(msg character varying, host character varying, port integer)
RETURNS integer AS
$BODY$
import _socket
try:
s = _socket.socket(_socket.AF_INET, _socket.SOCK_STREAM)
s.connect((host, port))
s.sendall(msg)
s.close()
return 1
except:
return 0
$BODY$
LANGUAGE plpython3u VOLATILE
COST 100;
ALTER FUNCTION public.sendsocket(character varying, character varying, integer)
OWNER TO keycloak;
CREATE OR REPLACE FUNCTION public.myTriggerToSendSockets()
RETURNS trigger AS
$BODY$
import json
stmt = plpy.prepare("select public.sendSocket(, , )", ["text", "text", "int"])
rv = plpy.execute(stmt, [json.dumps(TD), "myserver", 8881])
$BODY$
LANGUAGE plpython3u VOLATILE
COST 100;
CREATE TRIGGER myTrigger2
AFTER INSERT OR UPDATE OR DELETE
ON event_entity
FOR EACH ROW
EXECUTE PROCEDURE public.myTriggerToSendSockets();
我正在使用一个简单的 python TCP 服务器来接收数据并简单地打印它:
import socket
# Create a socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Ensure that you can restart your server quickly when it terminates
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# Set the client socket's TCP "well-known port" number
well_known_port = 8881
sock.bind(('', well_known_port))
# Set the number of clients waiting for connection that can be queued
sock.listen(5)
# loop waiting for connections (terminate with Ctrl-C)
try:
while 1:
newSocket, address = sock.accept( )
print ("Connected from", address)
# loop serving the new client
while 1:
receivedData = newSocket.recv(1024)
print(receivedData)
if not receivedData: break
newSocket.close( )
finally:
sock.close( )
我希望在此 table 上获得插入的交易,但是当我在 event_entity
上执行操作时,我收到一条空消息:
Connected from ('172.29.0.3', 56326)
b''
我认为 json.dumps(TD)
中的 TD 对象是空的。
有人可以帮忙解决这个问题吗?
我必须在发送之前对消息进行字节编码:
s.sendall(msg.encode())