Way2SMS python代码不发送SMS而POSTS返回Success

问题描述 投票:0回答:2

我获得了POST请求的成功状态代码,登录工作正常,但未发送短信我已经浏览了互联网上的所有代码,其中大多数都已过时,因为该网站已更改其代码。

    import requests as req

    def login_way2sms():
        with req.Session() as mySession:

            url = 'http://www.way2sms.com/re-login'
            home_url = 'http://www.way2sms.com/'
            mobile = [your registered  mobile number]
            password = [your password]
            headers = dict(Referrer="http://www.way2sms.com/")
            before = mySession.get(home_url)
            login_data = dict(mobileNo=mobile, password=password, CatType='', redirectPage='', pid='')
            mySession.post(url, data=login_data, headers=headers)
            after = mySession.get(home_url)
            return mySession

    def send_msg(mysession): #saw sendsms-toss in Inspect under Network tab
            url = 'http://www.way2sms.com/smstoss'
            home_url = 'http://www.way2sms.com/'
            sms_url = 'http://www.way2sms.com/send-sms'
            group_contact_url = 'http://www.way2sms.com/GroupContacts'
            web_msg_count_url = 'http://www.way2sms.com/CheckWebMsgCount'

            headers = dict(Referrer="http://www.way2sms.com/send-sms")
            before = mysession.get(home_url)
            token = '2B7CF7C9D2F14935795B08DAD1729ACF'
            message = 'How to make this work?'
            mobile = '[a valid phone number]'
            ssaction = 'undefined'
            senderid = 'WAYSMS'
            msg_data = dict(Token=token, message=message, toMobile=mobile, ssaction=ssaction, senderId=senderid)

            mysession.post(url, data=msg_data, headers=headers)
            after = mysession.get(home_url)

            mysession.post(group_contact_url, headers=headers)
            group_contacts = mysession.get(sms_url)

            mysession.post(web_msg_count_url, headers=headers)
            web_msg_count = mysession.get(sms_url)

    # last 2 POST requests send after clicking the Send Msg button

    def main():
        login_way2sms() #login using username and password 
        send_msg(currsession) #send sms 


    main()
python post python-requests sms way2sms
2个回答
0
投票

我终于做对了,谢谢你的回复。我们可以在不使用apikey和密钥的情况下完成,这里看一看。而init只是另一个脚本,其中定义了常量URL和登录,没有那么多。

import requests as req
import init


def login_way2sms(credential):
    with req.Session() as mySession:
        mobile = credential.username
        password = credential.password
        headers = dict(Referrer="http://www.way2sms.com/")
        login_data = dict(mobileNo=mobile, password=password, CatType='', redirectPage='', pid='')
        mySession.post(init.login_url, data=login_data, headers=headers)
        return mySession


def get_token(mysession):
    cookies = mysession.cookies['JSESSIONID']
    token = cookies[4:]
    return token


def send_msg(mysession, token):
    """
        :rtype: req.Session()
    """
    headers = dict(Referrer="http://www.way2sms.com/send-sms")
    message = 'Hi, I am Upgraded a little!!!'
    mobile = '[valid phone]'
    msg_data = dict(Token=token, message=message, toMobile=mobile, ssaction=init.ssaction, senderId=init.senderid)
    mysession.post(init.sms_url, data=msg_data, headers=headers)


def main():
    credential = init.enter_credentials()
    currsession = login_way2sms(credential)
    reply = currsession.get(init.home_url)
    page_content: str = str(reply.content)
    if (reply.status_code == 200) and (page_content.find('send-sms', 10, 200) != -1):
        print("Login Successful!\n")
    else:
        print("Login Failed ,  Try again\n")
        credential = init.enter_credentials()
        currsession = login_way2sms(credential)
    token = get_token(currsession)
    send_msg(currsession, token)


main()

-1
投票

在way2sms创建免费帐户后,以下方法和代码对我有用(我希望你已经这样做了)。然后点击API标签,然后点击左侧的广告系列。然后创建测试API和密钥(免费提供25条消息限制)。然后使用以下代码 -

import requests
import json

URL = 'http://www.way2sms.com/api/v1/sendCampaign'

# get request
def sendPostRequest(reqUrl, apiKey, secretKey, useType, phoneNo, senderId, textMessage):
  req_params = {
  'apikey':'your_apiKey',
  'secret':'your_secretKey',
  'usetype':'stage'
  'phone': 'receiving_phone_number',
  'message':'The textMessage I want to send',
  'senderid':'Your Name'
  }
  return requests.post(reqUrl, req_params)

# get response
response = sendPostRequest(URL, 'provided-api-key', 'provided-secret', 'prod/stage', 'valid-to-mobile', 'active-sender-id', 'message-text' )
"""
  Note:-
    you must provide apikey, secretkey, usetype, mobile, senderid and message values
and then requst to api
"""
# print response if you want
print response.text

只需填写字段并在python 2.7中运行即可。完美地使用任何印度号码。

© www.soinside.com 2019 - 2024. All rights reserved.