如何使用160by2.com发送短信

时间:2019-02-01 05:02:55

标签: python mechanize

我正在尝试使用by2sms.com发送短信。但我找不到发件人手机的控制器。这是JavaScript呈现的网站。 如way2sms代码所示,还有另一种方法。 我的问题是,如果我使用第一种方法(way2sms一个),如何找到这些URL。

  

smsurl ='http://www.160by2.com/smstoss'

     

data = ssaction = ss&Token ='+ session_id +'&toMobile ='+ number +'&message ='+ message

如果我使用第二个,如何找到控制器字段名称?

使用way2sms发送短信的另一种方式:

def send(username,password,number,message=''):
    #message = "+".join(message.split(' '))
    message = message
    url = 'http://www.160by2.com/re-login'
    data = 'mobileNo='+username+'&password='+password+'&CatType'

    #For Cookies:
    cj = http.cookiejar.CookieJar()
    opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))

    # Adding Header detail:
    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:
        socket = opener.open(url, data.encode('utf-8'))
    except :
        return False


    session_id = str(cj).split('~')[1].split(' ')[0]
    smsurl = 'http://www.160by2.com/smstoss'
    data = 'ssaction=ss&Token='+session_id+'&toMobile='+number+'&message='+message
opener.addheaders = [('Referer', 'http://www.160by2.com/send-sms+'+session_id)]
    print(data) 
    try:
        page = opener.open(smsurl,data.encode('utf-8'))
    except:
        return False
    print("SMS sent")
    return True    
# ====================================================== #

160by2代码:

import mechanize 
import cookielib

br=mechanize.Browser()

class sms_client:
    """Basic class accessingg features of 160by2.com"""
    def __init__(self):
        cj = cookielib.LWPCookieJar()
        br.set_cookiejar(cj)
        br.set_handle_equiv(True)
        br.set_handle_gzip(True)
        br.set_handle_redirect(True)
        br.set_handle_referer(True)
        br.set_handle_robots(False)
        br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]

    def authenticate_site(self,user,pword):
        br.open("http://www.160by2.com")
        br.select_form(nr=0)
        br.form['username']=user
        br.form['password']=pword
        res = br.submit()
        print("Success!\n")
        logincheck = res.read()
        #print logincheck

        soup = BeautifulSoup(logincheck,"html5lib")
        #print(soup)
        regex = re.compile('.*.val("");.*')
        #t=regex.match(str(soup))
        #u = re.findall(r'\#(.*?)\.',str(soup))
        #print(u)
        #t=re.findall(regex,str(soup))
        #print(t)

        s = br.geturl()


        #Check for Valid password here
        if s.find('Login') != -1:
           return False #returns false on password error
        link = s.split("?")
        self.id = link[1]
        return True

    def open_sendpage(self):
        action="SendSMS"
        s = self.get_weburl(action)
        br.open(s)
        br.select_form(nr=0)

    def send_sms(self,mobile_no,msg):
       self.open_sendpage()
       action="SendSMSAction"
       s = self.get_weburl(action)
       s = 'http://www.160by2.com/Main.action?'+self.id
       print(s)
       soup = BeautifulSoup(s,"html5lib")
       #print(soup)
       br.form['hid_msg']=mobile_no #<----- controller doesn't match
       br.form['sendSMSMsg']=msg
       br.form.action = s
       try:
           br.submit()
        except Exception:
           pass #ignore the response exception

    def logout(self):
        self.open_sendpage()
        action="Logout"
        s = self.get_weburl(action)
        br.form.action = s
        br.submit()

    def get_weburl(self,action):
        weburl = "http://www.160by2.com/" + action + ".action?" + self.id
        return weburl

if __name__=="__main__":
    pass
    client = sms_client()
    client.authenticate_site(username,password) # Fill this
    client.send_sms(mobileto,msg) # Fill this
    client.logout()

0 个答案:

没有答案