欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python接收手机短信的代码整理

程序员文章站 2022-06-22 11:47:12
python解决接口测试获取手机验证码问题的方法:android在收到短信后会发送一个action为android.provider.telephony.sms_received的广播,所以我们只需要...

python解决接口测试获取手机验证码问题的方法:

android在收到短信后会发送一个action为android.provider.telephony.sms_received的广播,所以我们只需要写个类继承broadcastreceiver就可以很容易地监听到短信。

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";
 
 @override
 public void onreceive(context context, intent intent) {
 // todo auto-generated method stub
 string message = "";
 
 log.e(tag, "free message " );
  
  bundle extras = intent.getextras();
  if ( extras != null ) {
   try {
   object[] smsextra = (object[]) extras.get( sms_extra_name );
    contentresolver contentresolver = context.getcontentresolver();
     
    log.e(tag, "free message " );
    for ( int i = 0; i < smsextra.length; ++i ) {
     smsmessage sms = smsmessage.createfrompdu((byte[]) smsextra[i]);
      
     string body = sms.getmessagebody().tostring();
     message += body;
    }
         
    log.e(tag, "free message : " + message);
   } catch (exception e) {
   // todo: handle exception
   log.e(tag, e.getmessage());
   }
  }  
 }
}

androidmanifest.xml里注册一下接收器:

 <receiver android:name=".smsinterceptreceiver">
   <intent-filter>
   <action android:name="android.provider.telephony.sms_received" />
   </intent-filter>
  </receiver>

添加权限:

<uses-permission android:name="android.permission.receive_sms"/>

python 代码,主要通过adb log来获取apk包所截取的短信信息,然后进行分析后既可使用。

__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("验证码:")[1].split(",")[0]
  break;
print "验证码是:"+smscode

实例扩展:

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";
 
 @override
 public void onreceive(context context, intent intent) {
 // todo auto-generated method stub
 string message = "";
 
 log.e(tag, "free message " );
 
  bundle extras = intent.getextras();
  if ( extras != null ) {
   try {
   object[] smsextra = (object[]) extras.get( sms_extra_name );
    contentresolver contentresolver = context.getcontentresolver();
    
    log.e(tag, "free message " );
    for ( int i = 0; i < smsextra.length; ++i ) {
     smsmessage sms = smsmessage.createfrompdu((byte[]) smsextra[i]);
     
     string body = sms.getmessagebody().tostring();
     message += body;
    }
    
    
    log.e(tag, "free message : " + message);
   } catch (exception e) {
   // todo: handle exception
   log.e(tag, e.getmessage());
   }
  } 
  
 }


}

以上就是python接收手机短信的代码整理的详细内容,更多关于python怎么接收手机短信的资料请关注其它相关文章!

相关标签: Python 手机短信