Account  |   Register  |   Pricing  |   Asterisk  |   HTTP API   |   Code  |   Batch Client   |   Coverage  |   Status  |   About  |   Contact

Number Portability Lookup - Sample Code for Carrier Query: PYTHON

PYTHON Sample Code:

The following code sample demonstrates how to query the carrier of a mobile subscriber by phone number via the NumberPortabilityLookup query service using the Python programming language.

Other Code Samples & Asterisk Carrier Lookup Examples

To find code samples for other languages, or number portability lookup integration examples for the Asterisk PBX, click here.

Code Sample:

# Python Example code for

import httplib

# Put your username and password here:

# This is the number to be queried:

# Call API to perform HLR Query
conn = httplib.HTTPConnection("")
conn.request("GET", "/npl?user="+username+"&pass="+password+"&msisdn="+number+"&apiver=2.4")
data = conn.getresponse().read()

# Parse the response

# First line indicates success of the query
if response[0]=="QUERYOK":

	# Response returned OK - now read the data into variables

	# This is a basic example querying a single number.
	# If you queried more than one number in a single API call by delimiting MSISDNs
	# with commas, you could extend the block below to loop through lines of response[]
	# for each pair of msisdn/hlrdata until you reach response[n]=="ENDBATCH"

	(msisdn,hlrdata)=response[1].split(" ",2)

	# Use the data as needed. For this example we will just print it to console:

	print "MSISDN ",msisdn
	print "MSC    ",msc
	print "IMSI   ",imsi

	# If we didn't get "QUERYOK", something broke - tell somebody!

	print "Lookup error:",response[0]™ & © 2024 SES IP Holdings Ltd - All Rights Reserved. Service operated by Wizard Island Software LLC.