0

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

    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()
4

2 回答 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()
于 2019-02-12T16:42:38.970 回答
-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 中运行。在任何印度号码上都能完美运行。

于 2019-02-08T10:35:41.153 回答