Pythontr

husonet | Tarih: 17.04.2024

Python Web Servis Kullanımı

Python web servis örneği teşkil etmesi açısından Posta Güvercini firmasına ait Web servisi üzerinden SMS gönderme örneği hazırlanmıştır.

Aşağıda hazırlamış olduğum CLASS ile basit bir şekilde posta güvercini web servisini kullanarak SMS gönderebilirsiniz.


#!/usr/bin/python
# -*- coding:utf-8 -*-

################################################################################
# Huseyin OZDEMIR
# husonet
# 23.10.2014
# Bu betik posta guvercini web servisini kullanarak sms gonnderir
################################################################################

import re
import httplib


VERSIYON = "1"

# api sabit veriler
KULLANICI_ADI = "xxxxx"
SIFRE = "xxxx"

HOST = 'www.postaguvercini.com'
HOST_SOAP = 'www.postaguvercini.com'

SOAP_SMS_SEND_XML = """<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<SmsInsert_1_N xmlns="http://83.66.137.24/PgApiWs">
<Username>#username#</Username>
<Password>#password#</Password>
<Recepients>
<string>#sms_no#</string>
</Recepients>
<Message>#message#</Message>
</SmsInsert_1_N>
</soap:Body>
</soap:Envelope>"""

# debug et 1 ise
DEBUG = 0

# -----------------------------------------------------------------------------
# PostaGuvercini istemci sinifi.
class PostaGuvercini():
kullanici_adi = None
sifre = None

#--------------------------------------------------------------------------
# Nesne ilk olusturuldugunda calisacak bolum. Eger verildiyse ilk degerler
# set ediliyor.
def __init__(self, sKullaniciAdi = None, sSifre = None):
if sKullaniciAdi is not None:
self.kullanici_adi = sKullaniciAdi
if sSifre is not None:
self.sifre = sSifre
#--------------------------------------------------------------------------
# send sms xml
def send_sms_xml(self, sSmsNo, sMesaj):
try:
result = ''
page = '/api_ws/smsservice.asmx?op=SmsInsert_1_N'
HEADERS = { 'Host':HOST_SOAP,
'Content-Type':'text/xml;charset=utf-8',
'SOAPAction':'http://83.66.137.24/PgApiWs/SmsInsert_1_N' }
# Sunucuya gonderilecek SOAP mesajini hazirla.
req = SOAP_SMS_SEND_XML
req = req.replace('#username#', self.kullanici_adi)
req = req.replace('#password#', self.sifre)
req = req.replace('#sms_no#', sSmsNo)
req = req.replace('#message#', sMesaj)
req = req.replace('#send_date#', "null")
req = req.replace('#expire_date#',"null")
# Sunucuya baglanip verileri almak icin
cnn = httplib.HTTPSConnection(HOST)
print req
cnn.request('POST', page, req, HEADERS)
res = cnn.getresponse().read()
cnn.close()
# print res
result = res
except Exception, err:
if DEBUG:
raise
else:
print(str(err))
result = None
return result

if __name__ == '__main__':
p = PostaGuvercini(KULLANICI_ADI, SIFRE)
print p.send_sms_xml('XXXXXXXXX', 'SMS TESTI')