【问题标题】:How to I start the site with QueryString?如何使用 QueryString 启动网站?
【发布时间】:2013-02-18 23:09:27
【问题描述】:

我在 Cherrypy 中使用 Mako 编写了服务器。

我有一个变量(json_data,其中是文件 [json] 的内容),其中包含在我的网站上工作的数据。

要更改这些参数,我必须使用查询字符串

为了简化我的数据管理工作。

当我的服务器 url 打开时,我需要在我的服务器 url 上有我的变量 (json_data)。

(localhost:8100?=json_data?json_data=demo_title%24+Demo+title+%23+) 我知道将分隔符的 python 替换为 javascript。

我希望在启动网站时在 url 上添加变量 json_data,但如何?

信息:

Json_data=

demo_title%24+Demo+title+%23+proc1_script%24+script.sh+parameters+%23+proc1_chk_make%24+on+%23+outputp2_value%24++%23+demo_input_description%24+hola+mundo+%23+outputp4_visible%24+on+%23+outputp4_info%24++%23+inputdata1_max_pixels%24+1024000+%23+tag%24++%23+outputp1_id%24+nanana+%23+proc1_src_compresion%24+zip+%23+proc1_chk_cmake%24+off+%23+outputp3_description%24++%23+outputp3_value%24++%23+inputdata1_description%24+input+data+description+%23+inputp2_description%24+bien%3F+%23+inputp3_description%24+funciona+%23+proc1_cmake%24+D+CMAKE_BUILD_TYPE%3Astring%3DRelease++%23+outputp2_visible%24+on+%23+outputp3_visible%24+on+%23+outputp1_type%24+header+%23+inputp1_type%24+text+%23+demo_params_description%24+va+bien+%23+outputp1_description%24++%23+inputdata1_type%24+image2d

用程序更新

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?" + params)


cherrypy.config.update({

        'server.socketPort': 8080

})
cherrypy.quickstart(Root())

【问题讨论】:

标签: python python-3.x cherrypy mako


【解决方案1】:

我不得不问,你为什么需要这样做?这些变量不能作为处理请求的方法的默认值吗?

【讨论】:

  • 是和不是。这不是对他问题的直接回答,但如果他的方法不需要像他描述的那样,这是一个选择。
  • raise cherrypy.HTTPRedirect("localhost:8080?" + params) 有了这个,我做了一个重定向,我有。
  • 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("localhost:8080?%s" % 参数) #urlVar = 1 #urlVar2 = 2 #requests.get("localhost:8080/?", params = p) raise cherrypy.HTTPRedirect("localhost:8080?" + params) cherrypy.config.update({ 'server.socketPort': 8080 }) cherrypy.quickstart(Root())
  • urllib.urlopen("localhost:8080?%s"; % params You have ; before % params。应该在那里。这是防止参数被添加到你的字符串中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-30
  • 2014-10-28
  • 1970-01-01
  • 1970-01-01
  • 2017-12-13
  • 2014-03-17
相关资源
最近更新 更多