在 Raspberry Pi 上使用 Python 连接到 wifi [英] Connect to wifi using Python on Raspberry Pi

查看:80
本文介绍了在 Raspberry Pi 上使用 Python 连接到 wifi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Python 2.7 和 wifi 库(https://wifi.readthedocs.org/en/最新/)在树莓派上.我设法使用以下方法安装了 wifi 库:

sudo pip install wifi

在终端上.该库似乎可以工作,但我无法弄清楚使用密码连接到 wifi 的方法.网站上的文档有点难以理解,尤其是如果你和我一样是 Python 菜鸟.我用过这个:

<预><代码>>>>从 wifi 导入 Cell,Scheme>>>Cell.all('wlan0')

并且我获得了所有可用的 wifi 网络,还有一个我想连接的名为 test1 的网络.所以我确信图书馆是有效的.我按照网站上的步骤操作,但在以下位置出现了权限被拒绝错误:

<预><代码>>>>方案.save()

另外,在此之前还有这个命令:

<预><代码>>>>scheme = Scheme.for_cell('wlan0', 'home', cell)

有人知道那个家"是指什么吗?是 SSID 名称吗?谁能帮我连接到密码是passwordtest1 的名为test1 的wifi?有没有更简单的方法可以通过终端连接到 wifi 以免使用 Python?提前致谢.

解决方案

经过一番研究,我没有找到在 Raspberry Pi 上使用 Python 轻松连接到 wifi 的方法.所以我通过在终端上使用 wifi 命令解决了我的问题:

sudo wifi connect --ad-hoc SSID_Name

它会自动要求我输入: passkey> .在那里你可以输入密码,按回车键,然后它会自动连接到 wifi.之后,我可以运行需要连接到 Internet 才能运行的 Python 脚本.当涉及到我的问题时,wifi 命令比其他终端命令更可取,因为它需要更少的时间手动连接到 wifi.例如,它比使用此终端命令的进程更可取:

sudo nano/etc/network/interfaces

我希望这能帮助所有遇到同样问题的人.

I am using Python 2.7 and wifi library (https://wifi.readthedocs.org/en/latest/) on a Raspberry Pi. I have managed to install wifi library using:

sudo pip install wifi 

on the terminal. The library seems to work but I can't figure out the way to connect to a wifi using a password. The documentation on the website is a bit difficult to understand, especially if you are a Python noob like me. I used this:

>>> from wifi import Cell, Scheme
>>> Cell.all('wlan0')

and I got all the wifi networks available and also the one I want to connect to called test1. So I am sure that the library works. I followed the steps on the website but got a permission denied error at:

>>> scheme.save()

Also, before that there was this command:

>>> scheme = Scheme.for_cell('wlan0', 'home', cell)

Does anyone know what that 'home' refer to? Is it the SSID name? Can anyone help me connect to a wifi called test1 whose password is passwordtest1? Is there any easier way to connect to wifi through terminal so as not to use Python?. Thanks in advance.

解决方案

After some research, I didn't find a way to connect easily to a wifi using Python on a Raspberry Pi. So I solved my problem by using the wifi command on the Terminal:

sudo wifi connect --ad-hoc SSID_Name

Which automatically asks me for an input: passkey> .Where you can actually type the password, press enter and after that it automatically connects to the wifi. After that I can run my Python script which needs a connection to the internet in order to run. The wifi command is preferable to other terminal commands when it comes to my problem cause it needs less time to connect to a wifi manually. For example it is preferable to the process which uses this terminal command:

sudo nano /etc/network/interfaces

I hope this helps everyone who has the same problem.

这篇关于在 Raspberry Pi 上使用 Python 连接到 wifi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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