Results 1 to 8 of 8

Thread: Http edirects in python

  1. #1
    Join Date
    Apr 2009
    Location
    Ukraine, Kherson
    Beans
    62

    Http edirects in python

    How may i redirect user's webrowser to some url if non-existent file requested ? For example i want all 404 errors(not found) redirect to index.html
    I am using basic methods to run webserver, as you see do_HEAD method of CGIHTTPRequestHandler already defined, but there is no 404 redirects.
    Code:
    #!/usr/bin/env python3
    
    import http.server
    from http.server import CGIHTTPRequestHandler
    
    port = 80
    addr = '192.168.150.1'
    addrport = (addr, port)
    
    class myHandler(CGIHTTPRequestHandler):
        def do_HEAD(s):
            if not self.path=="/":
                f = open(curdir + sep + self.path) 
                self.send_response(200)
                self.send_header('Content-type','text/html')
                self.end_headers()
                self.wfile.write(f.read())
                f.close()
            return
    try:
        serv = http.server.HTTPServer(
            addrport,
            myHandler
        )
        serv.serve_forever()
    except KeyboardInterrupt:
        print ('Shutting down the web server')
        serv.socket.close()
    EOF

  2. #2
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,160
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: Http edirects in python

    Send it a redirection status code with the new URL (302, usually).
    Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.

  3. #3
    Join Date
    Jul 2013
    Location
    Wisconsin
    Beans
    2,471

    Re: Http edirects in python

    ofnuts is right.

    Error and redirection codes exist for a reason. Use them, and let the client decide what they want to do about it.
    If a client keep trying to access foo.html, but keeps getting index.html instead, they may assume your server is broken.

  4. #4
    Join Date
    Apr 2009
    Location
    Ukraine, Kherson
    Beans
    62

    Re: Http edirects in python

    ok, and how practically to code this redirection in python ? If i use apache2 as webserver i simply put in .htaccess
    ErrorDocument 404 /index.php

  5. #5
    Join Date
    Jul 2013
    Location
    Wisconsin
    Beans
    2,471

    Re: Http edirects in python

    Pretty much the same way you coded the 200 success within your MyHandler class.

  6. #6
    Join Date
    Apr 2009
    Location
    Ukraine, Kherson
    Beans
    62

    Re: Http edirects in python

    I was unable to get it working, could you show any sample code example, please ?

  7. #7
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,160
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: Http edirects in python

    What have you done/tried so far?
    Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.

  8. #8
    Join Date
    Apr 2009
    Location
    Ukraine, Kherson
    Beans
    62

    Re: Http redirects in python

    Tried below code
    Code:
    #!/usr/bin/env python3
    
    import http.server
    from http.server import CGIHTTPRequestHandler
    
    port = 80
    addr = '192.168.150.1'
    addrport = (addr, port)
    
    class myHandler(CGIHTTPRequestHandler):
        def do_GET(self):
            if not self.path == '/':
                self.send_response(302)
                self.send_header('Location','/index.html')
                self.end_headers()
    
    try:
        serv = http.server.HTTPServer(
            addrport,
            myHandler
        )
        serv.serve_forever()
    except KeyboardInterrupt:
        print ('Shutting down the web server')
        serv.socket.close()
    When client requesting not root path / it gets redirected to 192.168.150.1/index.html, but index.html not shown, browser reporting "Firefox has detected that the server is redirecting the request for this address in a way that will never complete."
    If / requested index.html not shown as well. If i use http.server without my own class CGIHTTPRequestHandler index.html working. Code as below
    Code:
    #!/usr/bin/env python3
    
    import http.server
    
    port = 80
    addr = '192.168.150.1'
    addrport = (addr, port)
    
    try:
        serv = http.server.HTTPServer(
            addrport,
            http.server.CGIHTTPRequestHandler
        )
        serv.serve_forever()
    except KeyboardInterrupt:
        print ('Shutting down the web server')
        serv.socket.close()

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •