Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Making Requests with requests | UDP and HTTP Communication
Practice
Projects
Quizzes & Challenges
Quizzer
Challenges
/
Python Networking Basics

bookMaking Requests with requests

When you want to interact with web services or APIs in Python, the process typically involves sending requests over HTTP and handling the responses. APIs (Application Programming Interfaces) let you access data and functionality provided by other applications or servers—everything from weather data to stock prices to social media posts. In real-world Python projects, you often use a library like requests to do this easily. However, understanding how HTTP requests work at a lower level will give you a much deeper insight into networking and what happens under the hood. By using the socket module, you can directly send HTTP requests and receive responses, which is a valuable skill for troubleshooting, custom integrations, or when higher-level libraries are not available.

12345678910111213141516171819202122232425262728
import socket # Define the target host and port (HTTP defaults to port 80) host = "example.com" port = 80 # Create a socket object using IPv4 and TCP with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: # Connect to the server s.connect((host, port)) # Construct a simple HTTP GET request request = "GET / HTTP/1.1\r\nHost: {}\r\nConnection: close\r\n\r\n".format(host) s.sendall(request.encode()) # Receive the response data response = b"" while True: data = s.recv(4096) if not data: break response += data # Decode the bytes to a string response_text = response.decode("utf-8", errors="ignore") # Print the first 500 characters of the response print(response_text[:500])
copy

When you send an HTTP request using sockets, the server responds with a structured message. The response starts with a status line (such as HTTP/1.1 200 OK), followed by headers (like Content-Type, Content-Length, etc.), and then the response body (the actual data, such as HTML or JSON). To extract useful data from an API response, look for:

  • The status code in the first line, which tells you if the request was successful (200), redirected (3xx), had a client error (4xx), or server error (5xx);
  • Headers that describe the content and provide metadata, such as Content-Type: application/json for API responses;
  • The body of the response, which contains the data you requested—this might be HTML, plain text, or structured data like JSON.

When working with APIs, you usually care most about the body content. If the content is JSON, you can parse it with Python's built-in json module. Always check the status code and headers to make sure your request succeeded and to understand the format of the data you receive.

question mark

What does the status code 200 in an HTTP response mean?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 3

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

bookMaking Requests with requests

Stryg for at vise menuen

When you want to interact with web services or APIs in Python, the process typically involves sending requests over HTTP and handling the responses. APIs (Application Programming Interfaces) let you access data and functionality provided by other applications or servers—everything from weather data to stock prices to social media posts. In real-world Python projects, you often use a library like requests to do this easily. However, understanding how HTTP requests work at a lower level will give you a much deeper insight into networking and what happens under the hood. By using the socket module, you can directly send HTTP requests and receive responses, which is a valuable skill for troubleshooting, custom integrations, or when higher-level libraries are not available.

12345678910111213141516171819202122232425262728
import socket # Define the target host and port (HTTP defaults to port 80) host = "example.com" port = 80 # Create a socket object using IPv4 and TCP with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: # Connect to the server s.connect((host, port)) # Construct a simple HTTP GET request request = "GET / HTTP/1.1\r\nHost: {}\r\nConnection: close\r\n\r\n".format(host) s.sendall(request.encode()) # Receive the response data response = b"" while True: data = s.recv(4096) if not data: break response += data # Decode the bytes to a string response_text = response.decode("utf-8", errors="ignore") # Print the first 500 characters of the response print(response_text[:500])
copy

When you send an HTTP request using sockets, the server responds with a structured message. The response starts with a status line (such as HTTP/1.1 200 OK), followed by headers (like Content-Type, Content-Length, etc.), and then the response body (the actual data, such as HTML or JSON). To extract useful data from an API response, look for:

  • The status code in the first line, which tells you if the request was successful (200), redirected (3xx), had a client error (4xx), or server error (5xx);
  • Headers that describe the content and provide metadata, such as Content-Type: application/json for API responses;
  • The body of the response, which contains the data you requested—this might be HTML, plain text, or structured data like JSON.

When working with APIs, you usually care most about the body content. If the content is JSON, you can parse it with Python's built-in json module. Always check the status code and headers to make sure your request succeeded and to understand the format of the data you receive.

question mark

What does the status code 200 in an HTTP response mean?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 3
some-alt