Sponsor: VoiceMeUp - Corporate & Wholesale VoIP Services

VoIP Mailing List Archives
Mailing list archives for the VoIP community
 SearchSearch 

[asterisk-users] [Linux/Python 2.4.2] Forking Python doesn't


 
Post new topic   Reply to topic    VoIP Mailing List Archives Forum Index -> Asterisk Users
View previous topic :: View next topic  
Author Message
vincent.delporte at bi...
Guest





PostPosted: Wed Feb 13, 2008 7:46 am    Post subject: [asterisk-users] [Linux/Python 2.4.2] Forking Python doesn't Reply with quote

Hello

When a call comes in, I'd like to fork a Python script that
broadcasts a message so that users see the CID name + number pop up on
their computer screen, and simultaneously ring their phones.

The following script doesn't work as planned: It waits until the
script ends before moving on to the next step, which is Dial():

===========
exten => s,1,AGI(netcid.py|${CALLERID(num)}|${CALLERID(name)}) exten
=> s,n,Dial(${MYPHONE},5)
===========
# cat netcid.py
#!/usr/bin/python

import socket,sys,time,os

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

sys.stdout = open(os.devnull, 'w')
if os.fork():
#BAD? sys.exit(0)
os._exit(0)
else:
now = time.localtime(time.time())
dateandtime = time.strftime("NaVm/%y NaVM", 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"

for i in myarray:
sendstuff(i)

#Must pause, and send IDLE for dialog box to close
time.sleep(5)
sendstuff("IDLE " + dateandtime)
===========

In another forum, people told me that I should fork twice. Is that
really necessary?
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/278731

Thank you.
Back to top
dagmoller at yahoo.com.br
Guest





PostPosted: Wed Feb 13, 2008 7:59 am    Post subject: [asterisk-users] [Linux/Python 2.4.2] Forking Python doesn't Reply with quote

Vincent,

try to use System() instead of AGI()

Diego Aguirre
Infodag - Inform?tica
FWD#: 459696
Nikotel#: 99 21 8138-2710
EnumLookup#: +55 21 8138-2710
DUNDi-br#: 21 8138-2710

Vincent escreveu:
Quote:
Hello

When a call comes in, I'd like to fork a Python script that
broadcasts a message so that users see the CID name + number pop up on
their computer screen, and simultaneously ring their phones.

The following script doesn't work as planned: It waits until the
script ends before moving on to the next step, which is Dial():

===========
exten => s,1,AGI(netcid.py|${CALLERID(num)}|${CALLERID(name)}) exten
=> s,n,Dial(${MYPHONE},5)
===========
# cat netcid.py
#!/usr/bin/python

import socket,sys,time,os

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

sys.stdout = open(os.devnull, 'w')
if os.fork():
#BAD? sys.exit(0)
os._exit(0)
else:
now = time.localtime(time.time())
dateandtime = time.strftime("NaVm/%y NaVM", 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"

for i in myarray:
sendstuff(i)

#Must pause, and send IDLE for dialog box to close
time.sleep(5)
sendstuff("IDLE " + dateandtime)
===========

In another forum, people told me that I should fork twice. Is that
really necessary?
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/278731

Thank you.


_______________________________________________
-- Bandwidth and Colocation Provided by http://www.api-digital.com --

asterisk-users mailing list
To UNSUBSCRIBE or update options visit:
http://lists.digium.com/mailman/listinfo/asterisk-users
Back to top
michiel at vanbaak.info
Guest





PostPosted: Wed Feb 13, 2008 8:25 am    Post subject: [asterisk-users] [Linux/Python 2.4.2] Forking Python doesn't Reply with quote

On 13:46, Wed 13 Feb 08, Vincent wrote:
Quote:
Hello

When a call comes in, I'd like to fork a Python script that
broadcasts a message so that users see the CID name + number pop up on
their computer screen, and simultaneously ring their phones.

The following script doesn't work as planned: It waits until the
script ends before moving on to the next step, which is Dial():

In another forum, people told me that I should fork twice. Is that
really necessary?
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/278731

If you want it to detach the program from it's parent you
need the double fork yes.

--

Michiel van Baak
michiel at vanbaak.eu
http://michiel.vanbaak.eu
GnuPG key: http://pgp.mit.edu:11371/pks/lookup?op=get&search=0x71C946BD

"Why is it drug addicts and computer aficionados are both called users?"
Back to top
vincent.delporte at bi...
Guest





PostPosted: Wed Feb 13, 2008 3:59 pm    Post subject: [asterisk-users] [Linux/Python 2.4.2] Forking Python doesn't Reply with quote

On Wed, 13 Feb 2008 10:59:38 -0200, Diego Aguirre
<dagmoller at yahoo.com.br> wrote:
Quote:
try to use System() instead of AGI()

Thanks, but no go. I get an error:

[Feb 13 21:57:55] WARNING[2138]: app_system.c:107 system_exec_helper:
Unable to execute '/tmp/netcid.py|2000|Joe'
Back to top
vincent.delporte at bi...
Guest





PostPosted: Wed Feb 13, 2008 4:02 pm    Post subject: [asterisk-users] [Linux/Python 2.4.2] Forking Python doesn't Reply with quote

On Wed, 13 Feb 2008 14:25:52 +0100, Michiel van Baak
<michiel at vanbaak.info> wrote:
Quote:
If you want it to detach the program from it's parent you
need the double fork yes.

Thanks for the confirmation, but when doing this, the NetCID
application no longer pops up, regardless of whether I put the NetCID
code in the "second parent" or "second child":

============
exten => 9300,1,AGI(/tmp/test5.py|${CALLERID(num)}|${CALLERID(name)})
exten => 9300,n,Dial(${MYPHONE},15)
============
# cat test5.py

#!/usr/bin/python
import socket,sys,time,os

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

log = open('/tmp/output.txt','w')

sys.stdout = open(os.devnull, 'w')
if os.fork():
#Parent
log.write("Step 1\n")
log.close()
os._exit(0)
else:
#Child
os.chdir('/tmp')
os.setsid()
os.umask(0)

if os.fork():
#Parent
log.write("Step 2\n")
log.close()

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"

for i in myarray:
sendstuff(i)

time.sleep(5)
sendstuff("IDLE " + dateandtime)
os._exit(0)
else:
#Child
log.write("Step 3\n")
log.close()
os._exit(0)
============

Has someone already forked a Python script successfully from Asterisk
through AGI?

Thanks.
Back to top
russell at digium.com
Guest





PostPosted: Wed Feb 13, 2008 10:26 pm    Post subject: [asterisk-users] [Linux/Python 2.4.2] Forking Python doesn't Reply with quote

Vincent wrote:
Quote:
On Wed, 13 Feb 2008 10:59:38 -0200, Diego Aguirre
<dagmoller at yahoo.com.br> wrote:
Quote:
try to use System() instead of AGI()

Thanks, but no go. I get an error:

[Feb 13 21:57:55] WARNING[2138]: app_system.c:107 system_exec_helper:
Unable to execute '/tmp/netcid.py|2000|Joe'

The arguments to System() are a bit different. Put it in just like you would
type at the command line.

System(/tmp/netcid.py 2000 Joe)

--
Russell Bryant
Senior Software Engineer
Open Source Team Lead
Digium, Inc.
Back to top
vincent.delporte at bi...
Guest





PostPosted: Thu Feb 14, 2008 9:00 am    Post subject: [asterisk-users] [Linux/Python 2.4.2] Forking Python doesn't Reply with quote

On Wed, 13 Feb 2008 22:26:16 -0500, Russell Bryant
<russell at digium.com> wrote:
Quote:
The arguments to System() are a bit different. Put it in just like you would
type at the command line.

System(/tmp/netcid.py 2000 Joe)

That did it Smile Thanks guys.

BTW, for those interested, I didn't have to double-fork:

======
#!/usr/bin/python

import socket,sys,time,os

sys.stdout = open(os.devnull, 'w')
if os.fork():
sys.exit(0)
else:
#Here, send broadcast
======
Back to top
Display posts from previous:   
Post new topic   Reply to topic    VoIP Mailing List Archives Forum Index -> Asterisk Users All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group

VoiceMeUp - Corporate & Wholesale VoIP Services