Search code examples
python-2.7cherrypy

Server in Python: write or add parameters on URL possible without redirect


I need the command for working the Url using CherryPy and Python version 2.7.3

I want to change the url.

Old Url is

localhost:8080

and automatically adding foo=1&foo=2 (variable s) in the Url

localhost:8080?=foo=1&foo=2


import cherrypy
import urllib

class Root(object):
    @cherrypy.expose
    def index(self):
        jsondict = [('foo', '1'), ('foo', '2')]
        s = urllib.urlencode(jsondict)
        print s
        #foo=1&foo=2

        return "Hello"


cherrypy.config.update({
    'global': {
        'server.socketPort': 8080
    }
})
cherrypy.quickstart(Root())

There is a solution?

Using the suggestion's Andrew with raise cherrypy.HTTPRedirect("localhost:8080?" + params).

This is working but this is for redirect.

This I am not interested but write on new url using the redirect.

I don't want the redirect but I working always on this url, adding the Query String without redirect.

It is possible?


Solution

  • What about just a simple redirect?

    import cherrypy
    
    class Root(object):
        @cherrypy.expose
        def index(self):
            urlVar = 1
            urlVar2 = 2
            raise cherrypy.HTTPRedirect("localhost:8080?foo=" + str(urlVar) + "&fooo=" + str(urlVar2))
    
    cherrypy.config.update({
        'global': {
            'server.socketPort': 8080
        }
    })
    cherrypy.quickstart(Root())