Start learning free

LESSON 1 OF 188 · BACKEND: HOW THE WEB WORKS

Your First Request in Python

Practice

Every time a browser opens a page, it sends a request to a server, a program waiting on some computer, and the server sends back a response. A Python script can do the same with the requests library.

requests.get(address) sends the request and returns the response. A response starts with a status code, a number that says how it went (200 means OK), and then comes the body, the content itself. .status_code gives the number, and .text gives the body as a string.

page = requests.get("https://example.com/about")
print(page.status_code)
print(page.text)

The shop's supplier runs a server at the address in SUPPLIER. Its home page is the path /. When your script asks for it, it must print:

Status: 200
Text: Supplier API. Try /prices

Your task: send a GET request to the supplier's home page, keep the answer in response, then print its status code and its text, each after its label.


Assignment
Follow the Coding Tutorial to send your first request and print the status code and the text that come back.


Hint
The address is a string: join SUPPLIER and the path with +. print can take a label and a value, separated by a comma.


STEP 1 OF 3
    Ctrl + Enter to check

    Web First Request in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free