0

我在 python 中运行了以下 hello world 代码,但 localhost:8080 不打印任何内容

我正在使用 ubuntu 12.04

localhost:8080 显示一个空白页面

你好世界.py

import webapp2


class MainPage(webapp2.RequestHandler):

def get(self):
    self.response.headers['Content-Type'] = 'text/plain'
    self.response.write('Hello, World!')


application = webapp2.WSGIApplication([
('/', MainPage),
], debug=True)

应用程序.yaml

application: your-app-id
version: 1
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /.*
  script: helloworld.application

输出如下

kiran@kiru-Lenovo-G480:~/google$ dev_appserver.py helloworld/
INFO     2013-10-09 12:22:03,559 sdk_update_checker.py:245] Checking for updates to the SDK.
INFO     2013-10-09 12:22:03,565 __init__.py:94] Connecting through tunnel to: appengine.google.com:443
INFO     2013-10-09 12:22:03,571 sdk_update_checker.py:261] Update check failed: <urlopen error Tunnel connection failed: 407 Proxy Authentication Required>
INFO     2013-10-09 12:22:03,595 api_server.py:138] Starting API server at: http://localhost:44748
INFO     2013-10-09 12:22:03,610 dispatcher.py:168] Starting module "default" running at: http://localhost:8080
INFO     2013-10-09 12:22:03,614 admin_server.py:117] Starting admin server at: http://localhost:8000
4

3 回答 3

0

您正在write直接调用该Response对象。你会想做这样的事情:

self.response.out.write('Hello, World!')
于 2013-10-16T01:32:08.697 回答
0

您的代码似乎没有任何问题。GAE 运行没有错误,并且在浏览器中也没有显示任何错误的事实让我认为这可能是浏览器/显示问题。试试这两件事:

  • 删除设置“Content-Type”的链接,然后
  • 尝试使用正确的 html 文件而不是文本进行响应:

    self.response.write(template.render(tvalues))

于 2013-10-16T02:42:38.193 回答
0

好吧,您实际上并不需要查看错误,因为执行很好。问题是您的代码缩进。即 import webapp2: class MainHandler(webapp2.RequestHandler): def get(self): self.response.write('Hello World') app = webapp2.WSGIApplication([('/',MainHandler)], debug=True)

应该与 class 有相同的缩进而不是 def

于 2015-09-24T10:18:11.697 回答