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 theend 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… :)