python-gammu
A taste of python-gammu
Python-gammu allows you to easily access the phone. Following code will connect
to phone based on your Gammu configuration (usually stored in
~/.gammurc) and gets network information from it:
import gammu
import sys
# Create state machine object
sm = gammu.StateMachine()
# Read ~/.gammurc
sm.ReadConfig()
# Connect to phone
sm.Init()
# Reads network information from phone
netinfo = sm.GetNetworkInfo()
# Print information
print 'Network name: %s' % netinfo['NetworkName']
print 'Network code: %s' % netinfo['NetworkCode']
print 'LAC: %s' % netinfo['LAC']
print 'CID: %s' % netinfo['CID']
API documentation
gammu– Mobile phone accessgammu.smsd– SMSD accessgammu.data– Generic data usable with Gammugammu.worker- Asynchronous communication to phone.gammu.exception– Gammu exception handlinggammu.GSMErrorgammu.ERR_ABORTEDgammu.ERR_BADFEATUREgammu.ERR_BUGgammu.ERR_BUSYgammu.ERR_CANCELEDgammu.ERR_CANTOPENFILEgammu.ERR_CORRUPTEDgammu.ERR_COULDNT_CONNECTgammu.ERR_COULDNT_RESOLVEgammu.ERR_DATACONVERTEDgammu.ERR_DEVICEBUSYgammu.ERR_DEVICECHANGESPEEDERRORgammu.ERR_DEVICEDTRRTSERRORgammu.ERR_DEVICELOCKEDgammu.ERR_DEVICENODRIVERgammu.ERR_DEVICENOPERMISSIONgammu.ERR_DEVICENOTEXISTgammu.ERR_DEVICENOTWORKgammu.ERR_DEVICEOPENERRORgammu.ERR_DEVICEPARITYERRORgammu.ERR_DEVICEREADERRORgammu.ERR_DEVICEWRITEERRORgammu.ERR_DISABLEDgammu.ERR_EMPTYgammu.ERR_EMPTYSMSCgammu.ERR_FILEALREADYEXISTgammu.ERR_FILENOTEXISTgammu.ERR_FILENOTSUPPORTEDgammu.ERR_FOLDERNOTEMPTYgammu.ERR_FOLDERPARTgammu.ERR_FRAMENOTREQUESTEDgammu.ERR_FULLgammu.ERR_GETTING_SMSCgammu.ERR_GNAPPLETWRONGgammu.ERR_INSIDEPHONEMENUgammu.ERR_INSTALL_NOT_FOUNDgammu.ERR_INVALIDDATAgammu.ERR_INVALIDDATETIMEgammu.ERR_INVALIDLOCATIONgammu.ERR_MEMORYgammu.ERR_MOREMEMORYgammu.ERR_NEEDANOTHERANSWERgammu.ERR_NETWORK_ERRORgammu.ERR_NONEgammu.ERR_NONE_SECTIONgammu.ERR_NOSERVICEgammu.ERR_NOSIMgammu.ERR_NOTCONNECTEDgammu.ERR_NOTIMPLEMENTEDgammu.ERR_NOTRUNNINGgammu.ERR_NOTSUPPORTEDgammu.ERR_OTHERCONNECTIONREQUIREDgammu.ERR_PERMISSIONgammu.ERR_PHONEOFFgammu.ERR_PHONE_INTERNALgammu.ERR_READ_ONLYgammu.ERR_SECURITYERRORgammu.ERR_SHOULDBEFILEgammu.ERR_SHOULDBEFOLDERgammu.ERR_SOURCENOTAVAILABLEgammu.ERR_SPECIFYCHANNELgammu.ERR_TIMEOUTgammu.ERR_UNCONFIGUREDgammu.ERR_UNKNOWNgammu.ERR_UNKNOWNCONNECTIONTYPESTRINGgammu.ERR_UNKNOWNFRAMEgammu.ERR_UNKNOWNMODELSTRINGgammu.ERR_UNKNOWNRESPONSEgammu.ERR_USING_DEFAULTSgammu.ERR_WORKINPROGRESSgammu.ERR_WRITING_FILEgammu.ERR_WRONGCRCgammu.ERR_WRONGFOLDER
- Objects
python-gammu Examples
See also
Many examples are available in /examples directory in the python-gammu git repository.
Sending a message
#!/usr/bin/env python
# Sample script to show how to send SMS
from __future__ import print_function
import gammu
import sys
# Create object for talking with phone
state_machine = gammu.StateMachine()
# Optionally load config file as defined by first parameter
if len(sys.argv) > 2:
# Read the configuration from given file
state_machine.ReadConfig(Filename=sys.argv[1])
# Remove file name from args list
del sys.argv[1]
else:
# Read the configuration (~/.gammurc)
state_machine.ReadConfig()
# Check parameters
if len(sys.argv) != 2:
print("Usage: sendsms.py [configfile] RECIPIENT_NUMBER")
sys.exit(1)
# Connect to the phone
state_machine.Init()
# Prepare message data
# We tell that we want to use first SMSC number stored in phone
message = {
"Text": "python-gammu testing message",
"SMSC": {"Location": 1},
"Number": sys.argv[1],
}
# Actually send the message
state_machine.SendSMS(message)
Sending a long message
#!/usr/bin/env python
# Sample script to show how to send long (multipart) SMS
from __future__ import print_function
import gammu
import sys
# Create object for talking with phone
state_machine = gammu.StateMachine()
# Optionally load config file as defined by first parameter
if len(sys.argv) > 2:
# Read the configuration from given file
state_machine.ReadConfig(Filename=sys.argv[1])
# Remove file name from args list
del sys.argv[1]
else:
# Read the configuration (~/.gammurc)
state_machine.ReadConfig()
# Check parameters
if len(sys.argv) != 2:
print("Usage: sendlongsms.py [configfile] RECIPIENT_NUMBER")
sys.exit(1)
# Connect to the phone
state_machine.Init()
# Create SMS info structure
smsinfo = {
"Class": -1,
"Unicode": False,
"Entries": [
{
"ID": "ConcatenatedTextLong",
"Buffer": "Very long python-gammu testing message "
"sent from example python script. "
"Very long python-gammu testing message "
"sent from example python script. "
"Very long python-gammu testing message "
"sent from example python script. ",
}
],
}
# Encode messages
encoded = gammu.EncodeSMS(smsinfo)
# Send messages
for message in encoded:
# Fill in numbers
message["SMSC"] = {"Location": 1}
message["Number"] = sys.argv[1]
# Actually send the message
state_machine.SendSMS(message)
Initiating a voice call
#!/usr/bin/env python
from __future__ import print_function
import gammu
import sys
# Create object for talking with phone
state_machine = gammu.StateMachine()
# Read the configuration (~/.gammurc or from command line)
if len(sys.argv) > 2:
state_machine.ReadConfig(Filename=sys.argv[1])
del sys.argv[1]
else:
state_machine.ReadConfig()
# Connect to the phone
state_machine.Init()
# Check whether we have a number to dial
if len(sys.argv) != 2:
print("Usage: dialvoice.py NUMBER")
sys.exit(1)
# Dial a number
state_machine.DialVoice(sys.argv[1])
Reading calendar from phone
#!/usr/bin/env python
# Example for reading calendar from phone
from __future__ import print_function
import gammu
# Create object for talking with phone
state_machine = gammu.StateMachine()
# Read the configuration (~/.gammurc)
state_machine.ReadConfig()
# Connect to the phone
state_machine.Init()
# Get number of calendar entries
status = state_machine.GetCalendarStatus()
remain = status["Used"]
start = True
while remain > 0:
# Read the entry
if start:
entry = state_machine.GetNextCalendar(Start=True)
start = False
else:
entry = state_machine.GetNextCalendar(Location=entry["Location"])
remain = remain - 1
# Display it
print()
print("%-20s: %d" % ("Location", entry["Location"]))
print("%-20s: %s" % ("Type", entry["Type"]))
for v in entry["Entries"]:
print("%-20s: %s" % (v["Type"], str(v["Value"])))