DEV Community

Godadevi
Godadevi

Posted on

Python program to communicate to an API

Python program acts as the client.It sends an HTTP(Hyper Text Transfer Protocol)get request to the API server and then the server processes the request and it reads the data,searches its database,prepares the response.The server sends data back in JSON format and then it becomes python data.The below is an example for it:
Example code:
import requests
url = "https://api.chucknorris.io/jokes/random"
response = requests.get(url)
print("Status Code:", response.status_code)a
print("Raw Data:", response.text)
data = response.json()
print("\nChuck Norris Joke:")
print(data["value"])

simple data transfer diagram
python program
|
API server
|
python program
|
output
Exploring python frameworks like Django and fastapi:
DGANGO
Django is a high-level Python web framework used to build secure and scalable web applications quickly.
It follows the MVT architecture (Model–View–Template).
It provides many built-in features like:
Authentication
Admin panel
Database management
Security protection
FastAPI
FastAPI is a modern Python framework used to build APIs quickly and efficiently.
FastAPI is mainly used for building REST APIs.
It is very fast and high performance.
It supports automatic API documentation.
It uses Python type hints.

Top comments (0)