AI Workshop: learn to build apps with AI →
Networking and GUI: Create a network request

Join the AI Workshop and learn to build real-world apps with AI. A hands-on, practical program to level up your skills.


Python offers us the urllib standard library package to create network requests.

Create a request using:

from urllib import request
url = 'https://dog.ceo/api/breeds/list/all'

response = request.urlopen(url)
content = response.read()

print(content)

You can also use the with statement to simplify

from urllib import request
url = 'https://dog.ceo/api/breeds/list/all'

with request.urlopen(url) as response:
   content = response.read()

print(content)

The response is a sequence of bytes, as you will notice because the response is wrapped in a b'' string:

b'{"message":{"affenpinscher":[],"african":[],"airedale":[],"akita":[],"appenzeller":[],"australian":["shepherd"],"basenji":[],"beagle":[],"bluetick":[],"borzoi":[],"bouvier":[],"boxer":[],"brabancon":[],"briard":[],"buhund":["norwegian"],"bulldog":["boston","english","french"]},"status":"success"}'

Decode it to a UTF-8 encoded string using content.decode('utf-8')

This gets the HTML content from my website flaviocopes.com:

from urllib import request

url = 'https://flaviocopes.com'

with request.urlopen(url) as response:
   content = response.read().decode('utf-8')

print(content)

You can parse the response as JSON, using the json standard library module:

from urllib import request
import json

url = 'https://dog.ceo/api/breeds/list/all'

with request.urlopen(url) as response:
   content = response.read()

data = json.loads(content)
print(data['status'])

If you need to specify query parameters, use the urllib.parse module to build the query string (e.g. parse.urlencode()):

from urllib import request, parse
url = 'https://api.thecatapi.com/v1/images/search'

params = {
    'limit' : 5,
    'page' : 1,
    'order' : 'Desc'
}

querystring = parse.urlencode(params)

with request.urlopen(url + '?' + querystring) as response:
   content = response.read().decode('utf-8')

print(content)

This is the built-in urllib package.

For convenience purposes, you might want to use the requests package, not part of the Python standard library but quite popular.

Lessons in this unit:

0: Introduction
1: ▶︎ Create a network request
2: Create a Web HTTP server
3: Create a TCP server
4: Multithreading
5: Accept arguments from command line
6: GUI programming with Tkinter
7: Pygame installation
8: PEP8 style guide