syslog best practices -- when to call closelog?

2006-07-10 Thread J Rice

I have a question:

When should syslog.closelog() be called?  I have a daemon that spends
most of its time asleep and quiet, but writes messages to the mail log
when active.  Should I open the log at the start and keep it open until
the program closes?  This seems much simpler than issuing three
commands everytime I want to write to the log.

The program will essentially be running constantly.  Is having the log
constantly "open" a problem?  What happens if the program crashes or is
killed without a closelog()?  

Jeff

-- 
http://mail.python.org/mailman/listinfo/python-list


Bidirectional communication over unix socket (named pipe)

2006-03-08 Thread J Rice
Hi, I feel like I should apologize in advance because I must be missing
something fairly basic and fundamental here.  I don't have a book on
Python network programming (yet) and I haven't been able to find an
answer on the net so far.

I am trying to create a pair of programs, one (the client) will be
short-lived (fairly) and the second (server) will act as a cache for
the client. Both will run on the same machine, so I think a simple file
socket is the easiest and most reliable method.

The problem I have is that the client can send to the server, but the
server can't send back to the client because it gets this error:

socket.error: (107, 'Transport endpoint is not connected')

This is despite the client waiting on a socket.recv() statement.  Is
the client really not connected, or is the server unaware of the
connection?  And how do I fix this?

I was able to get this working by switching to AF_INET, but that is not
what I want.

Unix sockets are bidirectional, correct?  I have never programmed one,
but I know that programs like clamav use a socket to receive an email
to scan and return the result.

Any help would be greatly appreciated!

Jeff

*** server.py ***
#!/usr/bin/python
import socket
import os, os.path
import time

if os.path.exists("/tmp/mysock"): os.remove("/tmp/mysock")

server = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
server.bind("/tmp/mysock")

while True:
datagram = server.recv(1024)
if not datagram:
break
print datagram
# the preceeding works, and I see the TEST TEST TEST statement the
client sent

time.sleep(2)
# it dies on the next statement.
server.send("Thank you\n")


server.close()
if os.path.exists("/tmp/mysock"): os.remove("/tmp/mysock")


 *** client.py: ***
#!/usr/bin/python

import socket

client = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
client.connect("/tmp/mysock")


TX = "TEST TEST TEST"
TX_sent = client.send(TX)

if TX_sent <> len(TX):  print "TX incomplete"

while True:
print "Waiting..."
datagram = client.recv(1024)
# the client sits here forever, I see the "waiting appear" but
it doesn't advance beyond
#   the recv statement.
if not datagram:
break
print "Received: ",datagram

client.close()

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Bidirectional communication over unix socket (named pipe)

2006-03-08 Thread J Rice

OK, never fails that I find a solution once I post a problem.  If I use
a stream rather than a datagram, it seems to work fine.

So... for my education, how would I make this work with a datagram, if
I insisted on doing it that way?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Bidirectional communication over unix socket (named pipe)

2006-03-08 Thread J Rice

Hi Donn,
Not sure I fully understand your suggestion.  bind() only works once --
I can't bind again in the client.  Same thing with connect() -- once I
issue a connect in the server, it rejects it in the client.

Doing this as a stream works for what I want, but I would like to
understand why it didn't work with datagram.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Bidirectional communication over unix socket (named pipe)

2006-03-10 Thread J Rice
Thank you, that answers my question!  And it works fine with stream, so
I can do what I want as well.

-- 
http://mail.python.org/mailman/listinfo/python-list


Threads modify "global" variable -- asking for trouble?

2006-03-16 Thread J Rice
I have been experimenting with some thread programming, but as I'm
doing this on my own I am worried I might be making a major mistake.

Here's a brief rundown of what I am working on.  Multiple threads, via
Queue, are used to perform RBL checks on an IP.  The threads are passed
a defined class (ConnectionScore) in their init.  This is used to
collect the results, including a method to add the result.  If I run
the program, everything goes fine and once all the threads complete the
main program has all the data from their RBL checks in the
ConnectionScore class.

