Difference between revisions of "Very simple webserver"

From MyWiki
Jump to: navigation, search
(Created page with " <source lang=python> import network import socket import machine led = machine.Pin("LED", machine.Pin.OUT) ssid = 'dlink2' password = 'bananatree' wlan = network.WLAN(netw...")
 
 
Line 1: Line 1:
 
+
This uses a Raspberry Pi Pico W with Wifi
 
<source lang=python>
 
<source lang=python>
 
import network
 
import network

Latest revision as of 09:55, 25 January 2023

This uses a Raspberry Pi Pico W with Wifi

import network
import socket
 
import machine
led = machine.Pin("LED", machine.Pin.OUT)
 
ssid = 'dlink2'
password = 'bananatree'
 
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
 
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
 
s = socket.socket()
s.bind(addr)
s.listen(1)
 
print('listening on', addr)
status = wlan.ifconfig()
print(status[0])
 
 
 
#Listen for Connections
while True:
    try:
        cl, addr = s.accept()
        print('client connected from', addr)
        request = cl.recv(1024)
        print(request)
 
        response = "Hello"
        led.toggle()
        print("below the LED toggle line")
 
        cl.send('HTTP/1.0 200 OK/r/nContent-type: text/html\r\n\r\n')
        cl.send(response)
        cl.close()
 
 
    except OSError as e:
        cl.close()
        print('connection closed')