makaleler / Python Programlama / python sms script, sms gönderme scripti turktelekom üzerinden

python sms script, sms gönderme scripti turktelekom üzerinden

14.12.2010 00:36:34

Python ile yazdığım bu fonksiyon, Türk Telekom'un SMS sayfasından, SMS mesajı gönderir. SMS gönderebilmek için öncelikle Türk Telekom'dan kullanıcı adı ve parola almak gerekmektedir.
#!/usr/bin/python
#-*- coding:utf-8 -*-

import pycurl
import StringIO
import urlparse
import re

#Hüseyin ÖZDEMİR tarafından yazılmıştır.
#Husonet
#13.12.2010
#pythontr.com.
#------------------------------------------------------------
def smsGonder(sTelefon, sParola, sAlici, sMesaj):
    url         = "[url]https://www.sms.turktelekom.com.tr[/url]"
    url_giris   = "/fsms_subscriberportal/Login.do"
    url_gonder  = "/fsms_subscriberportal/InputSubmit.do"
    url_cikis   = "/fsms_subscriberportal/logout.do"

    try:
        body    = StringIO.StringIO()
        ch      = pycurl.Curl()
        # Cookie bilgilerini almak için giriş sayfasına git
        ch.setopt(ch.URL,url+url_giris)
        ch.setopt(ch.HEADER,True)
        ch.setopt(ch.WRITEFUNCTION, body.write)
        ch.setopt(ch.SSL_VERIFYPEER, False)
        ch.setopt(ch.SSL_VERIFYHOST, False)
        ch.perform()
        if ch.getinfo(ch.HTTP_CODE) != 200:
            return -10

        #Cookie ve Sessionid bilgilerini al
        regexp =re.search('\Set-Cookie: (.*JSESSIONID=(.*);.*)/',body.getvalue())
        if regexp.group(0) == '':
            return -11
        cookies = ''
        session = ''
        cookies = regexp.group(1)
        session = regexp.group(2)
        del regexp
        
        #----------------------------------------------------
        #Kullanıcı adı ve parolayı kullanarak giriş yap
        #Giriş için cookie göndermek de gerekiyor
        ch.setopt(ch.POST, True)
        ch.setopt(ch.POSTFIELDS, 'phoneNumber='+sTelefon+'&password='+sParola)
        ch.setopt(ch.COOKIE,cookies)
        ch.perform()
        if ch.getinfo(ch.HTTP_CODE) != 200:
            return -12
        # mesaj gonderme sayfasina login olunabilindi mi
        regexp =re.search("\<input type=\"hidden\" name=\"source\" value=\"0"+sTelefon+"\"",body.getvalue())
        if regexp.group(0) == '':
            return -13
        del regexp
        
        #----------------------------------------------------
        #mesaj gönderme sayfasından mesajı gönderir
        veri = "submit=submit&deliveryTime=&source=0"+sTelefon+"&destination="+sAlici+"&msgContent="+sMesaj
        ch.setopt(ch.POSTFIELDS, veri)
        ch.setopt(ch.URL, url+url_gonder+';jsessionid='+session)
        ch.perform()
        if ch.getinfo(ch.HTTP_CODE) != 200:
            return -14
        
        #gonderildi mesajı gelmişmi
        regexp =re.search("( g.{1,6}nderildi)",body.getvalue())
        if regexp.group(0) == '':
            return -15

        #----------------------------------------------------
        # sunucuya session yuku bindirmemek icin cikis yap
        ch.setopt(ch.URL, url+url_cikis+';jsessionid='+session);
        ch.perform()
        ch.close()

        # ---------------------------------------------
        # mesaj sorunsuz gonderildiyse 1 dondur
        if (regexp): return 1


        print body.getvalue()
    except:
        pass

TEL     = "212XXXXXXX"
PAROLA  = "XXXXXX"
ALICI   = "535XXXXXXX"
MESAJ   = "pythontr.com dan merhaba ğüişçö ĞÜİŞÇÖ"

print smsGonder(TEL, PAROLA, ALICI, MESAJ)


İlgili Konular
Clickatell sms göndermek /makale.py?tid=28
yazar husonet

Yorumlar

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