【问题标题】:Creating a web server with Flask that outputs something available for a GET request使用 Flask 创建 Web 服务器,输出可用于 GET 请求的内容
【发布时间】:2012-03-31 22:32:54
【问题描述】:

我有一个 Python 脚本,可以在字符串中输出一段文本。我正在尝试使那段文本在线可用,以便我可以将其拉到 Arduino 微控制器上。换句话说,工作流程是这样的:文本源 > Python > ??? > Arduino > 最终输出。

我已经使用 Heroku 的示例 Flask 代码开始尝试让这个功能正常工作。他们的 Flask 代码如下:

 import os

 from flask import Flask
 app = Flask(__name__)

 @app.route('/')
 def hello():
    return 'Hello World!'

 if __name__ == '__main__':
    # Bind to PORT if defined, otherwise default to 5000.
    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0', port=port)

当我尝试在我的 heroku 应用程序上执行 HTTP 获取请求时,它给了我一个 404。我怀疑这是因为这个脚本并没有真正输出任何东西。例如,当我使用 Processing.org 网站上的这个 Processing 应用程序执行 GET 请求时:

import processing.net.*;

Client c;
String data;

void setup() {
   size(200, 200);
   background(50);
   fill(200);
   c = new Client(this, "http://freezing-stream-5123.herokuapp.com/", 80); // Connect to server on port 80
   c.write("GET / HTTP/1.1\n"); // Use the HTTP "GET" command to ask for a Web page
   c.write("Host: my_domain_name.com\n\n"); // Be polite and say who we are
   }

void draw() {
   if (c.available() > 0) { // If there's incoming data from the client...
   data = c.readString(); // ...then grab it and print it
   println(data);
  }
}

返回的是这样的:

HTTP/1.1 200 OK
Date: Sat, 31 Mar 2012 22:27:10 GMT
Server: Apache
Cache-control: no-cache, no-store
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Pragma: no-cache
Content-Length: 968
Connection: close
Content-Type: text/html; charset=UTF-8

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head><meta http-     equiv="refresh" content="0;url=http://earthlink-help.com/main?
InterceptSource=0&ClientLocation=us&ParticipantID=xj6e3468k634hy3945zg3zkhfn7zfgf6&FailureMode    =1&SearchQuery=&FailedURI=http%3A%2F%2Fmy_domain_name.com%2F&AddInType=4&Version=2.1.8-1.90base&Referer=&Implementation=0"/><script type="text/javascript">url="http://earthlink-help.com/main?InterceptSource=0&ClientLocation=us&ParticipantID=xj6e3468k634hy3945zg3zkhfn7zfgf6&FailureMode=1&SearchQuery=&FailedURI=http%3A%2F%2Fmy_domain_name.com%2F&AddInType=4&Version=2.1.8-1.90base&Referer=&Implementation=0";if(top.location!=location){var w=window,d=document,e=d.documentElement,b=d.body,x=w.innerWidth||e.clientWidth||b.clientWidth,y=w.innerHeight||e.clientHeight||b.clientHeight;url+="&w="+x+"&h="+y;}window.
location.replace(url);</script></head><body></body></html>

又名:什么都没有。当我使用 curl 拉网页时,“Hello world”确实出现了,但我不知道这是否意味着什么。

所以我的问题是:谁能指出我可以将我的字符串粘在我可以从中检索到的东西上?我意识到这可能是一个愚蠢的问题,但我完全迷失在网络服务器等的海洋中,希望得到一些指导。

谢谢!

【问题讨论】:

    标签: python http heroku flask


    【解决方案1】:

    当我使用 curl 拉网页时,“Hello world”确实出现了,但我不知道这是否意味着什么。

    这可能意味着它正在工作,问题可能出在处理代码中。

    不清楚您的问题是否只是让基本的 Flask 应用程序运行。

    【讨论】:

      【解决方案2】:

      看起来问题的一部分是您没有返回响应对象,请查看 API 文档。有一个 Response 对象,您应该可以只填充文本,json 或文本。如果您打算使用 URL 端点,请查看使用 Flask 中的函数“jsonify”。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-12
        • 2015-07-09
        • 1970-01-01
        相关资源
        最近更新 更多