Hands-On Python Programming With HTTPX
Hands-On Python Programming With HTTPX
Nick Russo
Network and Automation Engineer
http://njrusmc.net
About This Class
Click to edit Master title style
• Lots of
• Pragmatic examples
• Hands-on demonstrations
• Absolutely no
• Academic "foo" and "bar" examples
• Gimmicks and trivia
• Have questions? Please:
• Use Q&A widget, not attendee/group chat
• Keep them timely and relevant
• Don't ask about your specific job
Section 1: HTTP Refresher
Click to edit Master title style
Basic request/response structure
Request methods
Return codes
Use cases
HTTP for Kindergarteners
Click to edit Master title style
• Stuff Transport Protocol (Russ White)
• Data exchange on the web
• Variety of operations/methods
• Create, read, update, and delete (CRUD)
• Less common
• HEAD: Get the headers only; no body
• PATCH: Update part of a resource
HTTP Status Codes – "Successes"
Click to edit Master title style
• 100 – 199: Continue
• 200 – 299: Success
• 200: OK
• 201: Created (new resource)
• 202: Accepted (asynchronous OK)
HTTP/1.1 200 OK
GET / HTTP/1.1
Content-Type: text/html
Host: njrusmc.net
Content-Length: 1796
Accept: */*
Server: AmazonS3
• Generic function:
• httpx.request(method="get")
• httpx.request(method="post")
• Specific functions:
• httpx.get(), httpx.post(), httpx.put(), etc.
• Simple wrappers for the generic function
Specifying Headers
Click to edit Master title style
• Headers are key-value pairs
• Great fit for a Python dictionary
• Often carries API keys/tokens, accepted encodings, etc.
• Very application-dependent
$ cat simple_script.py
import httpx
resp = httpx.get("http://njrusmc.net")
print(resp.status_code, resp.reason)
print(resp.text)
$ python simple_script.py
200 OK
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
(...)
Python Logging
Click to edit Master title style
• Set basic configuration
• Timestamp formatting
• Log level
• Add breakpoint
• "breakpoint()" in py3.7+
• "import pdb; pdb.set_trace()" in py3.6-
Section 3: Handling HTTP Redirection
Click to edit Master title style
Real-life project from my private business
• Design choice
• Disable for security and step-by-step inspection
• Enable for ease of use, especially for trusted use-cases
What is tweeter-lite?
Click to edit Master title style
• Validates a Twitter message (called tweets)
SSL validation
Persistent sessions
• Not ...
• A protocol
• A framework
• A language
• A package
REST Constraints
Click to edit Master title style
• To be RESTful, a system must have/be:
• Uniform interface: resources directly accessible
• Client-server: separate user interface from data storage
• Stateless: requests are independent; no server state
• Cacheable: temporarily storable for improved performance
• Layered system: hierarchical servers, load balancers, etc.
• Code on demand (optional): server extends functionality of
client at runtime by sending code (JavaScript, etc.)
Why HTTP Is Usually a Good Fit
Click to edit Master title style
• Most constraints are met using HTTP
• Don't HAVE to use HTTP, but it's a natural fit
• One URI per resource à HTTP URL in a request
• CRUD operations easily map to HTTP methods
• Client-server and load balancing are inherent
• Caching can be public (CDN) or private (temp Internet files)
HTTP Body: Using "data"
Click to edit Master title style
• Relates to HTTP bodies
• Hosted at https://pokeapi.co/api/v2
Our Goals
Click to edit Master title style
• Project #1: Get details from some Pokémon
• Grab small number of Pokémon once
• Programmatically drill deeper into them
• Unanswered questions
• What about API rate limits?
• How do we know when to stop?
Section 7: REST API Interaction Involving File
Click to edit Master title style
Transfers and Asynchronous API Processing
Complete view of business application
Method customization
What is py-auphonic?
Click to edit Master title style
• Python client for www.auphonic.com REST API
• Many options
• Encoding type (mp3, wav, etc.)
• Noise cancellation
• Loudness adjustment
• Audio smoothing
• Metadata (title, artist, album, track, etc.)
My Workflow – Building a Production
Click to edit Master title style
• A production is a container for files
• Basic steps
• Create production from preset
• Upload file(s) into production
• Produce the audio
• Download the resulting file
Recap and Q&A
Click to edit Master title style
HTTP/REST fundamentals
• Twitter: @nickrusso42518