发送非阻塞HTTP POST请求 [英] sending a non-blocking HTTP POST request
问题描述
我有两个使用php和python的网站. 当用户向服务器发送请求时,我需要php/python将HTTP POST请求发送至远程服务器.我想立即回复用户,而不必等待远程服务器的响应.
I have a two websites in php and python. When a user sends a request to the server I need php/python to send an HTTP POST request to a remote server. I want to reply to the user immediately without waiting for a response from the remote server.
在向用户发送响应后,是否可以继续运行php/python脚本.在这种情况下,我将首先回复用户,然后才将HTTP POST请求发送到远程服务器.
Is it possible to continue running a php/python script after sending a response to the user. In that case I'll first reply to the user and only then send the HTTP POST request to the remote server.
是否可以在php/python中创建一个无阻塞的HTTP客户端而根本不处理响应?
Is it possible to create a non-blocking HTTP client in php/python without handling the response at all?
对于我来说,最好使用在php和python中具有相同逻辑的解决方案.
A solution that will have the same logic in php and python is preferable for me.
谢谢
推荐答案
在PHP中,您可以通过发送此请求来关闭连接(这与HTTP有关,并且在python中也可以工作,尽管我不知道使用哪种正确的语法):
In PHP you can close the connection by sending this request (this is HTTP related and works also in python, although I don't know the proper syntax to use):
// Send the response to the client
header('Connection: Close');
// Do the background job: just don't output anything!
附录:我忘了提到您可能必须设置上下文长度".另外,请查看此评论以获得提示和真实的测试用例.
Addendum: I forgot to mention you probably have to set the "Context-Length". Also, check out this comment for tips and a real test case.
<?php
ob_end_clean();
header('Connection: close');
ob_start();
echo 'Your stuff goes here...';
header('Content-Length: ' . ob_get_length());
ob_end_flush();
flush();
// Now we are in background mode
sleep(10);
echo 'This text should not be visible';
?>
这篇关于发送非阻塞HTTP POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!