0

我想在 python 中构建一个简单的 http 服务器,它监听 TCP 端口并在 Web 浏览器访问时显示一些 html 页面表单(通过 HTTP GET 请求)。用户可以通过 HTTP POST 将一些数据放在表单上。

我正在使用 Eclipse 和 Pydev。我尝试使用 Web 模块

import SimpleHTTPServer
import web 

urls = (
   '/', 'Index'
)
pytest = web.application(urls, globals())

render = web.template.render('templates/')

class Index(object):
    def GET(self):
        return render.index()

    def POST(self):
        form = web.input(name="Nobody", greet="Hello")
        greeting = "%s, %s" % (form.greet, form.name)
        return render.index(greeting = greeting)

if __name__ == "__main__":
   app.run()

错误消息是 app = web.application(urls, globals()) AttributeError: 'module' object has no attribute 'application'

如果还有其他技术可以这样做,我会很高兴。

4

1 回答 1

0

我假设您使用的是http://webpy.org

我试图运行你的应用程序:

$ pip install web.py
Downloading/unpacking web.py
  Downloading web.py-0.37.tar.gz (90kB): 90kB downloaded
  Running setup.py egg_info for package web.py

Installing collected packages: web.py
  Running setup.py install for web.py

Successfully installed web.py
Cleaning up...

运行你的代码显示一个问题:

$ python test.py 
Traceback (most recent call last):
  File "test.py", line 21, in <module>
    app.run()
NameError: name 'app' is not defined

当我替换app.run()pytest.run()

if __name__ == "__main__":
    pytest.run()

一切正常:

$ python test.py 
http://0.0.0.0:8080/

如果您在 Eclipse 中运行此表单并且您使用的是 virtualenv,他们确保您将 virtualenv (bin/python) 添加到

Preferences...>PyDev->Interpreter - Python

在你的启动配置中,使用这个环境。

于 2013-07-23T16:29:05.113 回答