python 发送非阻塞 HTTP POST 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1555517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sending a non-blocking HTTP POST request
提问by pablo
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 中有两个网站。当用户向服务器发送请求时,我需要 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 POST 请求发送到远程服务器。
Is it possible to create a non-blocking HTTP client in php/python without handling the response at all?
是否可以在 php/python 中创建非阻塞 HTTP 客户端而不处理响应?
A solution that will have the same logic in php and python is preferable for me.
在 php 和 python 中具有相同逻辑的解决方案对我来说更可取。
Thanks
谢谢
回答by ntd
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):
在 PHP 中,您可以通过发送此请求来关闭连接(这与 HTTP 相关并且在 python 中也有效,尽管我不知道要使用的正确语法):
// 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 commentfor tips and a real test case.
附录:我忘了提到您可能必须设置“上下文长度”。另外,请查看此评论以获取提示和真实测试用例。
Example:
例子:
<?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';
?>
回答by Brent Baisley
You can spawn another process to handle the POST to the other server. In PHP you would spawn the process and "disconnect" so you don't wait for the response.
您可以生成另一个进程来处理对另一台服务器的 POST。在 PHP 中,您将生成进程并“断开连接”,因此您无需等待响应。
exec("nohup /path/to/script/post_content.php > /dev/null 2>&1 &");
You can then you curl to perform the post. If you want to pass parameters to the PHP script, you can use the getopt() function to read them. Not sure if you would do something similar in Python.
然后你可以卷曲来执行帖子。如果要将参数传递给 PHP 脚本,可以使用 getopt() 函数读取它们。不确定你是否会在 Python 中做类似的事情。
回答by Anthony
What you need to do is have the PHP script execute another script that does the server call and then sends the user the request.
您需要做的是让 PHP 脚本执行另一个执行服务器调用的脚本,然后向用户发送请求。
回答by Mangirdas Skripka
You have to use fsockopen. And don't listen to the result
你必须使用 fsockopen。不要听结果
<?php
$fp = fsockopen('example.com', 80);
$vars = array(
'hello' => 'world'
);
$content = http_build_query($vars);
fwrite($fp, "POST /reposter.php HTTP/1.1\r\n");
fwrite($fp, "Host: example.com\r\n");
fwrite($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
fwrite($fp, "Content-Length: ".strlen($content)."\r\n");
fwrite($fp, "Connection: close\r\n");
fwrite($fp, "\r\n");
fwrite($fp, $content);
回答by Francisco Luz
You have to set a middle man. So in your own server you would have:
你必须设置一个中间人。因此,在您自己的服务器中,您将拥有:
- A web form;
- A submit handler ( php or python script that handles the form submission );
- Your handler creates a new file and fill it up with the submission data. You can, for instance, format the data as JSON;
- So your handler has a single job, save the submitted data in a file and respond the user, nothing else. This should be fast.
- Create a filesystem event driven cron ( not a time driven cron ). See thisand thisquestions (for a windows and an ubuntu servers respectively).
- Set your cron to execute a php or python script which will then re-post the data to a remote server.