简体   繁体   English

使用python连接到wifi网络

[英]Connecting to a wifi network using python

I have this code that is supposed to connect to wifi using a given ESSID and password.我有这个代码应该使用给定的 ESSID 和密码连接到 wifi。 Here is the code:这是代码:

def wifi_connect(essid, password):
# Connect to the wifi. Based on the example in the micropython
# documentation.
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
    print('connecting to network ' + essid + '...')
    wlan.connect(essid, password)
    # connect() appears to be async - waiting for it to complete
    while not wlan.isconnected():
        print('waiting for connection...')
        print('checking connection...')
    print('Wifi connect successful, network config: %s' % repr(wlan.ifconfig()))
else:
    # Note that connection info is stored in non-volatile memory. If
    # you are connected to the wrong network, do an explicity disconnect()
    # and then reconnect.
    print('Wifi already connected, network config: %s' % repr(wlan.ifconfig()))

At first, I got an error message that network was not installed.起初,我收到一条错误消息,说没有安装网络。 This was fixed by simply using pip to install network.这是通过简单地使用 pip 安装网络来解决的。 After I ran this again, it told me that network has no attribute WLAN.再次运行后,它告诉我网络没有属性WLAN。 How do I fix this?我该如何解决? What am I doing wrong?我究竟做错了什么?

You are trying to run code designed for the MicroPython language , and it won't work on CPython (the Python version you'd download from Python.org or find installed on most PCs and servers).您正在尝试运行为MicroPython 语言设计的代码,但它无法在 CPython(您可以从 Python.org 下载或安装在大多数 PC 和服务器上的 Python 版本)上运行。

MicroPython is designed to run on embeddable specialist hardware, and comes with its own library to support the hardware it is running on, including a network module : MicroPython 旨在在可嵌入的专业硬件上运行,并带有自己的库来支持它正在运行的硬件,包括一个network模块

To use this module, a MicroPython variant/build with network capabilities must be installed.要使用此模块,必须安装具有网络功能的 MicroPython 变体/构建。 Network drivers for specific hardware are available within this module and are used to configure hardware network interface(s).该模块中提供了特定硬件的网络驱动程序,用于配置硬件网络接口。

It tells you so in the comments at the top:它在顶部的评论中告诉你:

# [...] Based on the example in the micropython
# documentation.

The code can't run on 'regular' CPython.代码不能在“常规”CPython 上运行。 You installed the PyPI network project , which is a very different module, originally designed to help learn coding for the Raspberry PI .您安装了PyPI network项目,这是一个非常不同的模块,最初旨在帮助学习 Raspberry PI 编码

What project could work depends on your operating system (OS).什么项目可以工作取决于您的操作系统 (OS)。 Different OSes use different programming interfaces to let programs change networks.不同的操作系统使用不同的编程接口来让程序改变网络。 Most have command line tools to let you do this, which should be easy to drive from Python with the subprocess module :大多数都有命令行工具可以让您执行此操作,使用subprocess模块应该很容易从 Python 驱动:

On a PC, you don't need network.py to connect to a Wifi access point as in ESPP32.在 PC 上,您不需要 network.py 来连接到 ESPP32 中的 Wifi 接入点。 You connect normally by OS network connection.您通过操作系统网络连接正常连接。

The only library you need is socket.您唯一需要的库是 socket。 Here an example of code to get data !这是获取数据的代码示例!

import socket
def http_get(url, port):
    _, _, host, path = url.split('/', 3)
    addr = socket.getaddrinfo(host, port)[0][-1]
    s = socket.socket()
    s.connect(addr)
    s.send(bytes('GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % (path, host), 'utf8'))
    while True:
        data = s.recv(100)
        if data:
            print(str(data, 'utf8'), end='')
        else:
            break
    s.close()

http_get('http://micropython.org/ks/test.html',80)
http_get('http://towel.blinkenlights.nl/',23)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Python 3 在 Windows 上连接到 WiFi 网络? - How to connect to WiFi network on Windows using Python 3? 使用 Python 连接到 Network Manager(公司)VPN - Connecting to Network Manager (Company) VPN Using Python 连接到 micro python 中的 Wifi 挂起 - Connecting to Wifi in micro python hangs 在尝试使用python连接到wifi网络时出现ConnectionError - Getting ConnectionError, while trying to connect to wifi network using python 我如何使用 python 连接到 WIFI.network 3 - how can i connect to WIFI network using python 3 Python:连接到连接在 wifi 中继器(WiFi NAT 路由器)上的设备的 IP - Python: connecting to the IP of a device connected on a wifi repeater (WiFi NAT Router) 在Linux上通过Python连接到受保护的WiFi - Connecting to a protected WiFi from Python on Linux 将网络摄像机连接到 python/ opencv - connecting a network camera to python/ opencv Python:给定的网络接口是wifi还是以太网? - Python: is a given network interface wifi or ethernet? 使用 Python 检查 Raspberry Pi 是否连接到任何 WiFi.network(不一定是 Inte.net) - Check status if Raspberry Pi is connected to any WiFi network (Not Internet necessarily) using Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM