Overview

22 Data over the network

This chapter surveys practical ways to move data across networks with Python and how to turn what you fetch into usable structures. It starts by framing when automation in Python makes sense versus simpler shell tools, then explores common transports for moving files or retrieving data programmatically. Along the way, it stresses workflow concerns such as security, repeatability, scheduling, and the trade-offs between quick, manual downloads and robust, scriptable pipelines.

The chapter presents hands-on patterns for acquiring data: using the standard library to work with FTP (and its TLS variant) for simple, low-security transfers; employing an external library for SFTP over SSH when stronger security is required; and relying on a high-level HTTP client to fetch resources via HTTP/HTTPS, including handling headers, bodies, and automatic URL quoting. It then shifts to APIs—particularly REST-style endpoints—covering query strings, HTTP verbs, and receiving structured responses. JSON is the primary format: you parse it with the built-in module or convenience methods, pretty-print results, and serialize back to files, with cautions about writing multiple objects versus a single container and about memory with very large payloads.

For structured data beyond JSON, the chapter demonstrates turning XML into familiar Python dictionaries using a lightweight parser that preserves attributes and nested structure in an intuitive way. When no API or downloadable file exists, it introduces basic web scraping: fetching pages and extracting content with an HTML parser via tag searches and CSS selectors, balanced with legal/ethical and load considerations. A short lab ties the techniques together by pulling historical weather data from an API, aligning parallel lists (dates and measurements), and exporting a clean CSV for downstream analysis—illustrating the overarching theme: pick the right transport, parse with format-aware tools, and write concise, maintainable, “Pythonic” data pipelines.

Summary

  • Python has the FTP client in the ftplib library to fetch data over the network via FTP.
  • The parameiko library can be used to fetch files via SFTP
  • Using a Python script may not be the best choice for fetching files. Be sure to consider options like curl and wget from the command line.
  • Using the requests module is your best bet for fetching files by using HTTP/HTTPS and Python.
  • Fetching files from an API using requests is very similar to fetching static files.
  • Parameters for API requests often need to be quoted and added as a query string to the request URL.
  • The most common structured format used for serving data is JSON.
  • XML is a more complex data format for serving data and is still also used.
  • Using a library like Beautiful Soup you can get data by scraping a web site and parsing the HTML.
  • Scraping sites that you don’t control may not be legal or ethical and requires consideration not to overload the server.

FAQ

When should I use a Python script instead of shell tools to fetch data files?Use Python when either (a) simpler options like curl/wget or small shell scripts aren’t available or comfortable for your team, or (b) the retrieval is complex, repeated, or tightly coupled with processing/validation. If a shell script grows beyond a few lines or becomes hard to reason about, switch to Python.
How do I list directories and download a file from an FTP server with ftplib?
import ftplib

ftp = ftplib.FTP("example.org")
ftp.login()                 # or ftp.login(user, password)
ftp.cwd("data")             # change directory
print(ftp.nlst())           # list names

with open("remote.txt", "wb") as f:
    ftp.retrbinary("RETR path/to/remote.txt", f.write)

ftp.quit()
For encrypted FTP over TLS, use ftplib.FTPTLS.
What’s the difference between FTP and SFTP, and how do I use SFTP in Python?FTP is an older file transfer protocol; SFTP is a separate protocol that runs over SSH (not FTP-over-SSH). Python’s standard library lacks SFTP, but the paramiko package supports it.
import paramiko

t = paramiko.Transport((host, 22))
t.connect(username="user", password="secret")
sftp = paramiko.SFTPClient.from_transport(t)
sftp.get("/remote/path/file.txt", "file.txt")  # download
sftp.put("local.txt", "/remote/path/local.txt") # upload
sftp.close(); t.close()
paramiko can run remote commands via SSH, but does not include scp; use your OS scp if needed.
What’s the easiest way to download files over HTTP/HTTPS in Python?Use the requests library.
import requests

r = requests.get("https://example.com/data.txt", timeout=30)
r.raise_for_status()
open("data.txt", "wb").write(r.content)  # use .content for bytes
Use response.text for decoded text. For very large files, stream with stream=True and iterate over chunks.
How do I pass query parameters to an API and handle URL encoding?Let requests handle encoding via the params argument.
import requests

url = "https://api.open-meteo.com/v1/forecast"
params = {
    "latitude": 41.882,
    "longitude": -87.623,
    "current": "temperature_2m,wind_speed_10m",
}
r = requests.get(url, params=params)
requests safely quotes spaces and punctuation in query strings. This also makes code clearer than concatenating raw URLs.
How do I parse JSON from an API response and pretty-print or save it?Use response.json() to get Python objects, and json.dumps/json.dump to pretty-print or persist.
import requests, json

r = requests.get("https://api.example.com/data")
data = r.json()                       # parse JSON
print(json.dumps(data, indent=2))     # pretty print

with open("data.json", "w") as f:
    json.dump(data, f, indent=2)      # save pretty JSON
If saving multiple JSON records, either write one JSON array/object, or write line-delimited JSON and load line by line.
What’s a simple way to turn XML into Python dictionaries?Use xmltodict to parse XML to nested dict/list structures, with attributes prefixed by @ and text content as #text.
import requests, xmltodict

xml = requests.get("https://api.worldbank.org/v2/...&format=xml").text
doc = xmltodict.parse(xml)
# Access attributes with "@attr", text with "#text"; repeated nodes become lists.
This makes XML extraction feel similar to JSON handling.
What should I consider legally and ethically before scraping a website?Scraping sites you don’t control can be a legal gray area. Review the site’s terms of use and robots.txt, be transparent, throttle requests, add delays, and avoid excessive load to prevent accidental denial-of-service. Prefer official APIs when available.
How can I extract links and elements by CSS class with Beautiful Soup?Fetch the page with requests, parse with bs4, then select by tag or CSS selector.
import requests, bs4

html = requests.get("https://example.com").text
soup = bs4.BeautifulSoup(html, "html.parser")

# All links
for a in soup("a"):
    print(a.get("href"), a.get_text(strip=True))

# Elements with class "special"
for el in soup.select(".special"):
    print(el.get_text(strip=True))
How do I write parallel lists from an API (e.g., dates and values) to CSV?Zip the lists and write rows with csv.writer.
import csv

dates = ["2024-07-01", "2024-07-02"]
values = [23.1, 22.5]

with open("out.csv", "w", newline="") as f:
    w = csv.writer(f)
    w.writerow(["Date", "Value"])
    w.writerows(zip(dates, values))   # zip returns an iterator (fine for writerows)
Ensure the lists are the same length or validate before zipping.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • The Quick Python Book, Fourth Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • The Quick Python Book, Fourth Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • The Quick Python Book, Fourth Edition ebook for free