Thursday, March 18, 2010

Python Script for sending free sms in INDIA using mycantos (www.mycantos.com)

i had this thing in mind since long.. I had searched a lot on google but couldnt find one..  So, I decided of making my own.. :)

At the end of the page, you will have an idea of:-
  • How to fetch pages in python
  • Handling cookies while fetching pages
  • Do GET & POST FORMS using python

I decided to use python for making the script because I just love python.

For making the script, I used Live HTTP Headers to get the info about the GET & POSTs being performed while running the site..

When I logged into the page of mycantos, i got the following in mycantos....

http://www.mycantos.com/

POST / HTTP/1.1
Host: www.mycantos.com
User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.3) Gecko/20091020 Ubuntu/9.10 (karmic) Firefox/3.5.3 GTB7.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://www.mycantos.com/
Cookie: __utma=18572837.1612947254.1268907002.1268907002.1268909453.2; __utmc=18572837; __utmz=18572837.1268907002.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); username=shadyabhi; __utmb=18572837.4.10.1268909453; PHPSESSID=uevm8nnr6vcf8tui70nc419ac4
Content-Type: application/x-www-form-urlencoded
Content-Length: 49
username=shadyabhi&password=mypassissecret&checklogin=1
HTTP/1.0 302 Found
Server: nginx/0.6.34
Date: Thu, 18 Mar 2010 10:52:06 GMT
Content-Type: text/html; charset=UTF-8
Location: http://www.mycantos.com/myAccount.php
Connection: close
X-Powered-By: PHP/5.2.9
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie: PHPSESSID=n9apaivcsd09rht5459hvb6r23; path=/; domain=.mycantos.com
Set-Cookie: username=shadyabhi; path=/; domain=.mycantos.com
Vary: Accept-Encoding
Content-Encoding: gzip
So, now I know that its doing a POST to "http://www.mycantos.com/" with content = username=shadyabhi&password=mypassissecret&checklogin=1

So, i opened the python interpreter & wrote the following lines...
import urllib
url = 'http://www.mycantos.com/'
data = 'username=shadyabhi&password=mypassissecret&checklogin=1'
f = urllib.urlopen(url,data)
the_page = f.read()
After reading the value of the_page, I got to know the page was still not logged in. So, I was sure that the site will not let me log in until the cookies are enabled. 

So, I used the cookies in the code for logging into the account.

Next, I faced the problem when I was sending SMS on the sendSMS.php page..
When I was doing POST on that page, I was repeatedly getting error page saying "Missing data, Try again!!". After analyzing the data from "View HTTP Headers" addon, I came to know that it was required for the referrer URL to be "http://www.mycantos.com/sendSMS.php" and a valid User-Agent. Otherwise, the site just wont let me send sms..

The script works fine on linux with python 2.6..

So, here is the script :-

#!/usr/bin/python

__author__ = """
NAME: Abhijeet Rastogi (shadyabhi)
Profile: http://www.google.com/profiles/abhijeet.1989
"""

import cookielib
import urllib2
from getpass import getpass
import sys

url = 'http://www.mycantos.com/'

#Credentials taken here
username = raw_input("Enter USERNAME: ")
passwd = getpass()

data = 'username='+username+'&password='+passwd+'&checklogin=1'
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
try:
    usock = opener.open(url, data)
except IOError:
    print "Error fetching page www.mycantos.com\nExiting now.."
    sys.exit()

#Headers added to avoid the Missing data, try again!! error
opener.addheaders = [('Referer','http://www.mycantos.com/sendSMS.php'),('User-Agent','Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.3) Gecko/20091020 Ubuntu/9.10 (karmic) Firefox/3.5.3 GTB7.0')]

message = raw_input("Enter your message: ")
number = raw_input("Enter mobile number: ")
data_to_send = 'checkSMS=1&SMSnumber='+number+'&SMSmessage='+message
url_send = "http://www.mycantos.com/sendSMStoanyone.php"

#SMS send POST
try:
    send = opener.open(url_send,data_to_send)
except IOError:
    print "Error sending SMS\nExiting now.."
    sys.exit()

print "SMS SENT!!!"

Test run:-
shadyabhi@shadyabhi-desktop:~/mycantos$ ./mycantos.py
Enter USERNAME: shadyabhi
Password:
Logged into the page.....
Now, Into the SendSMS page...
Enter your message: Hi. The sms is going to be sent
Enter mobile number: 9510216275
SMS SENT!!!
shadyabhi@shadyabhi-desktop:~/mycantos$
Feel free to distribute the script... :)

