V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
DAOCLOUD
推荐学习书目
Python Cookbook
Using Google App Engine
推荐下载
Latest Google App Engine SDK
其他兼容技术
AppScale
supersheep

求教基础问题 关于中文传参

  •  
  •   supersheep · Jan 23, 2011 · 4558 views
    This topic created in 5576 days ago, the information mentioned may be changed or developed.
    1.当我通过需要接受get或post参数交给python脚本处理的时候。
    如果发送的是中文数据
    比如 "http://localhost:8081/myapp?key=内容"
    就会发生500错误。
      
    2.如果发送英文数据,如
    "http://localhost:8081/myapp?key=value"
    则没这个问题,可以通过以下方式输出。
      
    class myHandler(webapp.RequestHandler)
    def get(self):
       print self.request.get('key')
      
    但是输出内容为:
    value
    Status: 200 OK
    Content-Type: text/html; charset=utf-8
    Cache-Control: no-cache
    Expires: Fri, 01 Jan 1990 00:00:00 GMT
    Content-Length: 0
    这种格式。

    我不需要后面的状态信息,该怎么办呢?

    谢谢大家!
    3 replies    1970-01-01 08:00:00 +08:00
    darasion
        1
    darasion  
       Jan 23, 2011
    0、这个节点好像是Google 而不是 Google App Engine。
    1、正确的应该是 self.response.out.write(self.request.get('key').encode('utf-8'))
    2、print 输出的都是“ raw data ”[1],所以会包含传给浏览器的所有内容(包括http协议头,数据内容等;不叫状态信息)。


    [1]具体解释见:http://code.google.com/appengine/docs/python/runtime.html#Responses
    “ App Engine collects all of the data the request handler script writes to the standard output stream, then waits for the script to exit. When the script exits, all of the output data is sent to the user.”
    Hinc
        2
    Hinc  
       Jan 23, 2011
    中文,URL 之类的参数值都是要做编码处理的。
    supersheep
        3
    supersheep  
    OP
       Jan 24, 2011
    各种羞愧。谢谢ls两位。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2567 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 12:38 · PVG 20:38 · LAX 05:38 · JFK 08:38
    ♥ Do have faith in what you're doing.