我们有一个Django项目,需要将way2sms otp验证服务更改为msg91消息服务。
需要更改的代码如下:
(我是Django框架的初学者。请帮助我解决此问题。可以在here上找到MSG91消息服务网站API文档。)
def trainer_verify(request):
request.session["first_name"] = request.POST["first_name"]
request.session["last_name"] = request.POST["last_name"]
request.session["phone"] = request.POST["phone"]
request.session["email"] = request.POST["email"]
request.session["train"] = "true"
# request.session["skills"] = request.POST["skills"]
# request.session["experience"] = request.POST["experience"]
# request.session["language"] = request.POST["language"]
# request.session["date"] = request.POST["date"]
# request.session["city"] = request.POST["city"]
# request.session["address"] = request.POST["address"]
r = random.randint(1111,9999)
sms_OTP = str(r)
r = random.randint(1111,9999)
email_OTP = str(r)
username = "9629493491"
passwd = "aviakash.96"
message = sms_OTP
number = request.POST["phone"]
message = "+".join(message.split(' '))
url = 'http://site24.way2sms.com/Login1.action?'
data = 'username='+username+'&password='+passwd+'&Submit=Sign+in'
data = data.encode('utf-8')
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders = [('User-Agent','Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36')]
try:
usock = opener.open(url, data)
except IOError:
print ("Error while logging in.")
sys.exit(1)
jession_id = str(cj).split('~')[1].split(' ')[0]
send_sms_url = 'http://site24.way2sms.com/smstoss.action?'
send_sms_data = 'ssaction=ss&Token='+jession_id+'&mobile='+number+'&message='+message+'&msgLen=136'
send_sms_data = send_sms_data.encode('utf-8')
opener.addheaders = [('Referer', 'http://site25.way2sms.com/sendSMS?Token='+jession_id)]
try:
sms_sent_page = opener.open(send_sms_url,send_sms_data)
except IOError:
print ("Error while sending message")
fromaddr = 'avinashravi96@gmail.com'
toaddrs = str(request.POST["email"])
# container = MIMEBase('multipart', 'mixed', boundary='BOUNDARY')
msg = "\r\n".join([
"From: avinashravi96@gmail.com",
"To:" +toaddrs,
"Subject:Confirmation email",
"",
email_OTP
])
# container.attach(msg)
# container['Subject'] = 'Confirmation Email'
username = 'avinashravi96@gmail.com'
password = 'aviavinash'
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
db = OTP(mobile_number = number , email_id = toaddrs , sms = sms_OTP , email = email_OTP)
db.save()
return render(request,"train_otp.html",{})