Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
应用程序引擎-Python-从脚本打开本地HTML文件_Python_Google App Engine_Wsgi - Fatal编程技术网

应用程序引擎-Python-从脚本打开本地HTML文件

应用程序引擎-Python-从脚本打开本地HTML文件,python,google-app-engine,wsgi,Python,Google App Engine,Wsgi,使用启动器测试Google App Engine应用程序时,启动器将启动本地服务器,并打开一个选项卡,收听http://localhost:8080/如果将App.yaml文件配置为指向html文件,则该网页将打开。比如你的主页是index.html app.yaml文件 application: myProjectName version: 2 runtime: python27 threadsafe: false api_version: 1 handlers: - url: .* st

使用启动器测试Google App Engine应用程序时,启动器将启动本地服务器,并打开一个选项卡,收听
http://localhost:8080/
如果将App.yaml文件配置为指向html文件,则该网页将打开。比如你的主页是index.html

app.yaml文件

application: myProjectName
version: 2
runtime: python27
threadsafe: false
api_version: 1
handlers:
- url: .*
  static_dir: index.html
- url: /.*
  script: main.py
如果app.yaml配置为指向根url中的python脚本,我不知道如何让脚本加载网页:

app.yaml文件

application: myProjectName
version: 2
runtime: python27
threadsafe: false
api_version: 1
handlers:
- url: .*
  static_dir: index.html
- url: /.*
  script: main.py
如果我的
main.py
Python脚本是:

import webbrowser
webbrowser.open_new("README.html")
当我从pythonshell运行代码时,这将在我的浏览器中打开README.html文件,但是如果我从googleappenginelauncher启动应用程序,它将不会加载html文件。如何让.py文件在启动程序在localhost:8000上启动应用程序后打开HTML文件

我正在看一个谷歌的例子,我猜它使用了一个WSGIApplication Web应用程序。首先,Python代码经过授权过程,然后在脚本末尾,有以下代码:

# Create an WSGI application suitable for running on App Engine
application = webapp.WSGIApplication(
    [('/', MainPage), ('/svc', ServiceHandler), ('/about', AboutHandler),
     ('/user', UserHandler)],
    # XXX Set to False in production.
    debug=True
)


def main():
  """Main entry point for executing a request with this handler."""
  run_wsgi_app(application)


if __name__ == "__main__":
    main()

我非常感谢任何有这方面经验的人的反馈。

我所知道的最简单的例子是:

相应的
app.yaml
为:

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

handlers:
- url: /.*
  script: helloworld.application
您创建了一个类
MainPage
——每当您向
localhost:8080/
发出请求时(请注意,斜杠是不必要的),您都会被引导到主页面。
应用程序负责将请求路由到适当的类,并为每个请求创建该类的新实例。它还调用
get
post
或任何HTTP方法。无论您在响应中写入什么,都会作为网页返回到浏览器


现在一页也没那么有趣了。也许你也需要
localhost:8080/再见
。然后您只需添加另一个类并在应用程序中“注册”它:

# helloworld.py
import webapp2

class MainPage(webapp2.RequestHandler):

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

class GoodbyePage(webapp2.RequestHandler):

    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.write('Goodbye, World.  Time to sleep.')

application = webapp2.WSGIApplication([
    ('/', MainPage),
    ('/goodbye', GoodbyePage),
], debug=True)
此时无需对app.yaml进行任何更改


但是,如果你需要将所有页面保存在一个文件中,生活可能会有点令人沮丧。我们可以通过修改
app.yaml
将其分解为2个(或更多)文件

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

handlers:
- url: /goodbye
  script: goodbye.app

- url: /.*
  script: helloworld.application
helloworld.py
与第一个示例相同<代码>再见.py
如下所示:

# goodbye.py
import webapp2

class GoodbyePage(webapp2.RequestHandler):

    def get(self):
        self.response.headers['Content-Type'] = 'text/html'
        self.response.write('<html><head></head><body>Goodbye!</body></html>')

