views:

41

answers:

1

I have a program (simple web server) which I try to understand. There is a class called MyHandler. In this class we define 2 methods do_GET and do_POST.

I do not understand several things:

  1. Where do we use the two above defined methods? I would expect to see something like that objectname.do_GET() and objectname.do_POST() but I do not see such things.

  2. At which point we instantiate the class? Do we use the whole class (not an instance of the class) as an argument to the HTTPServer?

  3. Why do we specify argument in the definition of the class (BaseHTTPRequestHandler) and then do not use it?

+4  A: 

I'm quite new to Python, but I will have a go at an answer—it might help me learn too!

  1. We don't ever call the do_GET() and do_POST() methods from our code, this is done automatically by the HTTPServer class instance when GET and POST requests are made (see point 2).

  2. The HTTPServer will create an instance of the MyHandler class for each HTTP request that is made.

  3. BaseHTTPRequestHandler is not an argument: it specifies that our MyHandler class subclasses BaseHTTPRequestHandler.

Hope this helps!

Mark B