python - Arduino Heroku 请求

标签 python http heroku arduino httprequest

我正在尝试从 Arduino 向 Heroku 发出请求应用。它返回的只是 Heroku 的 404 页面。所以我用 Python 写了一个类似的脚本,它运行良好。

Arduino代码:

char serverName[] = "ruby-coffee-maker.herokuapp.com";

...

if (!client.connected()) {
    Serial.println("connecting...");

    if (client.connect(serverName, 80)) {
        Serial.println("connected");

        // Make a HTTP request
        client.println("GET / HTTP/1.0");
        client.println();

        // Wait for response
        delay(100);

        // If data can be read from te server, print it
        while (client.available()) {
            char c = client.read();
            Serial.print(c);
        }

        Serial.print("Done");
        client.stop();
    }
    else {
      // If you didn't get a connection to the server:
      Serial.println("connection failed");
    }
}

Python代码:

import httplib

h = httplib.HTTPConnection("ruby-coffee-maker.herokuapp.com")

h.request("GET", "/")

r = h.getresponse()
data = r.read()

print r.status, r.reason, "\"" + data + "\""

h.close()

我该如何解决这个问题?

最佳答案

您是否将 Arduino 代码发送的请求与 Python 代码进行了比较?根据我对HTTP/1.1的理解,你需要在GET请求中添加一个Host header filed。见 5.1.2 here .

抱歉,我刚刚看到您在 Arudino 请求中使用了 HTTP/1.0,是否有特殊原因?即使 1.0 不需要 Host-filed,您可能仍然需要包含它(例如,某些代理要求它存在)。

关于python - Arduino Heroku 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15879592/

相关文章:

python - np.loadtxt() 如何从txt文件中每隔一行加载? Python

python-docx: 添加来自网络的图片

http - Firefox 获取 API : How to omit the "origin" header in the request?

php - Heroku 部署错误 : No runtime requirements

postgresql - 在 padrino 中配置 heroku 数据库连接 - DATABASE_URL 失败

python - 调用重置函数时,仅清除最后一行条目

python - 使用 Python v3.x 和 C API 解释 Python v2.5 代码(关于整数除法)

c++ - 如何在 Windows 上即时读取和更改 HTTP 流量?

php - 除了 fclose() 和 fopen() 之外,如何在 PHP 中重新倒回 () 一个 http 流文件?

ruby-on-rails - 第一个 Heroku、Ruby 和 Postgres 应用程序 - 本地 Postgres 错误