V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
DAOCLOUD
推荐学习书目
Python Cookbook
Using Google App Engine
推荐下载
Latest Google App Engine SDK
其他兼容技术
AppScale
supersheep
V2EX  ›  Google App Engine

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

  •  
  •   supersheep · 2011-01-23 22:08:54 +08:00 · 3874 次点击
    这是一个创建于 4857 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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 条回复    1970-01-01 08:00:00 +08:00
    darasion
        1
    darasion  
       2011-01-23 22:37:04 +08:00
    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  
       2011-01-23 22:38:59 +08:00
    中文,URL 之类的参数值都是要做编码处理的。
    supersheep
        3
    supersheep  
    OP
       2011-01-24 13:37:40 +08:00
    各种羞愧。谢谢ls两位。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2682 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 04:31 · PVG 12:31 · LAX 21:31 · JFK 00:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.