Python tornado.httputil.HTTPServerRequest() Examples
Last Updated :
24 Apr, 2025
Python's Tornado is a powerful web framework known for its non-blocking I/O and high performance. One crucial component of Tornado is the HTTPServerRequest class, which represents an incoming HTTP request. In this article, we'll delve into the tornado.httputil.HTTPServerRequest class and explore practical examples to better understand its usage in Python.
What is tornado.httputil.HTTPServerRequest Class?
The tornado.httputil.HTTPServerRequest class in the Tornado web framework represents an incoming HTTP request. It provides access to various aspects of the request, such as headers, query parameters, cookies, and request body. Here's a brief overview of the HTTPServerRequest class along with its commonly used methods and attributes:
Syntax:
class tornado.httputil.HTTPServerRequest(method, uri, version, headers=None, body=None, host=None, files=None, connection=None)
Parameters:
- method (str): HTTP method (e.g., "GET", "POST").
- uri (str): Request URI.
- version (str): HTTP version (e.g., "HTTP/1.1").
- headers (tornado.httputil.HTTPHeaders, optional): Request headers.
Python tornado.httputil.HTTPServerRequest() Examples
below, are the Python tornado.httputil.HTTPServerRequest() Examples in Python.
Example 1: Tornado Web Server with Query Parameters
In this example, The Tornado web server, listening on port 8888, responds to a GET request at the root ("/"). The `MainHandler` class extracts "name" and "age" query parameters from the URL, dynamically generates a greeting, and displays it on the webpage. The server is initiated using the `make_app` function, and the `IOLoop` ensures continuous listening for incoming requests.
Python3
import tornado.ioloop
import tornado.web
from tornado.httputil import HTTPServerRequest
class MainHandler(tornado.web.RequestHandler):
def get(self):
# Accessing query parameters
name = self.get_query_argument("name")
age = self.get_query_argument("age")
self.write(f"Hello, {name}! Age: {age}")
def make_app():
return tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
app = make_app()
app.listen(8888)
print("Server is running at http://localhost:8888/")
tornado.ioloop.IOLoop.current().start()