この実験ベースのチュートリアルでは、Pythonの最も人気のあるリクエスト関連パッケージを5つご紹介します。各パッケージの簡単なGETリクエストを作成する方法と簡単なコードスニペットを例示します。NASAのAstronomy Photo of the Day API(以降「APOD」と短縮)を使って、Webブラウザで宇宙の写真を表示させます。
For the second year in a row, my family decided to embrace the “secret santa” method of giving gifts at Christmas. Rather than everyone getting a gift for everyone else, which can be expensive and time consuming, each person’s name is put in a box and we each draw one name from the box. The name we draw is the person we give a gift to, up to a certain dollar amount that we’ve all agreed on in advance.
This year, my family is spread all over the United States so it wasn’t possible to draw names from a box. Instead, I thought it would be fun to create my own Flask application that would match people up and notify each person of their gift recipient using Twilio SMS.
Follow along with the tutorial below, or clone the GitHub repository if you’re in a rush!
- Python 3.6+
When it comes to software development, there are almost always several different ways to achieve the same outcome. This is true when evaluating a family of third-party software packages, too. For example, in the Python ecosystem there are thousands of packages related to making HTTP requests. Which one should you use?
In this experiment-based tutorial, we’ll walk through brief code snippets that show how to make a simple GET request using 5 of Python’s most popular requests-related packages. We’ll use NASA’s Astronomy Photo of the Day API (shortened to APOD throughout the rest of the tutorial) and open today’s photo in our web browser.
Our goal is to make simple GET requests quickly using a variety of Python packages, rather than to compare and contrast all of the features and subtleties of each package. If asynchronous requests are a better fit for your use case, check out the companion …
Django comes with a lightweight server that allows you to serve your project on your local machine at http://localhost:8000. This is a great way to test and troubleshoot your application code.
Your local server isn't exposed to the real Internet, though, so your Django project can't receive requests from an external source, such as a Twilio webhook.
For development purposes, you may need to test that your app code properly handles requests from external sources. To do that, you must expose your app to the Internet.
What is ngrok?
The ngrok utility generates public URLs that map to your local server, enabling you to test how your development code handles requests from external sources. Read more about ngrok and how it works in the official documentation.
All you need for this tutorial is an existing Django project running on Python 3.
Ask just about any programmer why they like to write code, and I’ll bet you a giant slice of chocolate cake that every single person will give at least one of these answers:
- “I love making things!”
- “I love learning how things work!”
- “I love solving problems!”
Of course, this isn’t an exhaustive list of the reasons why people like to write code, but the exclamations in this list have reliably and recurrently come up when I get into conversations with fellow developers. Often, these developers will tell me that not only do they love making things, love learning how things work, or love solving problems, they have actually always enjoyed these things, even in their childhood.
This unique collection of interests — building, learning, solving — is the longstanding core of my identity.
How it started
In elementary school, Mrs. Elliott was my daycare provider and watched over me …