nando client p3

This commit is contained in:
Q
2020-05-08 22:47:18 +03:00
parent fe600f8546
commit ff71a588cc

30
nando
View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python #!/usr/bin/env python3
import socket,sys,time,os,configobj import socket,sys,time,os,configobj
@@ -39,11 +39,11 @@ def setup_options():
if options.MSGFILE is None: if options.MSGFILE is None:
if "MSGFILE" in rc.keys(): if "MSGFILE" in rc.keys():
options.MSGFILE=rc['MSGFILE'] options.MSGFILE=rc['MSGFILE']
if options.HOST is None: if options.HOST is None:
parser.error("Host name required") parser.error("Host name required")
if options.PORT is None: if options.PORT is None:
parser.error("Port number required") parser.error("Port number required")
return options return options
@@ -71,7 +71,7 @@ def query_ip(sock,opts):
sys.stdout.write(cols[1]) sys.stdout.write(cols[1])
sys.exit(0) sys.exit(0)
sys.exit(1) sys.exit(1)
def query_host(sock,opts): def query_host(sock,opts):
sock.sendto("list", (opts.HOST, opts.PORT)) sock.sendto("list", (opts.HOST, opts.PORT))
received = sock.recv(BLOCK_SIZE) received = sock.recv(BLOCK_SIZE)
@@ -83,14 +83,14 @@ def query_host(sock,opts):
sys.exit(1) sys.exit(1)
def read_desc(opts): def read_desc(opts):
if not opts.MSGFILE: if not opts.MSGFILE:
return "" return ""
if not os.path.isfile(opts.MSGFILE): if not os.path.isfile(opts.MSGFILE):
return "" return ""
DESC=open(opts.MSGFILE,"rb").read(1024) DESC=open(opts.MSGFILE,"rb").read(1024)
DESC=''.join([ c for c in DESC if c not in ['\n','\r','|'] ]) DESC=''.join([ str(c) for c in DESC.decode('utf-8') if c not in ['\n','\r','|'] ])
return DESC return DESC
opts=setup_options() opts=setup_options()
@@ -103,26 +103,26 @@ while True:
sock.settimeout(opts.TIMEOUT) sock.settimeout(opts.TIMEOUT)
sock.connect((opts.HOST,opts.PORT)) sock.connect((opts.HOST,opts.PORT))
MYIP=sock.getsockname()[0] MYIP=sock.getsockname()[0]
if opts.QUERY_HOSTNAME: if opts.QUERY_HOSTNAME:
query_ip(sock,opts) query_ip(sock,opts)
if opts.QUERY_IP: if opts.QUERY_IP:
query_host(sock,opts) query_host(sock,opts)
if opts.command=="": if opts.command=="":
DESC=read_desc(opts) DESC=read_desc(opts)
MSG="{0}|{1}|{2}\n".format(MYNAME,MYIP,DESC) MSG="{0}|{1}|{2}\n".format(MYNAME,MYIP,DESC)
else: else:
MSG=opts.command MSG=opts.command
sock.sendto(MSG, (opts.HOST, opts.PORT)) sock.sendto(MSG.encode('utf8'), (opts.HOST, opts.PORT))
received = sock.recv(BLOCK_SIZE) received = sock.recv(BLOCK_SIZE)
if opts.command!="": if opts.command!="":
print_table(received) print_table(received.decode('utf8'))
if opts.INTERVAL==0 and opts.command=="": if opts.INTERVAL==0 and opts.command=="":
sock.sendto("list", (opts.HOST, opts.PORT)) sock.sendto("list".encode('utf8'), (opts.HOST, opts.PORT))
received = sock.recv(BLOCK_SIZE) received = sock.recv(BLOCK_SIZE)
print_table(received) print_table(received.decode('utf8'))
errors=0 errors=0
except socket.error: except socket.error:
print("Didn't get reply from {0}:{1}".format(opts.HOST,opts.PORT)) print("Didn't get reply from {0}:{1}".format(opts.HOST,opts.PORT))