25 comments:

  1. couldn't send message using this script. everything works as you say, but the sms does not appear in the receiver's mobile

    ReplyDelete
  2. Try reading the page which you receive after sending the sms..

    After line 42:- add these lines...

    f = open("log.html","w")
    f.write(send.read)
    f.close()

    #Remember, there should be a indentation before the lines..

    After the script is run, a file named log.html will be generated. Open it in your browser, and what page you receive..

    If you have problem modifying the script, give me your email id.. I will send you the modified script.

    ReplyDelete
  3. Hello
    i m using php. but i got missing data message as mention above.

    how can i add Headers to avoid the Missing data, try again!! error in php.

    waiting for reply

    tanik

    ReplyDelete
  4. Set the referer and a valid user agent as i said in the post.. See this link on how to set options. http://goo.gl/HVW7

    ReplyDelete
  5. File "kio.py", line 19, in
    send_page = opener.open('http://www.mycantos.com/sendSMS.php')
    File "/usr/lib/python2.6/urllib2.py", line 389, in open
    response = meth(req, response)
    File "/usr/lib/python2.6/urllib2.py", line 502, in http_response
    'http', request, response, code, msg, hdrs)
    File "/usr/lib/python2.6/urllib2.py", line 427, in error
    return self._call_chain(*args)
    File "/usr/lib/python2.6/urllib2.py", line 361, in _call_chain
    result = func(*args)
    File "/usr/lib/python2.6/urllib2.py", line 510, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
    urllib2.HTTPError: HTTP Error 407: Proxy Authentication Required


    ERROR

    ReplyDelete
  6. This comment has been removed by a blog administrator.

    ReplyDelete
  7. Hi,

    Every time it is login for sending sms. Why don't you keep the session until it expires. because we don't want to login for 10 times for sending 10 SMS.
    Probably it is my suggession.

    ReplyDelete
  8. hey man .... :D nce script ...by the way i wanna ask you about this line

    url_send = "http://www.mycantos.com/sendSMStoanyone.php"

    how if the web change the url every day ...
    can you parsing the url ? if you can..can you give me the example parsing script..thanks :)

    ReplyDelete
  9. appreciate your work. But you don't need this line

    send_page = opener.open('http://www.mycantos.com/sendSMS.php')
    You are already logged in!

    ReplyDelete
  10. hi,
    I need to go through a proxy server which requires authentication and use this script.
    Can you please give me the modifications required in this code?
    -dheeraj.

    ReplyDelete
  11. @dheeruparu
    What kind of authentication do you have?

    ReplyDelete
  12. This comment has been removed by the author.

    ReplyDelete
  13. sorry...didn't read your comment above...
    Our college has Squid proxy authentication on a proxy server(with 192.168.1.254 IP) and username and password....

    Apart from that will this code work from march 1 as TRAI is implementing some new rules from that date?

    ReplyDelete
  14. The script will work till mycantos works. So, don't worry :)

    ReplyDelete
  15. ok.Thanx a lot.But what about the proxy problem.I have to demonstrate this in my college...so...i need to know how to bypass the proxy.I don't give the username and password everytime the sms is sent.

    Regards,
    dheeraj.

    ReplyDelete
  16. @dheeruparu

    try getting this http://ntlmaps.sourceforge.net/ to work and use this http proxy to run the script

    ReplyDelete
  17. thanx a lot.Will try it soon....
    But the sms takes a very long time to send.Is the way2sms version still not working?I desperately need a faster way to send...can u please tell me a faster method to send sms?

    Regards,
    Dheeraj.

    ReplyDelete
  18. @dheeruparu
    I will build an optimized GUI version soon.

    ReplyDelete
  19. But i need the script version for my BE project now....so...can u please tell me any other method to send sms faster?
    Btw,Iam using LabVIEW software and using python script to send sms within it.so...i need it very badly..

    Dheeraj

    ReplyDelete
  20. hey plz mail me modified script and tell me how to run that and requirements plzzzzz
    my gmail id:- p.chh73@ymail.com

    ReplyDelete
  21. I would like to have way2sms script than this one. it woudld be great if someone could provide me

    ReplyDelete
  22. @TEJASWI I already have that in my blogpost but the way requests are handled is changed. I don't have time right now but will update soon.

    ReplyDelete
  23. Working version of way2sms code: http://pastebin.com/8cQyD6nt

    ReplyDelete
  24. is it possible to send message to multiple nos..???

    ReplyDelete
  25. Way2sms changes the links often. That's the reason most of the scripts doesn't work. It's better to write on your own.

    ReplyDelete