如何调用 c++ 来自 Perl 或 PHP 的二进制文件(在 Linux 上使用 Apache 的 CGI-BIN)?
我有一个用 C++ 编写的网站 cgi-bin 程序。
不幸的是,我朋友网站的网站提供商只允许 Perl 或 PHP cgi-bin 脚本。
有没有一种简单的方法来简单地拥有一个非常小的 Perl 或 PHP 包装器,只调用 c++ 编译的二进制文件?
C++ 程序是否仍然能够从标准输入读取 POST 命令,或者必须由 Perl 或 PHP 读取然后转发。
I have a website cgi-bin program that is written in c++.
Unfortunately the website provider for my friend's site only allows Perl or PHP cgi-bin scripts.
Is there an easy way to simply have a very small Perl or PHP wrapper that just calls the c++ compiled binary?
Would the c++ program still be able to read from stdin for POST commands or would it have to be read by the Perl or PHP and then forwarded.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 Perl 的反引号或“系统”命令来运行 shell 命令。 另外,perl 有很多“内联”类,允许您用其他语言编写代码以在 perl 中调用,其中包括一个用于 C++。 如果您找不到有效的东西,也许您可以使用该包制作自己的包装器。
You can use Perl's backticks or "system" commands to run shell commands. Also, perl has a lot of "Inline" classes that allow you to write code in other languages to be called in perl, including one for C++. If you can't find something that works, maybe you can make your own wrapper using that package.
您可以使用 PHP 的 system() 函数来执行 shell 命令,您可以使用该命令来启动另一个程序。 该程序的 STDOUT 然后将转到与 php 的 stdout 相同的位置(到 HTTP 连接)。 您可能需要做一些乱七八糟的事情才能让标准输入从 get/post/etc 中读取。
但很可能网站提供商已禁用您以这种方式执行程序的能力。 这可能是一个安全风险,如果他们专门只允许 php 和 perl,那么他们可能会专门禁用尽可能多的运行非 php/perl 的方法。 所以,简短的回答是你可能不走运。
You can use PHP's system() function to execute a shell command, which you could use to launch another program. The STDOUT of that program would then go to the same place as php's stdout (to the HTTP connection). You might have to do some messing around to get stdin to read from get/post/etc.
But most likely the website provider has disabled the ability of you to execute programs in this way. It can be a security risk, and if they specifically only allow php and perl, then they would probably specifically disable as many methods of running non-php/perl as they could. So, short answer is you're probably out of luck.
请参阅我的 Awstats Wrapper 博客文章,了解一些 PHP 代码解决了我的问题。 我的方法是构造一个命令行,该命令行具有从 CGI 查询参数设置的正确环境变量,然后对结果输出进行替换,以便任何返回到 CGI 的链接都将转到我的包装器脚本。 我还取消了 CGI 的标头输出,因为 PHP 提供了自己的标头。
See my Awstats Wrapper blog post for some PHP code that solved my problem. My approach was to construct a command line that has the right environment variables set from the CGI query params and then do substitutions on the resulting output so that any links back to the CGI would go to my wrapper script instead. I also nuked the headers output from the CGI since PHP provides its own headers.