CircuitPython WiFi 微型 HTTP 服务器

Adafruit CircuitPython相关
MicroPython重要分支
回复
头像
shaoziyang
帖子: 3917
注册时间: 2019年 10月 21日 13:48

CircuitPython WiFi 微型 HTTP 服务器

#1

帖子 shaoziyang »

为 CircuitPython WiFi 设备制作的微型 HTTP 服务器。

代码: 全选

import ampule, socketpool, wifi

@ampule.route("/hello/world")
def light_set(request):
  return (200, {}, 'Hi There!'})

try:
  from secrets import secrets
except ImportError:
  print("WiFi secrets not found in secrets.py")
  raise

try:
  print("Connecting to %s..." % secrets["ssid"])
  print("MAC: ", [hex(i) for i in wifi.radio.mac_address])
  wifi.radio.connect(secrets["ssid"], secrets["password"])
except:
  print("Error connecting to WiFi")
  raise

pool = socketpool.SocketPool(wifi.radio)
socket = pool.socket()
socket.bind(['0.0.0.0', 80])
socket.listen(1)
print("Connected to %s, IPv4 Addr: " % secrets["ssid"], wifi.radio.ipv4_address)

while True:
  ampule.listen(socket)
隐藏内容
你必须登入/注册才可观看隐藏内容
 

回复

  • 随机主题
    回复总数
    阅读次数
    最新文章