Accessing Your Local Web Server from Python Requests

Feb 3, 2024 ยท 2 min read

When building a web application or API locally on your own machine, you'll likely spin up a development server on localhost to test it out. Often, you'll want to access this local server from other scripts and applications running on your machine.

Python's requests library provides an easy way to make HTTP requests to localhost without needing to hardcode URLs or handle low-level socket programming. Here's what you need to know.

The Localhost URL

To access localhost, you'll simply use the address http://localhost or http://127.0.0.1. Both will route to your own local machine.

So to access a web server running on port 8000, you'd use this URL:

http://localhost:8000

The same URL works for Python requests.

Sending Requests to Localhost

Import requests and make a GET request like usual, just using the localhost URL:

import requests

response = requests.get("http://localhost:8000/api/status")
print(response.text)

This will send a GET request to the /api/status path on the local server and print the response.

Handling Security Warnings

When accessing HTTPS URLs on localhost, you may get SSL certificate warnings since localhost uses a self-signed cert by default.

You can disable these warnings:

requests.packages.urllib3.disable_warnings()

Or change the verify option:

requests.get("https://localhost:8000", verify=False)

Summary

Accessing a development server on localhost is easy with Python requests:

  • Use http://localhost or http://127.0.0.1
  • Add the port your server uses like :8000
  • Disable SSL warnings for HTTPS
  • Import requests and call get/post as usual!
  • Leveraging requests allows you to integrate and test your local APIs in scripts as you build out your application.

    Browse by tags:

    Browse by language:

    Tired of getting blocked while scraping the web?

    ProxiesAPI handles headless browsers and rotates proxies for you.
    Get access to 1,000 free API credits, no credit card required!