Not forking?

G

Gilles Ganault

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.
 
G

greg

Gilles said:
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?
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?
 
G

Gilles Ganault

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.
You realise that having directed stdout into /dev/null,
you're not going to see these messages?

Stupid me :-/
What happens if you run it without any output redirection?

It works fine now :) Thank you for the help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top