[Tutor] How to get url call back from the browser

Danny Yoo dyoo at hashcollision.org
Wed Mar 1 20:20:10 EST 2017


Hi Palanikumar,

It looks like you're using Python 2, since you're referring to
SimpleHTTPServer.  But you probably do not want to use
SimpleHTTPRequestHandler: it's a local file system server.  The docs
at https://docs.python.org/2/library/simplehttpserver.html#SimpleHTTPServer.SimpleHTTPRequestHandler
say: "This class serves files from the current directory and below,
directly mapping the directory structure to HTTP requests.", and that
probably isn't what you want.

Instead, you can define your own handler whose logic you can control.
You can subclass BaseHTTPRequestHandler, and implement do_GET() .
Here is a minimal example:

###################################################
import BaseHTTPServer

class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header("Content-type", "text/plain")
        self.end_headers()
        self.wfile.write("Hello world! I see path is: %r" % self.path)

if __name__ == '__main__':
    httpd = BaseHTTPServer.HTTPServer(("", 8080), MyHandler)
    httpd.serve_forever()
###################################################

Within the handler, you can look at attributes like "path"
(https://docs.python.org/2.7/library/basehttpserver.html#BaseHTTPServer.BaseHTTPRequestHandler.path),
which


You can find more examples at:

    https://wiki.python.org/moin/BaseHttpServer


If you have questions, please feel free to ask!


More information about the Tutor mailing list