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
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!