Hi All,
Ive got a main python tool which reads syslog and generates a list of servers it see's (this is a test tool to get the functionality correct).
The client script sends a query on a sock and the main prints the contents of its list however i want to send the contents back to the client and read there rather than being output on the main tool.
Code is below
MAIN TOOL
Code:
#!/usr/bin/python
import sys
import time
import re
import threading
import socket
import errno
def handle():
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
PORT = 19999
HOST = '127.0.0.1'
MAX_LENGTH = 4096
try:
serversocket.bind((HOST, PORT))
except socket.error, v:
errorcode=v[0]
print "Error found during startup [ %s ] " % (errorcode)
sys.exit(0)
serversocket.listen(10)
(clientsocket, address) = serversocket.accept()
while True:
buf = clientsocket.recv(MAX_LENGTH)
if buf == '': return #client terminated connection
elif buf == "dump":
for item in serverList:
print item
else:
print "Unrecognised command entered"
print buf
def follow(thefile):
thefile.seek(0,2)
while True:
line = thefile.readline()
if not line:
time.sleep(0.1)
continue
yield line
handlethread = threading.Thread(target=handle)
handlethread.daemon = True
handlethread.start()
serverList = []
print "hello there"
logfile = open("/var/log/messages","r")
loglines = follow(logfile)
for line in loglines:
servername=line.split(' ')[3]
if servername not in serverList:
serverList.append(servername)
print "added %s to list of servers" % (servername)
CLIENT TOOL
Code:
#!/usr/bin/python
import socket
import sys
HOST = '127.0.0.1'
PORT = 19999
s = socket.socket()
s.connect((HOST, PORT))
while 1:
msg = raw_input("Command To Send: ")
if msg == "close":
s.close()
sys.exit(0)
s.send(msg)
Any help in getting the main tool to send the requested back and read would be most appreciated.