Client

This will be similar to the client module from the Making REST requests with urllib recipe:

  1. Import the essential modules for working with RESTful APIs:

        import urllib.request 
        import urllib.parse 
        import json 
  1. There's a sequence of steps to make the POST request that will create a new, shuffled deck. This starts by defining the URL in pieces, by creating a ParseResult object manually. This will be collapsed into a single string later:
        full_url = urllib.parse.ParseResult( 
            scheme="http", 
            netloc="127.0.0.1:5000", 
            path="/dealer" + "/decks", 
            params=None, 
            query=None, 
            fragment=None 
        ) 
  1. Build a Request object from the URL, method, and headers:

 request = urllib.request.Request( url = urllib.parse.urlunparse(full_url), method = "POST", ...

Get Modern Python Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.