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:

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!