Drag or tap steps into the slots.
PCAP Modules and Packages Practice Question
Drag and drop the steps to create a simple HTTP server using the http.server module in Python into the correct order.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
Import http.server, define handler class, create HTTPServer instance, call serve_forever()
Creating an HTTP server involves importing http.server, defining a handler, overriding methods, creating an HTTPServer instance, and calling serve_forever().
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
Import http.server, define handler class, create HTTPServer instance, call serve_forever()
Why this is correct
Correct order: First import the module, then define a handler class that inherits from BaseHTTPRequestHandler, then create an instance of HTTPServer with the server address and handler class, and finally call serve_forever() to start the server.
- ✗
Define handler class, import http.server, create HTTPServer instance, call serve_forever()
Why it's wrong here
Defining the handler class before importing http.server fails because Python resolves base classes at class definition time. Since http.server is not yet in the module's namespace, the expression http.server.BaseHTTPRequestHandler raises NameError. The import statement must execute first to make the module's attributes available. Thus, starting with the class definition is invalid.
- ✗
Import http.server, create HTTPServer instance, define handler class, call serve_forever()
Why it's wrong here
Creating the HTTPServer instance before defining the handler class is invalid because the constructor's second argument must be the actual handler class (e.g., a subclass of BaseHTTPRequestHandler). At that point, the name for the handler class is either undefined (NameError) or, if a placeholder was used, the server would be bound to the wrong class and fail on every request. The handler class must be fully defined first so it can be passed as a reference and stored by the server for later per-request instantiation.
- ✗
Import http.server, define handler class, call serve_forever(), create HTTPServer instance
Why it's wrong here
Calling serve_forever() before constructing the HTTPServer object is a logical and runtime error: a method call requires an existing instance. Since no instance has been created, the call either refers to an unbound name (NameError) or, in some contexts, an attribute lookup on None (AttributeError). The server's main loop must be started on an already-bound and configured server object, so instantiation must precede the serve_forever() call.
Go deeper
Related to this question
About these practice questions
One of 421 original PCAP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This PCAP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCAP exam.