It seems to work, even with an obscene (and silly) number of WORKERS
and RBLs to check.  But I was reading in the Python Cookbook this
evening and it raised the issue in my mind of what happens if multiple
threads try to add data to the class at the same time.

I've attached the code below.  The ConnectionClass.addRBL method just
takes the result and adds it to a dictionary object attached to the
class.


#!/usr/bin/python
from UCE_weight import ConnectionScore
from rbl import *
import Queue
import threading
import time, random

starttime = time.time()

WORKERS=5

class Worker(threading.Thread):

def __init__(self,queue,score):
self.__queue = queue
threading.Thread.__init__(self)
self.score = score

def run(self):
while 1:
RBL = self.__queue.get()
# Are we at the end of the queue?
if RBL is None: break
self.result = checkRBL("127.0.0.2",RBL,True,5)
score.addRBL(RBL,self.result[0],self.result[1])

queue = Queue.Queue(0)
score = ConnectionScore()

f = open("rbl.list",'r')
MEGARBL = []
while 1:
line = f.readline().strip()
if not line: break
MEGARBL.append(line)
f.close()


for i in range(WORKERS):
Worker(queue,score).start() # start a worker

for i in MEGARBL:
queue.put(i)

for i in range(WORKERS):
queue.put(None) # add end of queue marker

while 1:
# wait for the queue???
if queue.empty():
break
else:
time.sleep(0.1)


elapsed = time.time() - starttime
rate = len(MEGARBL)/elapsed
rate = str(round(rate,2))
elapsed = str(round(elapsed,2))

score.showRBL()
print "Checked",str(len(MEGARBL)),"zones in",elapsed,"seconds.
(",rate.strip(),"per second )"

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Threads modify "global" variable -- asking for trouble?

2006-03-16 Thread J Rice

My apologizes, I missed the newish FAQ entry on this.  The addrbl()
method looks like this:

 def addRBL(self, testname, result, info=""):
self.testresultsRBL[testname] = result, info

So according to the FAQ, D[x] = y, where D is a dictionary, is atomic
and therefore thread-safe.  Right?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Threads modify "global" variable -- asking for trouble?

2006-03-18 Thread J Rice
Thank you.  Implementing a results queue was much simpler than I
expected, and I think as I add this into the rest of the program it
will avoid a lot of potential problems later too.

-- 
http://mail.python.org/mailman/listinfo/python-list


Function params with **? what do these mean?

2006-03-20 Thread J Rice
I'm sorry for such a basic question, but I haven't been able to phrase
a search that gets me an answer and my books are totally silent on
this.  I have seen a number of python function defs that take
parameters of the form (**param1).  Looks like a pointer... but my
books on python (basic as they are) don't make a mention.  What is
this?

Jeff

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Function params with **? what do these mean?

2006-03-20 Thread J Rice
Wow, this is incredibly useful!  I can understand why an introductory
book wouldn't make use of them, but I am really glad to know about
them.  I can think of a bunch of ways to simply some code I have using
this.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: trying to grasp OO : newbie Q?

2006-04-13 Thread J Rice

Someone should correct me if I'm wrong but:

If you add "print myVar" to __init__, you will see that myVar is
assigned to "2" in that function.  It doesn't change the assignment of
"1" in the class because myVar in __init__ is local to __init__.  If
you want to change myVar for the whole class, you need to reference it
as self.myVar.

If you change your __init__ assignment to self.myVar, you will get the
"2" output you expect.  Also note that if you make a new variable in
__init__ (myVar2)and try to get its value with myObj.myVar2, you will
get an error that the variable doesn't exist.  But if you create the
variable using self.myVar2, assigning it to the class, you will be able
to access it.

Check some of the references on variable scope.

-- 
http://mail.python.org/mailman/listinfo/python-list