What Is Localhost? A Guide for Developers and Testers
Localhost sends a request back to the same device. Learn what it means, how ports and local servers work, and how to fix common testing problems.
Localhost is the name developers use for the loopback destination: a request to localhost targets the same device that made the request. For example, http://localhost:8000 asks your computer for a web page from a service listening on port 8000. It is useful for developing and testing an application on your own machine before making it available on a remote server.
To try a static site quickly, open a terminal in the directory containing its files and run python3 -m http.server 8000. Then visit http://localhost:8000 in your browser. For application code, use the development server provided by your framework.
1. What localhost means
When software connects to localhost, it is connecting back to its own device. The IPv4 loopback address 127.0.0.1 is commonly used for this purpose; localhost is the familiar hostname for that destination. Loopback is different from a local-network address such as 192.168.0.1, which identifies a device on a network rather than the current computer. MDN describes the loopback and local address spaces.
In a URL such as http://localhost:8000/products:
httpis the scheme.localhostis the host.8000is the port./productsis the path.
The port identifies which service endpoint on the device should receive the connection. The server must be listening at the host and port you enter. If the port is omitted, the scheme’s default port is used; HTTP normally uses 80 and HTTPS 443. See MDN’s URI authority reference.
2. Localhost versus a local-network address
| Destination | What it reaches | Typical use |
|---|---|---|
localhost or 127.0.0.1 |
The same device making the request | Run and inspect a development server on your computer |
A private network address such as 192.168.0.1 |
A device reachable on the local network | Reach a router, another computer, or a service configured for network access |
| A public hostname | A host reached through the internet | Visit a deployed site or remote service |
Binding a development server to loopback usually makes it available only from that device. To let another device on the network reach it, the server must listen on an appropriate network interface, and the network and firewall must allow the connection. The exact command depends on the framework. A local-network address is not interchangeable with localhost: one targets the current device, while the other identifies a network destination.
3. Start a local web server
Use the development server that comes with your framework when your project has one. Those servers understand the framework’s routing, build steps, and application behavior. For a simple directory of static files, Python’s built-in server is a convenient option. MDN’s local testing guide also covers Node.js, PHP, and editor extensions.
Serve a static directory with Python
- Open a terminal in the folder you want to serve.
- Start the server on port 8000.
- Open the URL in a browser.
python3 -m http.server 8000
# On Windows, try one of:
python -m http.server 8000
py -3 -m http.server 8000
Go to http://localhost:8000/. The server exposes files from the current directory; if an index page is not present, the server may show a directory listing. Stop it with Ctrl+C. If port 8000 is occupied, choose a different port, for example python3 -m http.server 7800, and visit http://localhost:7800/.
This simple server serves files. It does not execute arbitrary application code or replace a framework’s development server. Use the tool appropriate to the project.
Serve static files with Node.js
If Node.js and npm are installed, the http-server package can serve a directory. This command uses npx to run it and opens the site on port 9999:
npx http-server . -o -p 9999
Visit http://localhost:9999/. The dot means the current directory. To serve another directory, replace it with that directory’s path. This is a static file server, not a substitute for the development command of a server-side framework.
Run the project’s development server
For a framework application, start the server using that framework’s documented development command, then use the host and port it reports. For example, MDN’s Django tutorial starts a project with python3 manage.py runserver and serves it on 127.0.0.1:8000 by default. Development servers are intended for development, not as a production deployment. See MDN’s Django example.
4. Why a local server is different from opening a file
Opening an HTML file directly gives it a file:// URL. That is not the same environment as requesting a page over HTTP from localhost. A local server is useful when a page uses asynchronous requests, server-side code, or related files that the browser restricts when loaded as local files. Some browsers treat local files as opaque origins, which can cause cross-origin errors for related resources. MDN explains the common local-file testing problems, and its CORS error guide recommends serving files from a local server when needed.
Use file:// for a quick look at a self-contained file when it works. Use localhost when you need ordinary HTTP behavior, same-origin loading from a local server, or server-side execution supplied by your application server.
5. Test a local page with cURL, Python, or Node.js
These examples request the local server from the same machine where it is running. Replace the path or port to match your server.
cURL
curl -i http://localhost:8000/
The -i option includes response headers. A successful request returns an HTTP response; a connection error usually means there is no server listening at that host and port.
Python
import urllib.request
url = "http://localhost:8000/"
with urllib.request.urlopen(url, timeout=10) as response:
print("Status:", response.status)
print(response.read().decode("utf-8", errors="replace"))
Node.js
const response = await fetch("http://localhost:8000/");
console.log("Status:", response.status);
console.log(await response.text());
Run the Node.js example in an environment with global fetch support. A request from a script running on your computer is different from a browser page hosted on another origin: browser same-origin and local-network rules can affect the latter.
6. Localhost, secure contexts, and browser access
Browsers treat localhost and loopback specially for secure-context checks. MDN lists localhost and loopback URLs among potentially trustworthy origins, allowing some secure-context-only features to work in applicable local contexts. This is a special case for local development; it does not mean that any arbitrary HTTP website is secure. Frame ancestry and browser behavior can also matter. See MDN’s secure-context guidance.
A separate issue arises when a page on the public internet tries to contact localhost or another local device. Browser local-network protections exist because a malicious site could otherwise try to send requests to devices such as routers or printers. Supporting browsers may require permission, and policies can constrain access. Do not assume that because http://localhost works as a top-level page, a remote page can freely fetch it. See MDN’s local-network access guidance.
MDN marks Request.targetAddressSpace as experimental or limited in availability. Treat examples using it as browser-dependent; check the target browser’s current compatibility information before relying on it. Local-network access behavior varies by browser and version.
7. Troubleshooting localhost
| Symptom | Likely cause | What to do |
|---|---|---|
| “Connection refused” or the page cannot be reached | No process is listening on that port, or the server stopped. | Start the server and verify the exact port printed by its command. Try curl -i http://localhost:8000/ locally. |
| The browser shows the wrong page or a 404 | You requested the wrong path, or started the server in a different directory. | Check the URL path and the server’s working directory. Request / first and inspect available files or application routes. |
| “Address already in use” | Another process already owns the chosen port. | Stop the other server if appropriate, or pick another port and update the browser URL to match. |
| A page works as a file but fetches fail, or a file gives a CORS error | file:// has different origin and request behavior from HTTP. |
Serve the project through localhost and make requests using the correct URL. If the request goes to a different origin, configure that server’s CORS response deliberately. |
| Server-side code appears as text, downloads, or does not run | A static file server is serving source files rather than executing them. | Start the language or framework’s application server, such as the project’s documented development command. |
| Another device cannot open your localhost URL | On that other device, localhost points back to itself; the development server may also bind only to loopback. | Use the development machine’s reachable network address and configure the server to listen on a network interface, following the framework’s instructions. Check firewall and network access. |
| A browser request from a remote HTTPS site to localhost fails | Browser local-network permission, mixed-content, CORS, or local-address-space protections may apply. | Check browser console errors and the target browser’s local-network guidance. A top-level local page working does not prove a third-party page can access it. |
localhost and 127.0.0.1 behave differently |
The server may be bound to only one address family or interface; hostname resolution and browser configuration can differ. | Check the server’s bind address and try the address shown in its startup output. Keep the browser URL aligned with the server’s binding and allowed origins. |
8. Performance, reliability, and cost
Loopback avoids sending a request to a remote machine, but the application still has to start, process the request, and return a response. A slow local page can still be caused by expensive application work, large assets, or dependencies. A development server may reload code or provide debugging behavior that differs from a production deployment, so verify deployment behavior in an appropriate staging or production-like environment too.
For reliable local testing, keep track of the server’s working directory, bind address, and port; use the framework’s own startup output; and stop background servers when finished. Local development usually uses software and tools already part of the project rather than a dedicated localhost service fee. Hosting the same application remotely, exposing it to a network, or using third-party services can have separate costs and operational requirements.
9. Capture a screenshot of a public page without setting up a browser
For a page already reachable on the web, ScreenshotNeo is a website screenshot API and MCP server from Yorker Media. A screenshot service runs outside your computer, so a private localhost URL is not automatically reachable by it. Use this API with a publicly accessible page; for a local page, use your local browser or make the page reachable through an appropriately configured environment.
One GET request returns an image or PDF. The examples below use the API’s documented request shape; see the ScreenshotNeo API documentation for options.
cURL
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
Python
import requests
r = requests.get("https://api.screenshotneo.com/v1/shot", params={"access_key": "YOUR_API_KEY", "url": "https://stripe.com"}, timeout=90)
open("shot.webp", "wb").write(r.content)
Node.js
const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://stripe.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);
if (!res.ok) throw new Error(`Screenshot request failed: ${res.status}`);
await Bun.write("shot.webp", res);
In Node.js, save the response body using the file API available in your runtime; for example, with modern Node.js:
import { writeFile } from "node:fs/promises";
const q = new URLSearchParams({ access_key: "YOUR_API_KEY", url: "https://stripe.com" });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);
if (!res.ok) throw new Error(`Screenshot request failed: ${res.status}`);
await writeFile("shot.webp", Buffer.from(await res.arrayBuffer()));
Or skip the browser setup
ScreenshotNeo removes cookie banners, newsletter popups, and chat widgets before the shot. Bot checks, blank pages, and failed loads are never billed, and response headers identify the page verdict and billing status. An MCP server lets AI agents use screenshot tools. The free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000 screenshots.
Sign up free for 1,000 screenshots a month, with no card required.
10. FAQ
Is localhost the same on every computer?
No. It always refers to the device making the request, so the specific destination differs from one computer to another.
Does localhost require an internet connection?
A local server and a browser on the same device can communicate over loopback without reaching a public website. Your application may still depend on internet-hosted APIs or assets.
Can I use localhost as my live website address?
No. Visitors’ localhost URLs point to their own devices. A public site needs to be hosted at an address its intended visitors can reach.
Why does localhost sometimes use port 3000 or 5173?
Those are port choices made by particular development tools or projects. Use the port printed by the server or configured for your application.


