amélioré sexiictl et ajouté des commandes

This commit is contained in:
2016-06-04 19:03:10 +02:00
parent 2053ef7388
commit fd5019f739
3 changed files with 106 additions and 28 deletions

View File

@@ -3,6 +3,7 @@
import socket
import sys
import os
import struct
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sockpath = "/var/tmp/%s/sexiibot.sock" % os.environ["USER"]
@@ -30,6 +31,30 @@ To get a list of possible commands, use command "help"
args = sys.argv
def send_msg(connexion, msg):
# Prefix each message with a 4-byte length (network byte order)
msg = struct.pack('>I', len(msg)) + msg
connexion.sendall(msg)
def recv_msg(connection):
# Read message length and unpack it into an integer
raw_msglen = recvall(connection, 4)
if not raw_msglen:
return b''
msglen = struct.unpack('>I', raw_msglen)[0]
# Read the message data
return recvall(connection, msglen)
def recvall(connection,n):
# Helper function to recv n bytes or return None if EOF is hit
data = b''
while len(data) < n:
packet = connection.recv(n - len(data))
if not packet:
return b''
data += packet
return data
if len(args) < 2:
print(usage, file=sys.stderr)
sys.exit(1)
@@ -47,7 +72,6 @@ else:
message = " ".join(args[1:])
try:
print(message)
sock.connect(sockpath)
except socket.error:
error = """\
@@ -64,6 +88,8 @@ If you need help to generate a config file, use the sexiiwizard script.
print(error, file=sys.stderr)
sys.exit(1)
try:
sock.sendall(bytes(message, 'UTF-8'))
send_msg(sock, bytes(message, 'UTF-8'))
response = recv_msg(sock).decode('UTF-8')
print(response)
finally:
sock.close()