python - Converting htaccess setting to BaseHTTPServer setting -
so i'm trying allow users embed image on site, , image automatically replaced iframe button inside. reason make easy users avoiding using js or other code on networks they're not allowed.
this possible in apache php, way in python not particularly obvious me.
original htaccess: redirect /embed.jpg http://somesite.com/embed.php
but in python, doesn't work (this example doesn't work).
server.py:
#!/usr/bin/env python import basehttpserver import getopt import socket import sys addr = ["", 8080] def usage(f = sys.stdout): print >> f, "usage: %s [address [port]]" % sys.argv[0] class handler(basehttpserver.basehttprequesthandler): def do_get(self): if self.path == "/": self.send_response(200) self.send_header("content-type", "text/html; charset=utf-8") self.end_headers() print >> self.wfile, """\ <!doctype html> <body bgcolor=dodgerblue> <img src="x.png" width=200 height=200> </body> """ return if self.path == "/x.png": self.send_response(302) self.send_header("content-type", "image/png") self.send_header("location", "x.html") self.end_headers() return if self.path == "/x.html": self.send_response(200) self.send_header("content-type", "text/html; charset=utf-8") self.end_headers() print >> self.wfile, """\ <!doctype html> <body bgcolor=slateblue> inner html. </body> """ self.send_response(404) self.send_header("content-type", "text/html; charset=utf-8") self.end_headers() print >> self.wfile, """\ <!doctype html> not found. """ class server(basehttpserver.httpserver): allow_reuse_address = true opts, args = getopt.gnu_getopt(sys.argv[1:], "h", ["help"]) o, in opts: if o == "-h" or o == "--help": usage() sys.exit(0) if len(args) >= 1: addr[0] = args[0] if len(args) >= 2: addr[1] = args[1] if len(args) >= 3: usage(sys.stderr) os.exit(1) server = server(tuple(addr), handler) print "listening on %s:%d." % (server.server_name, server.server_port) server.serve_forever()
two problems can see code...
first, <img src="x.png" width=200 height=200>
implies browser should expect image data back, instead redirect them url returns html data.
second, think you're missing return
in if
-block directly before line...
self.send_response(404)
i don't think it's possible replace <img>
tag <iframe>
tag without modifying dom in javascript.
Comments
Post a Comment