在Python中使用WlanScan强制进行wifi扫描

时间:2019-06-21 10:27:28

标签: python python-3.x python-2.7 winapi ctypes

我想知道如何从python执行WlanScan函数来启动无线网络扫描。我正在使用python模块win32wifi。它需要使用WlanOpenHandle和接口GUID pInterfaceGuid获得的句柄。我不知道如何获取此GUID。任何帮助将不胜感激。

How do I get this pInterfaceGuid

2 个答案:

答案 0 :(得分:2)

您将获得WlanEnumInterfaces的Guid,它会返回WLAN_INTERFACE_INFO_LIST structure和{strong> InterfaceGuid 成员

WLAN_INTERFACE_INFO structure

答案 1 :(得分:1)

我安装了 Win32WiFi 模块,并简短检查了@Castorix提供的 URL (所有必需的信息都可以在[MS.Docs]: wlanapi.h header上找到),并且源代码,我能够写出这个小例子。

code.py

        if (profileImageUri != null) {

            //upload first image
            profileImageRef.putFile(profileImageUri).continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
                @Override
                public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
                    if (!task.isSuccessful()) {
                        throw task.getException();
                    }
                    return profileImageRef.getDownloadUrl();
                }
            }).addOnCompleteListener(new OnCompleteListener<Uri>() {
                @Override
                public void onComplete(@NonNull Task<Uri> task) {
                    if (task.isSuccessful()) {
                        Uri downloadUri = task.getResult();
                        profileImageUrl = String.valueOf(downloadUri);
                        saveUserInfo(); // method to save the URLs along with other info
                    } else {
                        Toast.makeText(EditProfile.this, "upload failed: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                    btSaveInfo.setVisibility(View.VISIBLE);
                    progressBar.setVisibility(View.GONE);
                }
            });
        }

        if (coverImageUri != null) {

            // Upload second image
            profileImageRef.putFile(coverImageUri).continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
                @Override
                public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
                    if (!task.isSuccessful()) {
                        throw task.getException();
                    }
                    return profileImageRef.getDownloadUrl();
                }
            }).addOnCompleteListener(new OnCompleteListener<Uri>() {
                @Override
                public void onComplete(@NonNull Task<Uri> task) {

                    if (task.isSuccessful()) {
                        Uri downloadCUri = task.getResult();
                        coverImageUrl = String.valueOf(downloadCUri);
                        Toast.makeText(getApplicationContext(), "Cover Picture Uploaded", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(EditProfile.this, "Cover picture upload failed: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                    } 
                }
            });

输出

#!/usr/bin/env python3

import sys
from win32wifi import Win32Wifi as ww


def main():
    interfaces = ww.getWirelessInterfaces()
    print("WLAN Interfaces: {:d}".format(len(interfaces)))
    handle = ww.WlanOpenHandle()
    for idx , interface in enumerate(interfaces):
        print("\n  {:d}\n  GUID: [{:s}]\n  Description: [{:s}]".format(idx, interface.guid_string, interface.description))
        try:
            scan_result = ww.WlanScan(handle, interface.guid)
        except:
            print(sys.exc_info())
            continue
        print("\n  Scan result: {:d}".format(scan_result))
    ww.WlanCloseHandle(handle)


if __name__ == "__main__":
    print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
    main()
    print("\nDone.")

@ EDIT0

根据[SO]: Unable to get all available networks using WlanGetAvailableNetworkList in Python (@CristiFati's answer)更新了代码。现在它将适用于具有多个 WLAN 适配器

的计算机