Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

66%
+2 −0
Q&A Python http.server how can I programatically refresh the browser page?

Two basic approaches to this, both require having your server inject a <script> tag into your page... The old-school "simple" script, which uses setInterval() to periodically poll your serv...

posted 9mo ago by jimbobmcgee‭

Answer
#1: Initial revision by user avatar jimbobmcgee‭ · 2025-12-17T18:08:53Z (9 months ago)
Two basic approaches to this, both require having your server inject a `<script>` tag into your page...

1. The old-school "simple" script, which uses `setInterval()` to periodically poll your server using `XMLHttpRequest.open()` or `fetch()` to query for changes, either by ETag header or *304 Not Modified* status code (generation of which you must build into your HTTP server).  If the response from the server is *not* the same ETag as the last time you requested it, or the server sends back something *other* than 304, call `window.location.reload()`.

2. The newer-style "complex" script, which establishes either a `WebSocket` or an `EventSource` to your HTTP server, and waits for messages -- the event handler function for that would call `window.location.reload()`.  In your HTTP server code, you *send* a message on that channel whenever you want the client to reload (i.e. when you detect the change to your content).

`EventSource` is *possibly* the cleanest approach (see https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events or https://dev.to/philip_zhang_854092d88473/mastering-server-sent-events-sse-with-python-and-go-for-real-time-data-streaming-38bf for ideas), but the old-school approach is probably *fine* for a local dev instance, where the load can be controlled.

In any case, if you intend to expose your HTTP server to the public, you should make it configurable on the server-side, whether the client script is actually injected.  You probably don't want hundreds of pings to your server from the outside world, every time someone browses your pages.