简体   繁体   English

发送非阻塞HTTP POST请求

[英]sending a non-blocking HTTP POST request

I have a two websites in php and python. 我在php和python中有两个网站。 When a user sends a request to the server I need php/python to send an HTTP POST request to a remote server. 当用户向服务器发送请求时,我需要php / python将HTTP POST请求发送到远程服务器。 I want to reply to the user immediately without waiting for a response from the remote server. 我想立即回复用户而不等待远程服务器的响应。

Is it possible to continue running a php/python script after sending a response to the user. 是否可以在向用户发送响应后继续运行php / python脚本。 In that case I'll first reply to the user and only then send the HTTP POST request to the remote server. 在这种情况下,我将首先回复用户,然后才将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 谢谢

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". 附录:我忘了你可能要设置“Context-Length”。 Also, check out this comment for 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';    
?>

You can spawn another process to handle the POST to the other server. 您可以生成另一个进程来处理到其他服务器的POST。 In PHP you would spawn the process and "disconnect" so you don't wait for the response. 在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. 如果要将参数传递给PHP脚本,可以使用getopt()函数来读取它们。 Not sure if you would do something similar in Python. 不确定你是否会在Python中做类似的事情。

您需要做的是让PHP脚本执行另一个执行服务器调用的脚本,然后向用户发送请求。

You have to use fsockopen. 你必须使用fsockopen。 And don't listen to the result 不要听结果

<?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);

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 ); 提交处理程序(处理表单提交的php或python脚本);
    • Your handler creates a new file and fill it up with the submission data. 您的处理程序创建一个新文件并用提交数据填充它。 You can, for instance, format the data as JSON; 例如,您可以将数据格式化为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 ). 创建一个文件系统事件驱动的cron(不是时间驱动的cron)。 See this and this questions (for a windows and an ubuntu servers respectively). 请参阅问题和问题(分别针对Windows和ubuntu服务器)。
    • Set your cron to execute a php or python script which will then re-post the data to a remote server. 设置你的cron来执行一个php或python脚本,然后将脚本重新发布到远程服务器。

Hookah旨在解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM