Connecting Tech Pros Worldwide Help | Site Map

Not forking?

Gilles Ganault
Guest
 
Posts: n/a
#1: Nov 23 '07
Hello

I'd like to rewrite the following Perl script in Python:

http://www.voip-info.org/wiki/view/Asterisk+NetCID

It seems like the following doesn't actually fork, so Asterisk is
stuck until the script ends:

===========
import socket,sys,time,os

def sendstuff(data):
s.sendto(data,(ipaddr,portnum))
return

#BAD?
#import posix
#posix.close(1)
#posix.open("/dev/null", posix.O_WRONLY)
#BAD?

sys.stdout = open(os.devnull, 'w')
if os.fork():
sys.exit(0)

try:
cidnum = sys.argv[1]
except:
print "Format: netcid.py cidnum cidname"
sys.exit(1)

try:
cidname = sys.argv[2]
except:
print "Format: netcid.py cidnum cidname"
sys.exit(1)

now = time.localtime(time.time())
dateandtime = time.strftime("%d/%m/%y %H:%M", now)

myarray = []
myarray.append("STAT Rings: 1")
myarray.append("RING")
myarray.append("NAME " + cidname)
myarray.append("TTSN Call from " + cidname)
myarray.append("NMBR " + cidnum)
myarray.append("TYPE K")

s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET,socket.SO_BROADCAST ,True)

portnum = 42685
#ipaddr = "192.168.0.255"
ipaddr = "localhost"

for i in myarray:
sendstuff(i)
#Must pause, and send IDLE for dialog box to close and call to be
logged in
time.sleep(5)
sendstuff("IDLE " + dateandtime)
===========

What's the correct way to handle this?

Thank you.
greg
Guest
 
Posts: n/a
#2: Nov 24 '07

re: Not forking?


Gilles Ganault wrote:
Quote:
It seems like the following doesn't actually fork,
>
sys.stdout = open(os.devnull, 'w')
if os.fork():
sys.exit(0)
What makes you think it's not forking? Chances are
it *is* forking, but something is going wrong later.
Do you get any traceback?
Quote:
try:
cidnum = sys.argv[1]
except:
print "Format: netcid.py cidnum cidname"
sys.exit(1)
You realise that having directed stdout into /dev/null,
you're not going to see these messages?

What happens if you run it without any output redirection?

--
Greg
Gilles Ganault
Guest
 
Posts: n/a
#3: Nov 30 '07

re: Not forking?


On Sat, 24 Nov 2007 14:33:34 +1300, greg <greg@cosc.canterbury.ac.nz>
wrote:
Quote:
>What makes you think it's not forking? Chances are
>it *is* forking, but something is going wrong later.
You were right. There were scrambled characters in the script
(copy/pasting from Windows), which kept the script from working even
without forking.
Quote:
>You realise that having directed stdout into /dev/null,
>you're not going to see these messages?
Stupid me :-/
Quote:
>What happens if you run it without any output redirection?
It works fine now :-) Thank you for the help.
Closed Thread


Similar Python bytes