Python接收手機短信的代碼整理
python解決接口測試獲取手機驗證碼問題的方法:
Android在收到短信后會發(fā)送一個Action為android.provider.Telephony.SMS_RECEIVED的廣播,所以我們只需要寫個類繼承BroadcastReceiver就可以很容易地監(jiān)聽到短信。
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>
添加權(quán)限:
<uses-permission android:name='android.permission.RECEIVE_SMS'/>
python 代碼,主要通過adb log來獲取apk包所截取的短信信息,然后進行分析后既可使用。
__author__ = ’guozhenhua’#coding=utf-8import urllib2import 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接收手機短信的代碼整理的詳細(xì)內(nèi)容,更多關(guān)于Python怎么接收手機短信的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 詳解Android studio 動態(tài)fragment的用法2. 什么是python的自省3. 解決Android studio xml界面無法預(yù)覽問題4. Spring Boot和Thymeleaf整合結(jié)合JPA實現(xiàn)分頁效果(實例代碼)5. 圖文詳解vue中proto文件的函數(shù)調(diào)用6. php模擬實現(xiàn)斗地主發(fā)牌7. Springboot Druid 自定義加密數(shù)據(jù)庫密碼的幾種方案8. Vue封裝一個TodoList的案例與瀏覽器本地緩存的應(yīng)用實現(xiàn)9. vue 使用localstorage實現(xiàn)面包屑的操作10. Vuex localStorage的具體使用

網(wǎng)公網(wǎng)安備