Not forking?

Discussion in 'Python' started by Gilles Ganault, Nov 23, 2007.

  1. 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.
     
    Gilles Ganault, Nov 23, 2007
    #1
    1. Advertising

  2. Gilles Ganault

    greg Guest

    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
     
    greg, Nov 24, 2007
    #2
    1. Advertising

  3. On Sat, 24 Nov 2007 14:33:34 +1300, greg <>
    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.
     
    Gilles Ganault, Nov 30, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SRam

    Forking server

    SRam, Aug 21, 2003, in forum: Perl
    Replies:
    1
    Views:
    3,013
  2. SRam

    Forking Server

    SRam, Sep 15, 2003, in forum: Perl
    Replies:
    0
    Views:
    550
  3. Matt Stevens

    Forking HTTP Daemon - Problem

    Matt Stevens, Dec 8, 2003, in forum: Perl
    Replies:
    1
    Views:
    677
    Jim Gibson
    Dec 9, 2003
  4. Ed
    Replies:
    0
    Views:
    699
  5. bob Smith
    Replies:
    0
    Views:
    129
    bob Smith
    Oct 23, 2003
Loading...

Share This Page