您好,登錄后才能下訂單哦!
這篇文章主要講解了Python實現接收手機短信的方法,內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。
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實現接收手機短信的方法有進一步的了解,如果還想學習更多內容,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。