0

这是我编写的 Cherrypy 中的服务器,我想添加查询字符串。

但是当我使用重定向站点时,它似乎不适用于localhost:8080/index?foo=1&foo=2 为什么?

我的项目

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())

但我想在网站启动时在 Url 上添加变量

4

1 回答 1

1

根据文档,您不能指定主机,并且您需要有一个处理查询参数的视图,因此请执行以下操作:

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
于 2013-04-06T22:07:05.867 回答