I have a long running python script which updated a list with a few "items"
I need to be able to query this running python script while its running to produce a print out of the content of this list.
Im not sure if this is possible on python2.6?
Added code below which basically generates a list of servers from log file. This is a basic script which ill make a lot more intrusive if i was able to dump the list it generates on a need to basis.
UPDATE
so ive got a script which does what i want in theory. The only thing ive noticed is that during the print from the "buf" received from client comes out on the main tool.
I cant rack my head around how to get it from the main tool to the client tool.
#!/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 SCRIPT
#!/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)