Python solution interface Test get phone verification code problem

Source: Internet
Author: User

Recently in the interface test encountered a problem, is that there is a very important interface to use the SMS Verification code, and other interfaces are dependent on this verification code, if no SMS verification code can not test the following interface, so in order to verify the timing of the interface is normal, and do not modify the code, So just think about the solution, if you have a better plan to share with each other


Android Code

Android sends an action-Android.provider.Telephony.SMS_RECEIVED broadcast after it receives a text message, so we just need to write a class-inheriting broadcastreceiver to easily listen to the SMS.

  
 
  1. Package Com.example.getsms;import Android.content.broadcastreceiver;import Android.content.contentresolver;import Android.content.context;import Android.content.intent;import Android.os.bundle;import Android.telephony.smsmessage;import Android.text.textutils;import Android.util.log;public Class Smsinterceptreceiver extends Broadcastreceiver {private final String TAG = "Smsrec";private static final String Sms_extra_name = "PDUs";@Overridepublic void OnReceive (context context, Intent Intent) {TODO auto-generated Method StubString message = "";LOG.E (TAG, "free message");Bundle extras = Intent.getextras (); if (extras! = null) {try {   object[] Smsextra = (object[]) extras.get (sms_extra_name);              &NBSP ; Contentresolver contentresolver = Context.getcontentresolver ();               & nbsp             &NBSP;LOG.E (TAG, "free message");            &NB Sp  for (int i = 0; i < smsextra.length; ++i) {                  &NBSP ; Smsmessage SMS = SMSMESSAGE.CREATEFROMPDU ((byte[]) smsextra[i]);                &N Bsp                      string BODY = sms.getmessagebody () Tostri Ng ();                   message + body;        &NBS P      }                           &NBS P                &NBSP;LOG.E (TAG, "free message:" + message);      &NBS P    } catch (Exception e) {       Todo:handle exceptionLOG.E (TAG, E.getmessage ()); }       }}}

androidmanifest.xml Register the receiver:

  
 
  1.  <receiver Android:name= ". Smsinterceptreceiver ">        <intent-filter>         <action android:name= " Android.provider.Telephony.SMS_RECEIVED "/>        < /intent-filter>        </RECEIVER>

Add Permissions:

  
 
  1. <uses-permission Android:name="Android.permission.RECEIVE_SMS"/>


Python code, mainly through the ADB log to get the SMS information captured by the APK package, and then after the analysis can be used

__author__ = ' Guozhenhua '

#coding =utf-8
Import Urllib2
Import Os,time



#解析短信验证码
Os.system ("adb logcat-c")
cmd= "adb logcat-d |findstr E/smsrec"
#time. Sleep (30);
while (1):
Smscode= os.popen (cmd). Read ()
#print Smscode
if (smscode!= ""):
Smscode=smscode.split ("CAPTCHA:") [1].split (",") [0]
Break


Print "Verification code is:" +smscode

Python solution interface test to get phone verification code problem

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.