Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте HTTP Basics in Python | UDP and HTTP Communication
Practice
Projects
Quizzes & Challenges
Вікторини
Challenges
/
Python Networking Basics

bookHTTP Basics in Python

Understanding how the Hypertext Transfer Protocol (HTTP) works is essential for interacting with web servers using Python. HTTP is the foundation of data communication on the World Wide Web. When you visit a website or use an API, your computer sends an HTTP request to a server, which then sends back an HTTP response. Each HTTP transaction consists of a request sent by the client (your computer) and a response returned by the server. The request specifies what resource you want, and the response contains the data or an error message, along with status information.

123456789101112131415161718192021
import socket # Connect to example.com on port 80 (HTTP) host = "example.com" port = 80 # Create a TCP socket with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.connect((host, port)) # Prepare a simple HTTP GET request request = "GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n" s.sendall(request.encode()) # Receive the response data in chunks response = b"" while True: data = s.recv(4096) if not data: break response += data # Print the HTTP response as text print(response.decode(errors="replace"))
copy

The code above demonstrates how to use Python's socket module to make a basic HTTP GET request. An HTTP request consists of a request line (GET / HTTP/1.1), followed by headers such as Host and Connection, and is terminated by an empty line (\r\n\r\n). The server responds with an HTTP response, which includes a status line (like HTTP/1.1 200 OK), response headers, a blank line, and then the response body (such as the HTML for a web page). When you send this request, the server returns the full HTTP response, which you can display or process as needed.

question mark

Which statement best describes the difference between an HTTP request and an HTTP response?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 2

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

bookHTTP Basics in Python

Свайпніть щоб показати меню

Understanding how the Hypertext Transfer Protocol (HTTP) works is essential for interacting with web servers using Python. HTTP is the foundation of data communication on the World Wide Web. When you visit a website or use an API, your computer sends an HTTP request to a server, which then sends back an HTTP response. Each HTTP transaction consists of a request sent by the client (your computer) and a response returned by the server. The request specifies what resource you want, and the response contains the data or an error message, along with status information.

123456789101112131415161718192021
import socket # Connect to example.com on port 80 (HTTP) host = "example.com" port = 80 # Create a TCP socket with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.connect((host, port)) # Prepare a simple HTTP GET request request = "GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n" s.sendall(request.encode()) # Receive the response data in chunks response = b"" while True: data = s.recv(4096) if not data: break response += data # Print the HTTP response as text print(response.decode(errors="replace"))
copy

The code above demonstrates how to use Python's socket module to make a basic HTTP GET request. An HTTP request consists of a request line (GET / HTTP/1.1), followed by headers such as Host and Connection, and is terminated by an empty line (\r\n\r\n). The server responds with an HTTP response, which includes a status line (like HTTP/1.1 200 OK), response headers, a blank line, and then the response body (such as the HTML for a web page). When you send this request, the server returns the full HTTP response, which you can display or process as needed.

question mark

Which statement best describes the difference between an HTTP request and an HTTP response?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 2
some-alt