-->

Apache下webapp2的(=没有谷歌的App Engine)(webapp2 under Ap

2019-07-30 10:16发布

我想对Python下与Apache和mod_wsgi的运行webapp2的 - 具体是:Wampserver与Apache 2.2.22为Windows 7。 到目前为止,我非常失败。 :-(

我用从以下示例https://developers.google.com/appengine/docs/python/gettingstartedpython27/usingwebapp :

import webapp2

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

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

当我将此文件保存为c:wamp\www\Python\hello.py ,并浏览到localhost/Python/hello.py我得到:

Not Found
The requested URL /python/hello.py was not found on this server.

然而,我要指出的mod_wsgi的Python中的Apache似乎罚款运行; 下面的代码

def application(environ, start_response):
    status = '200 OK'
    output = 'Hello from Python!'

    response_headers = [('Content-type', 'text/plain'), 
        ('Content-Length', str(len(output)))]

    start_response(status, response_headers)
    return [output]

位于c:\wamp\www\Python\test.py 。 当我去到localhost/Python/test.py ,浏览器说, Hello from Python! 如我期望的那样。

到目前为止,我只发现了如何通过将线改变的DEF的默认名称(=“应用程序”),以“something_else”

WSGICallableObject something_else

.htaccess

但是,我怎么能得到阿帕奇接受变量app作为一个可调用对象? (到目前为止,我已经使用的Python主要用于网络之外的节目,所以我希望这不是一个愚蠢的问题。)

任何帮助表示赞赏。

更新:

格雷厄姆问我。我正在使用Apache的配置文件的mod_wsgi的配置和在那里我加入吧。 我加

LoadModule wsgi_module modules/mod_wsgi.so

<Directory "c:/wamp/www/python">
Options +ExecCGI
AddHandler wsgi-script .py
Order allow,deny
Allow from all
</Directory>

httpd.conf就在所有“的LoadModule”行的末尾。

在我的配置一些额外的信息:我使用mod_wsgi-win32-ap22py27-3.3.so。 (当然,我重新命名为mod_wsgi.so并放置到c:\wamp\bin\apache\apache2.2.22\modules 。)我的Python的命令行表示,这对版本: Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win 32 。 我使用WAMP的服务器是32位。 我的操作系统是Windows 7旗舰版64位SP1。

希望这有助于诊断...

Answer 1:

从安装mod_wsgi的http://code.google.com/p/modwsgi/wiki/InstallationOnWindows并正确配置你的httpd.conf。

我假定你已经添加了这两条线:

LoadModule wsgi_module modules/mod_wsgi.so
WSGICallableObject app

安装PY-setuptools的从http://pypi.python.org/pypi/setuptools然后你的Python安装模块

easy_install WebOb
easy_install Paste
easy_install webapp2

创建虚拟主机

<VirtualHost *>
  ServerAdmin admin@mydomain.com
  DocumentRoot "/vhost/domains/mydomain/htdocs"
  ServerName a.mydomain.net
  WSGIScriptAlias / "/vhost/domains/mydomain/wsgi/main.py"
  Alias /static/ "/vhost/domains/mydomain/htdocs/static/"
</VirtualHost>

文件:main.py

import webapp2

class Hello(webapp2.RequestHandler):
  def get(self):
    self.response.headers['Content-Type'] = 'text/html; charset=utf-8'
    self.response.out.write('hello world!')

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


Answer 2:

1)您需要安装webapp2的,的WebOb,粘贴先决条件在使用PIP或easy_install的托管环境模块

2)根据网站的根文件夹(/var/www/website/wsgi.py)创建wsgi.py文件。

#/var/www/website/wsgi.py
import webapp2
class Index(webapp2.RequestHandler):
    def get(self):
        output = 'webapp2 running on apache2'
        self.response.headers = [('Content-type','text/plain'),('Content-length',str(len(output)))]
        self.response.out.write(output)

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

3)下创建的网站,提供文件夹的Apache2配置文件(/etc/apache2/sites-available/website.conf)

<VirtualHost *:80>
    ServerName website
    WSGIScriptAlias / "/var/www/ website /wsgi.py"
</VirtualHost>

4)添加“网站”别名“的/ etc / hosts”文件。

5)下列命令运行以使能“/etc/apache2/sites-available/website.conf”

a2ensite website.conf

6)刷新和重新启动的Apache2 web服务器

service apache2 reload 
/etc/init.d/apache2 restart

7)Apache网络服务器会自动加载在重新启动webapp2.WSGIApplication实例“网站”的配置将指向mod_wsgi的“应用程序”。

请注意,上面的例子是一个Ubuntu 13.10操作系统上测试。



Answer 3:

你还没有尝试:

 WSGICallableObject app

你也可以改变你的代码的说:

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

并避免需要告诉mod_wsgi的去寻找不同的名称。



Answer 4:

我还没有尝试过自己,只是还没有,但你创造了另一个Python模块,说runme.py,用下面的代码:

def main():
  run_wsgi_app(yourmodule.app)
if __name__ == '__main__':
  main()

(注:我得到这个从https://developers.google.com/appengine/docs/python/python27/migrate27#wsgi



Answer 5:

得到它了! 该生产线

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

应该:

app = webapp2.WSGIApplication([('/Python/hello.py', MainPage)], debug=True)

然后一切正常! Arghh!

非常感谢格雷厄姆耐心的把我推在正确的方向:这个问题确实是webapp2的的范围内,只要WSGICallableObject被设置为“应用程序”!

对于任何人的利益被卡在一个类似的路由问题而webapp2:退房http://webapp-improved.appspot.com/guide/routing.html 。 在“简单的路线”第一个例子让我重写我的呼吁webapp.WSGIApplication分钟之内!

更新

不幸的是,上述方案似乎并不可靠:今天,我有时会接到webapp2的正确反应,有时我从webapp2的404。

在不改变的一行代码从昨天起。

我不能在什么条件我得到404或正确的响应重现。 我对这个放弃现在。 这是可悲的,因为我认为Python是这样一个很酷的语言。

@Graham:再次感谢您的帮助。



文章来源: webapp2 under Apache (= without Google App Engine)