【问题标题】:POST request to Heroku results in Python IncompleteRead Error对 Heroku 的 POST 请求导致 Python IncompleteRead 错误
【发布时间】:2013-06-06 20:47:59
【问题描述】:

我的 Python 脚本,带有行:

from requests import post

...
while(1):

   result = readSensors().result

   payload["z"] = (result['zforce'])
   payload["x"] = (result['xforce'])
   payload["y"] = (result['yforce'])
   payload["light"] = (result['light'] )
   payload["pitch"] = ( result["pitch"] )
   payload["azimuth"] = ( result["azimuth"] )
   payload["roll"] = ( result["roll"] )

   post(SERVER, data = payload )
   sleep(0.02)

到我的 Heroku webapp 服务器会导致 Python 错误 httplib.IncompleteRead: IncompleteRead(0 bytes read) 错误。


web.js 文件如下所示:

var server = http.createServer(function(request, response){     
  if (request.method == 'POST'){
            var body = '';
            request.on('data', function (data) {
                body += data;
            });
            request.on('end', function () {
                //send data to clients.
                io.sockets.emit( 'data', parse(body) );

            });
    response.end()

}
server.listen(process.env.PORT || 5000);


var io = io.listen(server);

io.set('log level', 1);
io.configure(function () { 
  io.set("transports", ["xhr-polling"]); 
  io.set("polling duration", 10);  
});

【问题讨论】:

    标签: python heroku http-post python-requests


    【解决方案1】:

    这更有可能是您的“webapp 服务器”而不是您的客户端代码的问题,因此您必须包含有关服务器的更多信息。

    造成这种情况的一个常见原因是您发回了带有Transfer-Encoding: chunked 的响应,但没有正确提供chunk-encoded 的响应正文。

    比如下面的服务器代码……

    from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
    
    class MyRequestHander(BaseHTTPRequestHandler):
    
        def do_GET(self):
            self.send_bogus_response()
    
        def do_POST(self):
            self.send_bogus_response()
    
        def send_bogus_response(self):
            self.send_response(200)
            self.send_header('Content-Type', 'text/plain')
            self.send_header('Connection', 'close')
            self.send_header('Transfer-Encoding', 'chunked')
            self.end_headers()
    
    server = HTTPServer(('', 8000), MyRequestHander)
    server.serve_forever()
    

    ...使用requests 库调用时会导致同样的错误...

    >>> import requests
    >>> requests.post('http://localhost:8000', data={'foo':'bar'})
    ...
    httplib.IncompleteRead: IncompleteRead(0 bytes read)
    

    Apparently,如果服务器没有读取整个请求正文,也可能会发生此问题,并且可能有其他一些原因导致这种情况发生,但我不确定哪个最有可能不了解服务器设置的更多信息。


    更新

    web.js 代码存在很多问题,但主要问题是您在读取 ​​POST 数据之前发回了响应。 response.end() 需要进入 request.on('end', ...) 函数,否则你的 request.on(...) 函数都不会被调用。

    下面的代码应该可以解决这个问题...

    var server = http.createServer(function(request, response)
    {
        if (request.method == 'POST')
        {
            var body = '';
    
            request.on('data', function(data)
            {
                body += data;
            });
    
            request.on('end', function()
            {
                //send data to clients.
                io.sockets.emit('data', parse(body));
                response.end();
            });
        }
    });
    

    ...虽然我不确定parse(...) 函数应该是什么。

    【讨论】:

    • 感谢您的回复。我添加了有关服务器的更多信息。我明天会试试你的建议。
    • 谢谢!如果您对您的帮助感到好奇,这里是该项目:artech.herokuapp.com
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 2018-08-13
    • 1970-01-01
    • 2016-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多