smssender.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #coding: utf-8
  2. import hashlib
  3. import urllib,urllib2
  4. import time
  5. import sys
  6. from msmsender_config import *
  7. reload(sys)
  8. sys.setdefaultencoding('utf8')
  9. sms_content = {
  10. 'phonenum' : '18086663630',
  11. 'smsid' : '14126' , #短信模版id
  12. # 'smsv1': urllib.quote('for test'), #短信内容
  13. 'smsv1': urllib.quote_plus('今天 天气 不错'),
  14. 'smsv2' : str('%d' % time.time()) #unix时间戳
  15. }
  16. def sortandhashed(dict):
  17. content = ''
  18. sortcontent = sorted(dict.iteritems(), key=lambda d:d[0])
  19. for each in sortcontent:
  20. content += '%s=%s&' % (each[0],each[1])
  21. content = content[:-1] + auth_key
  22. content_bashed = hashlib.md5(content).hexdigest()
  23. return content_bashed
  24. def encodeurl(dict):
  25. data = ''
  26. for k,v in sms_content.items():
  27. data += '%s=%s%s' % (k,v,'&')
  28. return data
  29. sms_content['auth'] = sortandhashed(sms_content)
  30. data = encodeurl(sms_content)
  31. starttime = time.time()
  32. response = urllib2.urlopen('%s?%s' % (baseurl,data))
  33. endtime = time.time()
  34. print response.read()
  35. print endtime - starttime