app = webapp2.WSGIApplication([
    ('/goodbye', GoodbyePage),
], debug=True)
#再见.py
导入webapp2
类GoodbyePage(webapp2.RequestHandler):
def get(自我):
self.response.headers['Content-Type']='text/html'
self.response.write('再见!')
app=webapp2.WSGIApplication([
(“/再见”,再见页),
],debug=True)

app.yaml
中的URL是正则表达式——您希望将它们从最特定到最不特定排序(否则,您可能会使用错误的处理程序处理请求)。还请注意,
app.yaml
中的约定是
script:。
当我们以这种方式设置它时。

这将从文件中获取HTML,然后将其写入浏览器。这实际上是我想要的:

class MainPage(webapp2.RequestHandler):
    def get(self):
        template = JINJA_ENVIRONMENT.get_template('index.html')
        self.response.write(template.render())
这是整个模块:

import cgi
import os

import jinja2
import webapp2

JINJA_ENVIRONMENT = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
    extensions=['jinja2.ext.autoescape'],
    autoescape=True)

class MainPage(webapp2.RequestHandler):
    def get(self):
        template = JINJA_ENVIRONMENT.get_template('index.html')
        self.response.write(template.render())

class PageTwo(webapp2.RequestHandler):
    def post(self):
        self.response.write('<html><body>You wrote:<pre>')
        self.response.write(cgi.escape(self.request.get('content')))
        self.response.write('</pre></body></html>')

application = webapp2.WSGIApplication([
    ('/', MainPage),
    ('/sign', PageTwo),
], debug=True)
即使WebApp2请求处理程序基于URL为特定HTML文件提供服务,app.yaml文件也控制发送不同URL时发生的情况。可以将app.yaml文件配置为运行python脚本并上载特定的HTML文件。如果您的应用程序有很多子目录,并且main.py脚本直到app.yaml文件结束时才加载,则将app.yaml文件配置为直接加载静态HTML可能会导致主页加载速度加快:

INDEX_HTML = open('index.html').read()

下面是另一个没有Jinja2的示例。要使用Python提供HTML,您只需要以下三件简单的事情

1) 使用webapp2 Python模块

导入webapp2

2) 将HTML文件读入变量:

self.response.out.write(INDEX_HTML)
3) 然后在发送某个URL时将其写出来:

import cgi
import webapp2

class MainPage(webapp2.RequestHandler):
    def get(self):
        INDEX_HTML = open('index.html').read()
        self.response.out.write(INDEX_HTML)

class PageTwo(webapp2.RequestHandler):
    def post(self):
        self.response.write('<html><body>You wrote:<pre>')
        self.response.write(cgi.escape(self.request.get('content')))
        self.response.write('</pre></body></html>')

application = webapp2.WSGIApplication([
    ('/', MainPage),
    ('/sign', PageTwo),
], debug=True)
这是完整的Python脚本:

导入cgi
导入webapp2
类主页(webapp2.RequestHandler):
def get(自我):
INDEX_HTML=open('INDEX.HTML').read()
self.response.out.write(INDEX_HTML)
类PageTwo(webapp2.RequestHandler):
def post(自我):
self.response.write('youwrite:')
self.response.write(cgi.escape(self.request.get('content'))
self.response.write(“”)
application=webapp2.WSGIApplication([
(“/”,主页),
(“/签名”,第二页),
],debug=True)
请注意,您的app.yaml配置需要正确,并且需要一个HTML文件。
webapp2
模块提供了一个
RequestHandler
,可以路由您的URL请求。(显示不同的网页内容)这是一个包含代码、manifest app.yaml文件和index.html文件的GitHub存储库


我刚找到这个,太棒了!
import cgi
import webapp2

class MainPage(webapp2.RequestHandler):
    def get(self):
        INDEX_HTML = open('index.html').read()
        self.response.out.write(INDEX_HTML)

class PageTwo(webapp2.RequestHandler):
    def post(self):
        self.response.write('<html><body>You wrote:<pre>')
        self.response.write(cgi.escape(self.request.get('content')))
        self.response.write('</pre></body></html>')

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