Your Python SIP "Hello World!" Application

I'm not sure how one is supposed to make a SIP "Hello World!" application, but anyway here it is, a simple application to make call to a destination in less than 100 lines.

Simple Caller Application

This application will make call to a destination URI specified in the command line argument, and establish audio communication with remote party once the call is connected.

import sys
import pjsua as pj

# Logging callback
def log_cb(level, str, len):
    print str,

# Callback to receive events from Call
class MyCallCallback(pj.CallCallback):
    def __init__(self, call=None):
        pj.CallCallback.__init__(self, call)

    # Notification when call state has changed
    def on_state(self):
        print "Call is ",,
        print "last code =",, 
        print "(" + + ")"
    # Notification when call's media state has changed.
    def on_media_state(self):
        global lib
        if == pj.MediaState.ACTIVE:
            # Connect the call to sound device
            call_slot =
            lib.conf_connect(call_slot, 0)
            lib.conf_connect(0, call_slot)
            print "Hello world, I can talk!"

# Check command line argument
if len(sys.argv) != 2:
    print "Usage: <dst-URI>"

    # Create library instance
    lib = pj.Lib()

    # Init library with default config
    lib.init(log_cfg = pj.LogConfig(level=3, callback=log_cb))

    # Create UDP transport which listens to any available port
    transport = lib.create_transport(pj.TransportType.UDP)
    # Start the library

    # Create local/user-less account
    acc = lib.create_account_for_transport(transport)

    # Make call
    call = acc.make_call(sys.argv[1], MyCallCallback())

    # Wait for ENTER before quitting
    print "Press <ENTER> to quit"
    input = sys.stdin.readline().rstrip("\r\n")

    # We're done, shutdown the library
    lib = None

except pj.Error, e:
    print "Exception: " + str(e)
    lib = None

Save the snippet above as

Now you can call another softphone (such as pjsua) by specifying the SIP URI as command line argument. For example, if the other softphone is on "sip:", run the program as follows:

 python sip:

Here's the output of the program when I run it on Windows XP:

 13:05:25.718 os_core_win32. pjlib 0.9.0-trunk for win32 initialized
 13:05:25.718 sip_endpoint.c Creating endpoint instance...
 13:05:25.718          pjlib WinNT IOCP I/O Queue created (00D230A0)
 13:05:25.734 sip_endpoint.c Module "mod-msg-print" registered
 13:05:25.734 sip_transport. Transport manager created.
 13:05:25.968   pjsua_core.c pjsua version 0.9.0-trunk for win32 initialized
Call is  CALLING last code = 0 ()
Press <ENTER> to quit
Call is  EARLY last code = 180 (Ringing)
Call is  CONNECTING last code = 200 (OK)
Hello world, I can talk!
Call is  CONFIRMED last code = 200 (OK)
Call is  DISCONNCTD last code = 200 (Normal call clearing)

More Call Samples

  • sample application to make and receive incoming calls.


  • Authentication: the call samples above are not equipped with any SIP authentication credentials, thus you cannot call URI which requires SIP authentication. Please follow the rest of this tutorial (especially the Account page) to see how to put authentication credentials.
Last modified 16 years ago Last modified on Jul 24, 2008 11:45:17 PM