Redirection using WSGIref

Active3 hr before
Viewed126 times

8 Answers


Prevent double encoding of redirect URL when merge_slash=True for Rule,match

Example_snippet/controller/utility/_redirection.js/ Connection. . .
load more v

The first thing we do is redirect the user to the edit screen if the page doesn’t exist, exc

Example_snippet/controller/utility/_redirection.js/ class WikiApp(object): de. . .
class WikiApp(object):

   def __init__(self, storage_dir):
   self.storage_dir = os.path.abspath(os.path.normpath(storage_dir))
load more v

The Web Server Gateway Interface (WSGI) is a standard interface between web server software and web applications written in Python, Having a standard interface makes it easy to use an application that supports WSGI with a number of different web servers

Example_snippet/controller/utility/_redirection.js/ from wsgiref.util import setup. . .
from wsgiref.util
import setup_testing_defaults
from wsgiref.simple_server
import make_server

# A relatively simple WSGI application.It 's going to print out the
# environment dictionary after being updated by setup_testing_defaults
def simple_app(environ, start_response):

status = '200 OK'
headers = [('Content-type', 'text/plain; charset=utf-8')]

start_response(status, headers)

ret = [("%s: %s\n" % (key, value)).encode("utf-8")
   for key, value in environ.items()
return ret

with make_server('', 8000, simple_app) as httpd:
   print("Serving on port 8000...")
load more v

This is why WebOb will automatically use the information from the WSGI environment to return an absolute URI, The newer RFC's (specifically RFC7231 Section 7

Example_snippet/controller/utility/_redirection.js/ @staticmethod def _mak. . .
    def _make_location_absolute(environ, value):
       return value

    new_location = urlparse.urljoin(_request_uri(environ), value)
    return new_location

    def _abs_headerlist(self, environ):
       # Build the headerlist,
       if we have a Location header, make it absolute
    return [
          (k, v) if k.lower() != 'location'
          else(k, self._make_location_absolute(environ, v))
          for (k, v)
             in self._headerlist

       def __call__(self, environ, start_response):
    WSGI application interface
    if self.conditional_response:
       return self.conditional_response_app(environ, start_response)

    headerlist = self._abs_headerlist(environ)
load more v

If you are using wsgiref just for testing, then it would make even more sense to not auto-redirect requests, so you notice errors in your application that would later expose users to unencrypted HTTP,,Hi guys I have an app which is running on Https just fine but I need to redirect all my Http traffic to Https version

Example_snippet/controller/utility/_redirection.js/ from bottle import run, route,. . .
from bottle import run, route, template, static_file, error, ServerAdapter
# ======== Start ======== Pages ========
def index():
return template('./home/index')
# ======== End ======== pages ========

# ======== Start ======== Static files route ========
   def img_static(filename):
   return static_file(filename, root='./static/img')
      def css_static(filename):
      return static_file(filename, root='./static/css')
      @route('/static/js/<filename:path>' )
         def js_static(filename):
         return static_file(filename, root='./static/js')
         # ======== End ======== Static files route ========

         # ======== Start ======== Custom error pages ========
         def error404(error):
         return template('./home/404')
         # ======== End ======== Custom error pages ========

         # ======== Start ======== SSL ========
         class SSLWSGIRefServer(ServerAdapter):
         def run(self, handler):
         from wsgiref.simple_server import make_server, WSGIRequestHandler
         import ssl
         if self.quiet:
         class QuietHandler(WSGIRequestHandler):
         def log_request(*args, **kw): pass
         self.options['handler_class'] = QuietHandler
         srv = make_server(, self.port, handler, **self.options)
         srv.socket = ssl.wrap_socket (
         srv = SSLWSGIRefServer(host="", port=8090)
         # ======== End ======== SSL ========
         if __name__ == '__main__':
load more v

I am trying to develop a small web application using python's WSGI, For example, if a user chooses Google they would be redirected to google

Example_snippet/controller/utility/_redirection.js/ from wsgiref.simple_server imp. . .
from wsgiref.simple_server import make_server
from cgi import parse_qs, escape

main_html = """

   <title> Welcome to redirection test page </title>

   <form method="get" action='/visit'>
      <input type=radio name='site' value=google> Google
      <input type=radio name='site' value=facebook> Facebook
      <input type=submit value=submit>


def main(environ, start_response):

response_body = main_html
print type(response_body)
status = '200 OK'

response_headers = [
('Content-Length', str(len(response_body)))

start_response(status, response_headers)
return [response_body]

def visit(environ, start_response):
qs = parse_qs(environ['QUERY_STRING'])
dest ='site')[0]
if dest == 'google':
start_response('301 Moved Permanently', [('Location','')])
start_response('301 Moved Permanently', [('Location','')])

return [1]

def app(environ, start_response):
if environ['PATH_INFO'] == '/':
return main(environ, start_response)
elif environ['PATH_INFO'] == '/visit':
return visit(environ, start_response)

httpd = make_server('',8052, app)
print 'Serving on port 8052'
Step 2 continued with Traceback (most recent call la. . .
Traceback(most recent call last):
   File "/usr/lib/python2.7/wsgiref/", line 86, in run
File "/usr/lib/python2.7/wsgiref/", line 131, in finish_response
File "/usr/lib/python2.7/wsgiref/", line 33, in close
self.status.split(' ', 1)[0], self.bytes_sent
AttributeError: 'NoneType'
object has no attribute 'split'
load more v

What is the need for a "related work" section in state-of-the-art papers? ,Thanks for contributing an answer to Webmasters Stack Exchange!, Webmasters help chat , Webmasters Meta

Example_snippet/controller/utility/_wsgiref.js/ RewriteEngine On RewriteRule . . .
RewriteEngine On
RewriteRule ^ /api$   /metadata [PT]

It seems that you are not changing the HTTP status code from 200 to 302,,To do a HTTP redirection, you need both:,'302 Found' HTTP status code,'Location' HTTP header

Example_snippet/controller/utility/_wsgiref.js/ def _redirect_to(_self, _url):. . .
def _redirect_to(_self, _url):
"Takes an url as argument and returns a location header"

return ([('Location', _url)], "") #-- - (i)
load more v