makaleler / Python Programlama / Python Web Servis Kullanımı

Python Web Servis Kullanımı

23.10.2014 18:13:21

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="[url]http://www.w3.org/2001/XMLSchema-instance[/url]" xmlns:xsd="[url]http://www.w3.org/2001/XMLSchema[/url]" xmlns:soap="[url]http://schemas.xmlsoap.org/soap/envelope/[/url]">
  <soap:Body>
    <SmsInsert_1_N xmlns="[url]http://83.66.137.24/PgApiWs[/url]">
      <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':'[url]http://83.66.137.24/PgApiWs/SmsInsert_1_N[/url]'  }
            # 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')
yazar husonet

Yorumlar

Bu içerik için sizde yorum yapabilirsiniz!
anasayfa | makaleler | haberler | dosyalar | linkler | hakkımızda