Search code examples
pythonurlpython-2.7cherrypy

I add the query string on Url but the Url not exchange


This is a server in Cherrypy that I programmed and I want add the query string.

But when I use the redirect the site and it appears that does not work with the localhost:8080/index?foo=1&foo=2 Why?

My project

import cherrypy
import urllib
#import requests

class Root(object):
    @cherrypy.expose
    def index(self):
        jsondict = [('foo', '1'), ('foo', '2')]
        p = urllib.urlencode(jsondict)
        #url = urllib.urlopen("http://localhost:8080?%s" % params)
        #urlVar = 1
        #urlVar2 = 2
        #requests.get("localhost:8080/?", params =p)

        raise cherrypy.HTTPRedirect("localhost:8080/index?" + p)

cherrypy.config.update({

        'server.socketPort': 8080

})
cherrypy.quickstart(Root())

But I want add the variable on the Url while the site's starting


Solution

  • According to docs you must not specify host and also you need to have a view that handles your query parameters, so do something like this:

    class Root(object):
        @cherrypy.expose
        def index(self, foo=None):
            if not foo:
                jsondict = [('foo', '1'), ('foo', '2')]
                p = urllib.urlencode(jsondict)
                #url = urllib.urlopen("http://localhost:8080?%s" % params)
                #urlVar = 1
                #urlVar2 = 2
                #requests.get("localhost:8080/?", params =p)
    
                raise cherrypy.HTTPRedirect("/index?" + p)
    
            return foo