Python 谷歌应用程序引擎:响应“;内容长度“;标题始终为0
我学习了GoogleAppEngine教程,在guestbook类中向响应对象添加内容时遇到了一些问题Python 谷歌应用程序引擎:响应“;内容长度“;标题始终为0,python,google-app-engine,response,webapp2,http-content-length,Python,Google App Engine,Response,Webapp2,Http Content Length,我学习了GoogleAppEngine教程,在guestbook类中向响应对象添加内容时遇到了一些问题 class Guestbook(webapp2.RequestHandler): def post(self): # We set the same parent key on the 'Greeting' to ensure each greeting # is in the same entity group. Queries across the single ent
class Guestbook(webapp2.RequestHandler):
def post(self):
# We set the same parent key on the 'Greeting' to ensure each greeting
# is in the same entity group. Queries across the single entity group
# will be consistent. However, the write rate to a single entity group
# should be limited to ~1/second.
guestbook_name = self.request.get('guestbook_name',
DEFAULT_GUESTBOOK_NAME)
testvar = self.request.get('testvar',
DEFAULT_GUESTBOOK_NAME)
greeting = Greeting(parent=guestbook_key(guestbook_name))
if users.get_current_user():
greeting.author = users.get_current_user()
greeting.content = self.request.get('content')
greeting.info = 'DIDTHISWORK?'
greeting.put()
self.response.headers.add_header("Expires", 'Information here')
#self.response.set_status(200,'Is this working?!')
self.response.headers['Content-Type'] = 'text/plain'
#self.response.headers['Content-Length'] = '5'
self.response.out.write('Hello')
query_params = {'guestbook_name': guestbook_name}
self.redirect('/?' + urllib.urlencode(query_params))
print type(self.response)
以下是使用Wireshark的响应数据包:
HTTP/1.1 302 Found
Cache-Control: no-cache
Expires: Information here
Content-Type: text/plain
Location: http://_______.appspot.com/?guestbook_name=default_guestbook
Date: Fri, 17 May 2013 01:21:52 GMT
Server: Google Frontend
Content-Length: 0
正如你所看到的,我试图用“Hello”填充内容体,但它一直给我content length=0,手动设置似乎没有帮助,所以我把它注释掉了。我认为您可以安全地忽略带有问候语的代码,但我在其中添加了它,以防它影响我所做的任何事情。您在将任何内容打印到页面之前发送重定向,因此长度为0
self.redirect('/?' + urllib.urlencode(query_params)) # redirects
print type(self.response) # never executes
如果查看Wireshark捕获,请查看响应代码(302重定向)和位置:
标题(告诉浏览器重定向到哪里)
HTTP/1.1 302 Found
Location: http://_______.appspot.com/?guestbook_name=default_guestbook