简体   繁体   English

在python脚本中将webapp2用法替换为self.response.write

[英]Replace webapp2 usage for self.response.write in python script

I'm fairly new to python and have started by creating a simple app on google app engine, but now I want to deploy it somewhere else. 我是python的新手,并且开始通过在google app引擎上创建一个简单的应用程序开始工作,但是现在我想将其部署到其他地方。 (I know I can install webapp2 in a non-appengine python environment but would rather not at this point.) (我知道我可以在非appengine python环境中安装webapp2,但目前不愿意。)

How can I change the following python code so that it does the same thing but without webapp2? 如何更改以下python代码,使其执行相同的操作但没有webapp2?

import webapp2

class MainPage(webapp2.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/html'
        self.response.write('<a href="index.html">Search</a>')

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

I've tried the print command, urllib, redirects and even considered scripting a basic web server, but none of those work or seem like overkill. 我已经尝试过使用print命令,urllib,重定向,甚至考虑编写脚本来编写基本的Web服务器,但是这些都不起作用,或者看起来像是过大了。

I'm trying to have a very basic python controlled/created welcome page with a link that goes to my single-page website, index.html. 我正在尝试建立一个非常基本的python控制/创建的欢迎页面,并带有一个指向我的单页网站index.html的链接。

I'm currently using Cloud9 which runs an Apache web server which loads the index.html if the python script doesn't work. 我目前正在使用Cloud9,该服务器运行Apache Web服务器,如果python脚本不起作用,该服务器将加载index.html。 However I'd much prefer to have the python script work in this simple manner, before I begin converting the whole thing to a full-scale Flask or Django application. 但是,在开始将整个内容转换为完整的Flask或Django应用程序之前,我更喜欢以这种简单的方式来运行python脚本。

Any help or tips much appreciated. 任何帮助或提示,不胜感激。

In the end pythonanywhere gave me what I needed via WSGI and it turned out to be fairly straightfoward: 最终pythonanywhere通过WSGI给了我所需的东西,结果很简单:

SEARCH = """<html>
<head></head>
<body>
    <div style="text-align: center; padding-left: 50px; padding-top: 50px; font-size: 1.75em;">
        <p>Welcome:</p><a href="index.html">Search</a>
    </div>
</body>
</html>
"""

def application(environ, start_response):
    if environ.get('PATH_INFO') == '/':
        status = '200 OK'
        content = SEARCH
    else:
        status = '404 NOT FOUND'
        content = 'Page not found.'
    response_headers = [('Content-Type', 'text/html'), ('Content-Length', str(len(content)))]
    start_response(status, response_headers)
    yield content.encode('utf8')

If your goal is to handle basic requests using only built in modules, you could look up BaseHTTPServer and related classes. 如果您的目标是仅使用内置模块来处理基本请求,则可以查找BaseHTTPServer和相关类。 Here is a simple example: 这是一个简单的示例:

from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer

PORT_NUMBER = 8080

class myHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-type','text/html')
        self.end_headers()
        self.wfile.write('<a href="index.html">Search</a>')
        return

try:
    server = HTTPServer(('', PORT_NUMBER), myHandler)
    server.serve_forever()    
except KeyboardInterrupt:
    server.socket.close()

Note that this will not serve static files by default but you should be able to add multiple handlers. 请注意,默认情况下,这不会提供静态文件,但您应该能够添加多个处理程序。 Maybe look up: https://docs.python.org/2/library/simplehttpserver.html 也许可以查找: https : //docs.python.org/2/library/simplehttpserver.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM