一年前,因爲工做須要,給以色列的同事解釋一下國都短信平臺的短信發送格式,原本不懂python的我硬着頭皮寫了一個sample,比較粗,能用,但不優美,但願之後學會python能改得像我同事寫的那麼優雅python
#!/usr/bin/python #coding:utf8 import sys,httplib,urllib,urllib2 import xml.etree.ElementTree as ET sms=u'測試短信內容【簽名部分】' #input message here with '' gbk_string=sms.encode("gbk") #encode to GBK params = urllib.urlencode({'OperID':'國都平臺帳號', 'OperPass':'平臺密碼', 'SendTime':'發送時間', 'ValidTime':'', 'AppendID':'附加碼', 'DesMobile':'接受短信的手機號碼', 'Content':gbk_string, 'ContentType':'8'}) url='http://平臺接口地址/QxtSms/QxtFirewall' send = urllib2.Request(url,params) response = urllib2.urlopen(send) res_page = response.read() xml=res_page.decode('gbk') xml=xml.replace('gbk', 'utf-8') root = ET.fromstring(xml) codes=root.getiterator('code') status=codes[0].text if status == '03': print "短信發送成功!" else: print "短信發送失敗!"