Google App Engine是否实际执行“正常” CGI? [英] Does Google App Engine actually implement "normal" CGI?

查看:108
本文介绍了Google App Engine是否实际执行“正常” CGI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

主要文档页声称它确实:Python 2.7运行时支持WSGI标准和向后兼容的CGI标准。



这个问题必然是一个非常明显的问题,但我无法弄清楚。我刚刚接触Google App Engine,并试图用它来运行一个旧的Python CGI应用程序。我在我的Linux系统上安装了SDK,并有一个app.yaml文件说明了这一点:

  application:your-app -id 
版本:3
运行时:python27
api_version:1
线程安全:false

处理程序:
- url:/.*
脚本:members.py

脚本和必要的文件位于一个名为sms- gae在google_appengine文件夹中。当我运行

  ./ dev_appserver.py sms-gae / 

当我访问localhost:8080时,程序运行,但输出显示在终端控制台而不是浏览器中。什么都不输出到浏览器。相同的应用程序在正常的Web服务器CGI环境中运行良好。



根据主GAE文档,App Engine收集请求处理程序脚本写入标准输出流的所有数据,然后等待脚本返回。 ,所有的输出数据都发送给用户。



从我所看到的 GAE开发环境文档 - 以及更多详细信息 - 我已经正确设置了它。有关使用CGI脚本的文档相当稀少,并且在Internet上也无法找到关于此问题的任何内容。

输出如下:

  INFO 2013-10- 10 23:05:55,535 sdk_update_checker.py:245]检查SDK的更新。 
INFO 2013-10-10 23:05:56,838 sdk_update_checker.py:289]这个SDK版本比广告版本更新。
INFO 2013-10-10 23:05:57,181 api_server.py:138]启动API服务器:http:// localhost:49954
INFO 2013-10-10 23:05:57,225调度程序。启动模块默认运行在:http:// localhost:8080
INFO 2013-10-10 23:05:57,241 admin_server.py:117]启动admin服务器:http:// localhost :8000
Content-Type:text / html; charset = utf-8

[Content-Type行来自我的脚本,后面是输出]

INFO 2013-10-10 23:06:05,227成员.py:73]没有提供用户名或密码。
INFO 2013-10-10 23:06:05,262 module.py:599] default:GET / HTTP / 1.1200 -


解决方案

经过研究和一些答案(因为某些原因被删除),看起来 GAE事实上并不支持CGI (或者至少不是普通CGI)。它们似乎意味着GAE接受使用CGI适配器运行的WSGI代码(请参阅这里讨论一个例子)。 然而,使用这里给出的技巧。如果您将此代码添加到应用程序的底部,它将执行此操作,假设主代码是从 mainfunc 函数运行的,并且它响应获取请求(否则类似 post 方法可以被定义):

  import webapp2 
import StringIO
$ b class MainPage(webapp2.RequestHandler):
def get(self):
old_stdout = sys.stdout
new_stdout = StringIO.StringIO()
sys.stdout = new_stdout
mainfunc()
self.response.out.write(new_stdout.getvalue())
sys.stdout = old_stdout

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

然后重定向app.yaml指向members.app(在我的情况下)作为所有URL的处理程序。

The main documentation pages claim that it does: "The Python 2.7 runtime supports the WSGI standard and the CGI standard for backwards compatibility."

This must then be a very obvious problem, but I can't figure it out. I'm new to Google App Engine and am trying to use it to run an old Python CGI app. I've installed the SDK on my Linux system and have an app.yaml file that says this:

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

handlers:
- url: /.*
  script: members.py

The script and necessary files are in a folder called sms-gae inside the google_appengine folder. When I run

./dev_appserver.py sms-gae/

the program runs when I access localhost:8080, but the output appears on the terminal console instead of in the browser. Nothing is output to the browser. The same application runs fine in a normal web server CGI environment.

According to the main GAE documentation, "App Engine collects all of the data the request handler script writes to the standard output stream, then waits for the script to return. When the handler completes, all of the output data is sent to the user."

From what I can see of the GAE Development Environment documentation - and in more detail here - I've set it up correctly. Documentation on using CGI scripts is rather sparse, and can't find anything on this problem on the Internet as well.

The output is as follows:

INFO     2013-10-10 23:05:55,535 sdk_update_checker.py:245] Checking for updates to the SDK.
INFO     2013-10-10 23:05:56,838 sdk_update_checker.py:289] This SDK release is newer than the advertised release.
INFO     2013-10-10 23:05:57,181 api_server.py:138] Starting API server at: http://localhost:49954 
INFO     2013-10-10 23:05:57,225 dispatcher.py:168] Starting module "default" running at: http://localhost:8080
INFO     2013-10-10 23:05:57,241 admin_server.py:117] Starting admin server at: http://localhost:8000
Content-Type: text/html; charset= utf-8

[The Content-Type line comes from my script and is followed by its output]

INFO     2013-10-10 23:06:05,227 members.py:73] No userid or password supplied.
INFO     2013-10-10 23:06:05,262 module.py:599] default: "GET / HTTP/1.1" 200 -

解决方案

After research and some of the answers here (since deleted for some reason), it seems that GAE does not in fact support CGI (or at least not vanilla "normal" CGI). What they seem to mean is that GAE accepts WSGI code that is run with a CGI adaptor (see the discussion here for an example).

However, it is relatively easy to convert a CGI app to WSGI in a crude way, using the trick given here. If you add this code to the bottom of the app, it will do it, assuming the main code is run from the function mainfunc and it responds to get requests (otherwise a similar post method can be defined):

import webapp2
import StringIO

class MainPage(webapp2.RequestHandler):
   def get(self):
     old_stdout = sys.stdout
     new_stdout = StringIO.StringIO()
     sys.stdout = new_stdout
     mainfunc()
     self.response.out.write(new_stdout.getvalue())
     sys.stdout = old_stdout

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

and then redirect app.yaml to point at "members.app" (in my case) as the handler for all URLs.

这篇关于Google App Engine是否实际执行“正常” CGI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