Home CodeMicropython Reading a web page using a Raspberry Pi Pico W

Reading a web page using a Raspberry Pi Pico W

by rp2040guy71

In this article we will fetch a web page using a Raspberry Pi W running micropython, we will display the source code of the web page in the REPL window

You need to download the latest micropython firmware, you can get this from the following link


You first flash the MicroPython Firmware onto the Raspberry Pi Pico W.

You will need to put the Pico in Bootloader mode by holding the “BOOTSEL” button down while connecting the Pico W to our machine.

You will see a drive appear on your PC called RPI-PI2, drag the uf2 firmware that you downloaded from the micropython link above.

I use Thonny for development – now for the code


Connects to your network using Wi-Fi with your network id and password

Gets the IP Address of the Google homepage and connects to the IP address using a Socket connection

Performs a GET request and prints the result in the Shell window, you will see an example later on in the article

import time
import network
import socket

ssid = 'your network id here'
password = 'your password here'
wlan = network.WLAN(network.STA_IF)
wlan.connect(ssid, password)
# Wait for connection or fail
connection_attempts = 10
# 10 attempts to connect at 1 second intervals
while connection_attempts > 0:
    if wlan.status() < 0 or wlan.status() >= 3:
    connection_attempts -= 1
    print('waiting for a connection.')
# Handle connection error
if wlan.status() != 3:
    raise RuntimeError('network connection has failed')
    status = wlan.ifconfig()
    print( 'ip = ' + status[0] )

# Get IP address for google
addressinfo = socket.getaddrinfo("google.com", 80)
address = addressinfo[0][-1]

# Create a socket and make a HTTP request
mysocket = socket.socket()
mysocket.send(b"GET / HTTP/1.0\r\n\r\n")

# Print the response


This was what I saw in the thonny REPL window


ip =
b'HTTP/1.0 200 OK\r\nDate: Mon, 12 Sep 2022 20:22:45 GMT\r\nExpires: -1\r\nCache-Control: private, max-age=0\r\nContent-Type: text/html; charset=ISO-8859-1\r\nServer: gws\r\nX-XSS-Protection: 0\r\nX-Frame-Options: SAMEORIGIN\r\nSet-Cookie: AEC=AakniGOy2HvUIOceSrSD2uqee5S7_1amHXoccZitXBkO_-NBcv6gNbAtrvE; expires=Sat, 11-Mar-2023 20:22:45 GMT; path=/; domain=.google.com; Secure; HttpOnly; SameSite=lax\r\nAccept-Ranges: none\r\nVary: Accept-Encoding\r\n\r\n<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en-GB"><head><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"><meta content="/logos/doodles/2022/hm-queen-elizabeth-ii-6753651837109804.2-l.png" itemprop="image"><meta content="Queen Elizabeth II 1926 &#8211; 2022" property="twitter:title"><meta content="Queen Elizabeth II'


You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More