By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,529 Members | 1,860 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,529 IT Pros & Developers. It's quick & easy.

Not forking?

P: n/a
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.
Nov 23 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Gilles Ganault wrote:
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?

--
Greg
Nov 24 '07 #2

P: n/a
On Sat, 24 Nov 2007 14:33:34 +1300, greg <gr**@cosc.canterbury.ac.nz>
wrote:
>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.
Nov 30 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.