Add main.py
This commit is contained in:
parent
cf5adc149b
commit
867908bcd3
114
main.py
Normal file
114
main.py
Normal file
@ -0,0 +1,114 @@
|
||||
import select
|
||||
from threading import Thread
|
||||
|
||||
# Load configuration
|
||||
with open("config.json", "r") as config_file:
|
||||
data = json.load(config_file)
|
||||
|
||||
smppserverhostname = data["Hostname"]
|
||||
smppserverport = int(data["Port"])
|
||||
smppserverusername = data["Username"]
|
||||
smppserverpassword = data["Password"]
|
||||
usessl = bool(int(data["SSL"]))
|
||||
smssendsource = data["Source"]
|
||||
smssenddest = data["Destination"]
|
||||
smssendcont = data["Content"]
|
||||
numberofmsgtosend = int(data['Count'])
|
||||
validityperiod = data["Validityperiod"]
|
||||
|
||||
# Setup logging
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
|
||||
def get_state_description(state):
|
||||
state_map = {
|
||||
1: "Enroute",
|
||||
2: "Delivered",
|
||||
3: "Expired",
|
||||
4: "Deleted",
|
||||
5: "Undeliverable",
|
||||
6: "Accepted",
|
||||
7: "Unknown",
|
||||
8: "Rejected",
|
||||
}
|
||||
return state_map.get(state, "Unknown")
|
||||
|
||||
|
||||
# Initialize SMPP client with or without SSL
|
||||
if usessl:
|
||||
ssl_context = ssl.create_default_context()
|
||||
ssl_context.check_hostname = False
|
||||
ssl_context.verify_mode = ssl.CERT_NONE
|
||||
ssl_context.options = ssl.OP_NO_SSLv2
|
||||
client = smpplib.client.Client(smppserverhostname, smppserverport, allow_unknown_opt_params=True,
|
||||
ssl_context=ssl_context)
|
||||
else:
|
||||
client = smpplib.client.Client(smppserverhostname, smppserverport, allow_unknown_opt_params=True)
|
||||
|
||||
client.set_message_sent_handler(
|
||||
lambda pdu: sys.stdout.write('Sent msg with sequence ID {}, got ID back {}\n'.format(pdu.sequence, pdu.message_id)))
|
||||
client.set_message_received_handler(lambda pdu: sys.stdout.write(
|
||||
'DLR received, msg with ID {}, and state {}\n'.format(
|
||||
pdu.receipted_message_id,
|
||||
get_state_description(pdu.message_state)
|
||||
)
|
||||
))
|
||||
|
||||
try:
|
||||
print("Connecting to SMPP Server", smppserverhostname, "on port", smppserverport)
|
||||
client.connect()
|
||||
client.bind_transceiver(system_id=smppserverusername, password=smppserverpassword)
|
||||
print("Connected to SMPP Server", smppserverhostname, "on port", smppserverport)
|
||||
print("Sending", numberofmsgtosend, "messages towards server", smppserverhostname)
|
||||
except Exception as e:
|
||||
print(f"Error connecting to SMPP Server: {e}")
|
||||
client.disconnect()
|
||||
sys.exit(1)
|
||||
|
||||
try:
|
||||
for i in range(numberofmsgtosend):
|
||||
parts, encoding_flag, msg_type_flag = smpplib.gsm.make_parts(smssendcont + str(random.randint(1, 10000)))
|
||||
for part in parts:
|
||||
client.send_message(
|
||||
source_addr_ton=smpplib.consts.SMPP_TON_ALNUM,
|
||||
source_addr=smssendsource,
|
||||
dest_addr_ton=smpplib.consts.SMPP_TON_INTL,
|
||||
destination_addr=smssenddest,
|
||||
short_message=part,
|
||||
data_coding=encoding_flag,
|
||||
esm_class=msg_type_flag,
|
||||
registered_delivery=True,
|
||||
)
|
||||
print('Messages sent successfully.')
|
||||
|
||||
def listen_for_delivery_reports():
|
||||
print("Waiting for delivery reports...")
|
||||
while True:
|
||||
try:
|
||||
# Get the file descriptor associated with the client's socket
|
||||
client_fd = client._socket.fileno()
|
||||
|
||||
# Use select with a timeout to prevent blocking for more than 5 seconds
|
||||
ready = select.select([client_fd], [], [], 5)
|
||||
if ready[0]:
|
||||
client.listen()
|
||||
except smpplib.exceptions.ConnectionError:
|
||||
break
|
||||
|
||||
delivery_report_thread = Thread(target=listen_for_delivery_reports)
|
||||
delivery_report_thread.start()
|
||||
delivery_report_thread.join(timeout=5)
|
||||
client.disconnect()
|
||||
|
||||
except ConnectionError as e:
|
||||
print("Connection error, probably already disconnected.", e)
|
||||
# Handle the connection error here, such as retrying the connection or logging the error.
|
||||
|
||||
# finally:
|
||||
# try:
|
||||
# client.disconnect()
|
||||
# except ConnectionError as e:
|
||||
# print("Connection error, probably already disconnected from SMPP Server", e)
|
||||
# print("Disconnected from SMPP server")
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user