Accessing Resources in Python Without HTTP: Alternatives to the Requests Library

Feb 3, 2024 ยท 2 min read

The Python Requests library is a popular way to access resources over HTTP. But what if you need to interact with a resource that doesn't use HTTP? There are other options in the Python standard library and third-party packages.

Working with Local Files

Reading and writing local files doesn't require any network access. The built-in open function and file objects give you all the tools you need:

with open('data.txt') as f:
  data = f.read()

with open('results.txt', 'w') as f:
  f.write(processed_data)

This provides a file descriptor-based interface for reliable I/O operations without extra dependencies.

Interacting with Databases

Relational databases like PostgreSQL and MySQL are often easier to work with in Python using a database adapter like psycopg or mysql-connector. These handle the network protocol and expose the database as Python objects:

import psycopg

conn = psycopg.connect(dbname='mydata' user='script')
cur = conn.cursor()
cur.execute('SELECT * FROM table')
results = cur.fetchall()

No need to deal with sockets or HTTP at all!

Alternative Protocols with Low-Level Access

For direct TCP, UDP, or other network access, the socket module is the way to go:

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("example.com", 80))
s.send(b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n")

While more complex, sockets allow full control when working with custom protocols.

So in summary, whether accessing files, databases, or lower-level networking - Python has great options for resource access without HTTP and Requests. The standard library and add-on modules provide everything you need!

Browse by tags:

Browse by language:

The easiest way to do Web Scraping

Get HTML from any page with a simple API call. We handle proxy rotation, browser identities, automatic retries, CAPTCHAs, JavaScript rendering, etc automatically for you


Try ProxiesAPI for free

curl "http://api.proxiesapi.com/?key=API_KEY&url=https://example.com"

<!doctype html>
<html>
<head>
    <title>Example Domain</title>
    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
...

X

Don't leave just yet!

Enter your email below to claim your free API key: