Connect to wifi using Python on Raspberry Pi

2019-08-12 12:32发布

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.

1条回答
Melony?
2楼-- · 2019-08-12 12:43

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.

查看更多
登录 后发表回答