Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from os import popen, system
- from urllib import urlencode
- from urllib2 import urlopen, Request
- import sys
- field_from = "sender_name"
- field_contactno = ""
- field_msg = ""
- field_counter = 150-len(field_msg)
- URL_BASE='http://msgctr.m1.com.sg/guest/'
- # extract my JSESSIONID by loading a page from the site
- set_cookie = urlopen(URL_BASE).headers.getheader("Set-Cookie")
- sess_id = set_cookie[set_cookie.index("=")+1:set_cookie.index(";")]
- # construct headers dictionary using the JSESSIONID
- headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/2010010' \
- '1 Firefox/4.0.1',
- 'Host':'msgctr.m1.com.sg',
- 'Referer':'http://msgctr.m1.com.sg/guest/index.jsp?notice=empty',
- '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','Cookie':'JSESSIONID='+sess_id}
- text = (urlopen(Request(URL_BASE+"index.jsp",headers=headers)).read())
- for line in text.split("\n"):
- if "/guest/captcha.jpg" in line:
- line = (line.replace('<img src="','')).strip()
- position = line.find('"')
- captchaText = line[1:position].strip("guest/")
- sessionID = captchaText.strip("captcha.jpg?session=")
- #save img
- localFile = open('img.jpg', 'wb')
- localFile.write(urlopen(Request(URL_BASE+captchaText,headers=headers)).read())
- localFile.close()
- #END-save img#
- #img to text#
- convert = system("convert -contrast-stretch 12% -trim -depth 1 -colorspace Gray img.jpg ocr.tiff");
- read = system("tesseract -psm 7 ocr.tiff result");
- result = popen("cat result.txt", "r");
- result = result.read()
- result=(str(result).replace("\n","")).upper()
- if(len(result)>3 or len(result)<3):
- sys.exit(1)
- else:
- print "Captcha code is "+result
- # encode my POST parameters for the capcha page
- data = urlencode( {"from":field_from,"locale":"en",
- "msisdn":field_contactno,"msg":field_msg,
- "counter":field_counter,"sessionid":sessionID,"code":result} )
- # send captcha
- POST_URL = "http://msgctr.m1.com.sg/guest/processMessage.jsp?msisdn="+str(field_contactno.strip())
- #print POST_URL
- postOutput = urlopen(Request(POST_URL,headers=headers),data).read()
- if "notice=thankyou" in postOutput:
- print "SMS sent"
- else:
- print "Cannot read captcha. Please retry"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement