Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen HTTP Basics in Python | UDP and HTTP Communication
Practice
Projects
Quizzes & Challenges
Quizze
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

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 2

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

bookHTTP Basics in Python

Swipe um das Menü anzuzeigen

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

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 2
some-alt