使用Python将我的Windows计算机与wifi AP关联 [英] Associating my Windows computer to a wifi AP with Python

查看:148
本文介绍了使用Python将我的Windows计算机与wifi AP关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个python脚本,该脚本可以使我的计算机关联到无线访问点(以字符串形式给出).例如,我可能指定我要连接到linksys,而我的脚本将导致计算机执行此操作.

I'm trying to write a python script that can make my computer associate to a wireless access point, given the name as a string. For example, I might specify I want to connect to linksys, and my script would cause the computer to do that.

我查看了问题,但通过查看所提供的链接无法理解该怎么做.

I looked at this question, but wasn't able to understand what to do from looking at the links provided.

有人可以指出我正确的方向吗?

Can somebody point me in the right direction?

推荐答案

我决定接受Paulo的建议,并尝试使用Powershell/命令行.我发现了关于通过命令行连接网络的文章.

I decided to take Paulo's suggestion and try using Powershell/the command line. I found an article about connecting to a network via the command line.

在命令行中,您可以执行以下操作:

From the command line, you can do:

netsh wlan connect <profile-name> [name=<ssid-name>]

...,其中name=<ssid-name>部分是可选的,并且仅在配置文件包含多个ssid时才是必需的.

...where the name=<ssid-name> part is optional and is necessarily only if the profile contains multiple ssids.

但是,看来配置文件必须已经存在于计算机上才能使命令行工作正常.我确实找到了论坛帖子关于以编程方式创建个人资料,但我不希望通过它来进行浏览.

However, it looks like the profile must already exist on the machine in order for the command line stuff to work. I did find a forum post on programatically creating a profile, but I didn't feel like canvassing through it.

如果配置文件名称已经存在,则可以从Python中执行以下操作:

If the profile name already exists, then from Python you can do something similar to the following:

import subprocess

def connect_to_network(name):
    process = subprocess.Popen(
        'netsh wlan connect {0}'.format(name),
        shell=True,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE)
    stdout, stderr = process.communicate()

    # Return `True` if we were able to successfully connect
    return 'Connection request was completed successfully' in stdout        

这是一个不完善的解决方案,我不确定它是否在每种情况下都可以使用,但确实适用于我的特定情况.我以为我会发表我的想法,以防其他人想尝试对其进行修改以使其更好.

It's an imperfect solution, and I'm not entirely sure if it'll work in every case, but it did work for my particular case. I thought I'd post what I came up with in case somebody else wants to try modifying it to make it better.

这篇关于使用Python将我的Windows计算机与wifi AP关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