Advertisement
milo2012

M1 sms captcha

Feb 6th, 2012
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.29 KB | None | 0 0
  1. from os import popen, system
  2. from urllib import urlencode
  3. from urllib2 import urlopen, Request
  4. import sys
  5.  
  6. field_from = "sender_name"
  7. field_contactno = ""
  8. field_msg = ""
  9. field_counter = 150-len(field_msg)
  10.  
  11. URL_BASE='http://msgctr.m1.com.sg/guest/'
  12. # extract my JSESSIONID by loading a page from the site
  13. set_cookie = urlopen(URL_BASE).headers.getheader("Set-Cookie")
  14. sess_id = set_cookie[set_cookie.index("=")+1:set_cookie.index(";")]
  15.  
  16. # construct headers dictionary using the JSESSIONID
  17. headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/2010010' \
  18. '1 Firefox/4.0.1',
  19. 'Host':'msgctr.m1.com.sg',
  20. 'Referer':'http://msgctr.m1.com.sg/guest/index.jsp?notice=empty',
  21. 'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  22. 'Accept-Language':'en-us,en;q=0.5',
  23. 'Accept-Encoding':'gzip,deflate',
  24. 'Accept-Charset':'ISO-8859-1,utf-8;q=0.7,*;q=0.7','Cookie':'JSESSIONID='+sess_id}
  25.  
  26. text = (urlopen(Request(URL_BASE+"index.jsp",headers=headers)).read())
  27. for line in text.split("\n"):
  28.     if "/guest/captcha.jpg" in line:
  29.         line = (line.replace('<img src="','')).strip()
  30.         position = line.find('"')
  31.         captchaText = line[1:position].strip("guest/")
  32.         sessionID = captchaText.strip("captcha.jpg?session=")  
  33.  
  34. #save img
  35. localFile = open('img.jpg', 'wb')
  36. localFile.write(urlopen(Request(URL_BASE+captchaText,headers=headers)).read())
  37. localFile.close()
  38.  
  39. #END-save img#
  40. #img to text#
  41. convert = system("convert  -contrast-stretch 12% -trim -depth 1 -colorspace Gray img.jpg ocr.tiff");
  42. read = system("tesseract -psm 7 ocr.tiff result");
  43. result = popen("cat result.txt", "r");
  44. result = result.read()
  45.  
  46. result=(str(result).replace("\n","")).upper()
  47. if(len(result)>3 or len(result)<3):
  48.     sys.exit(1)
  49. else:
  50.     print "Captcha code is "+result
  51.     # encode my POST parameters for the capcha page
  52.     data = urlencode( {"from":field_from,"locale":"en",
  53.         "msisdn":field_contactno,"msg":field_msg,
  54.         "counter":field_counter,"sessionid":sessionID,"code":result} )
  55.     # send captcha
  56.     POST_URL = "http://msgctr.m1.com.sg/guest/processMessage.jsp?msisdn="+str(field_contactno.strip())
  57.     #print POST_URL
  58.     postOutput = urlopen(Request(POST_URL,headers=headers),data).read()
  59.     if "notice=thankyou" in postOutput:
  60.         print "SMS sent"
  61.     else:  
  62.         print "Cannot read captcha.  Please retry"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement