简体   繁体   English

通过 PHP 代码打印 Python 输出

[英]Print Python output by PHP Code

I have a scraper which scrape one site (Written in python).我有一个抓取一个站点的抓取器(用 python 编写)。 While scraping the site, that print lines which are about to write in CSV.在抓取网站时,打印即将写入 CSV 的行。 Scraper has been written in Python and now I want to execute it via PHP code. Scraper 是用 Python 编写的,现在我想通过 PHP 代码来执行它。 My question is我的问题是

how can I print each line which is getting printed by python code.如何打印由 python 代码打印的每一行。

I have used exec function but it is none of my use and gives output after executing all the program.我使用了 exec 函数,但它不是我的用途,而是在执行所有程序后给出输出。 So;所以;

Is it possible to get python output printed while it is getting executed via PHP.是否有可能在通过 PHP 执行时打印 python 输出。

If i understand it well, your python scraper output to a file and you want to "live" display the output via php.如果我理解得很好,你的 python抓取器输出到一个文件,你想“实时”通过 php 显示输出。 What about doing and loop in php in which you use filemtime to know whether or not the file has been updated?在使用filemtime来了解文件是否已更新的 php 中执行和循环怎么样? You might add a little sleep in order not to overload your server.为了不让您的服务器超载,您可能会增加一点睡眠时间。

If your are using a web page, you may use AJAX to reload only the concerned part of the page at a regular interval.如果您正在使用网页,您可以使用 AJAX 定期重新加载页面的相关部分。

Hoping this helps you.希望这对你有帮助。

Simple case简单案例

Assuming execution of scraper is limited to php runtime, run it via popen: http://php.net/manual/en/function.popen.php假设 scraper 的执行仅限于 php 运行时,通过 popen 运行它:http: //php.net/manual/en/function.popen.php

More involved case更多涉案案例

If you want scraper to run on background and only connect to it vis php from time to time, you can either use some pub/sub toolkit or implement a small web server in the scraper that you can fetch result updates with fopen("https://localhost:port/...") or curl.如果你想让 scraper 在后台运行并且只不时连接到 vis php,你可以使用一些 pub/sub 工具包或者在 scraper 中实现一个小型 web 服务器,你可以使用 fopen("https: //localhost:port/...") 或卷曲。 Many other rpc mechanisms are possible, domain sockets, watching a file, sysv rpc...许多其他 rpc 机制也是可能的,域套接字,监视文件,sysv rpc ...

I'd communicate using stdout instead of a file.我会使用标准输出而不是文件进行通信。 Meaning the python script writes to stdout and the php script reads that.这意味着 python 脚本写入 stdout 而 php 脚本读取它。

Using proc_open you can control the python process from php and also read it's output.使用proc_open您可以从 php 控制 python 进程并读取它的输出。

Instead of using exec you could use passthru , that will output the data directly to the browser.您可以使用passthru而不是使用exec ,它将数据直接输出到浏览器。 http://php.net/manual/en/function.passthru.php http://php.net/manual/en/function.passthru.php

That should be enough to get the println from your script.这应该足以从您的脚本中获取 println。

I think I have a fair idea of what you are saying put I am not too sure what you mean.我想我很清楚你在说什么,但我不太确定你的意思。

If you mean to say that everytime the python script does a print, you want the php code to output what was print?如果你的意思是说每次 python 脚本打印时,你希望 php 代码输出打印的内容?

If that is the case you could pass it as a POST DATA via HTTP.如果是这种情况,您可以通过 HTTP 将其作为 POST 数据传递。 That is instead of printing in Python, you could send it to the PHP Script, which on receiving the data would print it.那不是用 Python 打印,而是可以将它发送到 PHP 脚本,PHP 脚本在接收到数据时会打印它。

I am not too sure if this is what you want though.我不太确定这是否是您想要的。

For proper communication you need to setup any medium, so you can use fifo, through it you can write string in python and read it with php.为了正确的通信,你需要设置任何媒体,这样你就可以使用 fifo,通过它你可以在 python 中编写字符串并用 php 读取它。

For PHP fifo http://php.net/manual/en/function.posix-mkfifo.php对于 PHP fifo http://php.net/manual/en/function.posix-mkfifo.php

For Python http://www.doughellmann.com/PyMOTW/Queue/对于 Python http://www.doughellmann.com/PyMOTW/Queue/

Simply use system() instead of exec() .只需使用system()而不是exec() exec() saves all lines of stdout output of the external program into an array, but system() flushes stdout output "as it happens". exec()将外部程序的所有 stdout 输出行保存到一个数组中,但system() “当它发生时”刷新 stdout 输出。

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

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