通过PHP代码打印Python输出

时间:2021-02-27 21:45:52

I have a scraper which scrape one site (Written in python). While scraping the site, that print lines which are about to write in CSV. Scraper has been written in Python and now I want to execute it via PHP code. My question is

我有一个刮刮一个网站(用python编写)。在抓取网站时,即将用CSV写入的打印行。 Scraper是用Python编写的,现在我想通过PHP代码执行它。我的问题是

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. So;

我已经使用了exec函数,但它不是我的用途,并在执行所有程序后给出输出。所以;

Is it possible to get python output printed while it is getting executed via PHP.

是否有可能在通过PHP执行时打印python输出。

7 个解决方案

#1


4  

If i understand it well, your python scrapper output to a file and you want to "live" display the output via php. What about doing and loop in php in which you use filemtime to know whether or not the file has been updated? You might add a little sleep in order not to overload your server.

如果我理解得很好,你的python scrapper输出到一个文件,你想“生活”显示输出通过PHP。如何在php中进行循环并使用filemtime来知道文件是否已更新?您可以添加一点睡眠,以免使服务器过载。

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.

希望这对你有所帮助。

#2


2  

Simple case

简单的案例

Assuming execution of scraper is limited to php runtime, run it via popen: http://php.net/manual/en/function.popen.php

假设执行刮刀仅限于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. Many other rpc mechanisms are possible, domain sockets, watching a file, sysv rpc...

如果你想让scraper在后台运行并且只能连接到php,你可以使用一些pub / sub工具包或在scraper中实现一个小的web服务器,你可以用fopen获取结果更新(“https: // localhost:port / ...“)或curl。许多其他rpc机制是可能的,域套接字,观看文件,sysv rpc ...

#3


0  

I'd communicate using stdout instead of a file. Meaning the python script writes to stdout and the php script reads that.

我使用stdout而不是文件进行通信。这意味着python脚本写入stdout并且php脚本读取它。

Using proc_open you can control the python process from php and also read it's output.

使用proc_open你可以从php控制python进程并读取它的输出。

#4


0  

Instead of using exec you could use passthru, that will output the data directly to the browser. http://php.net/manual/en/function.passthru.php

您可以使用passthru而不是使用exec,它会将数据直接输出到浏览器。 http://php.net/manual/en/function.passthru.php

That should be enough to get the println from your script.

这应该足以从您的脚本中获取println。

#5


0  

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. That is instead of printing in Python, you could send it to the PHP Script, which on receiving the data would print it.

如果是这种情况,您可以通过HTTP将其作为POST数据传递。这不是在Python中打印,而是可以将其发送到PHP脚本,在接收数据时将打印它。

I am not too sure if this is what you want though.

我不太确定这是否是你想要的。

#6


0  

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/

#7


0  

Simply use system() instead of exec(). exec() saves all lines of stdout output of the external program into an array, but system() flushes stdout output "as it happens".

只需使用system()而不是exec()。 exec()将外部程序的所有stdout输出行保存到一个数组中,但是system()会在“发生”时刷新stdout输出。

#1


4  

If i understand it well, your python scrapper output to a file and you want to "live" display the output via php. What about doing and loop in php in which you use filemtime to know whether or not the file has been updated? You might add a little sleep in order not to overload your server.

如果我理解得很好,你的python scrapper输出到一个文件,你想“生活”显示输出通过PHP。如何在php中进行循环并使用filemtime来知道文件是否已更新?您可以添加一点睡眠,以免使服务器过载。

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.

希望这对你有所帮助。

#2


2  

Simple case

简单的案例

Assuming execution of scraper is limited to php runtime, run it via popen: http://php.net/manual/en/function.popen.php

假设执行刮刀仅限于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. Many other rpc mechanisms are possible, domain sockets, watching a file, sysv rpc...

如果你想让scraper在后台运行并且只能连接到php,你可以使用一些pub / sub工具包或在scraper中实现一个小的web服务器,你可以用fopen获取结果更新(“https: // localhost:port / ...“)或curl。许多其他rpc机制是可能的,域套接字,观看文件,sysv rpc ...

#3


0  

I'd communicate using stdout instead of a file. Meaning the python script writes to stdout and the php script reads that.

我使用stdout而不是文件进行通信。这意味着python脚本写入stdout并且php脚本读取它。

Using proc_open you can control the python process from php and also read it's output.

使用proc_open你可以从php控制python进程并读取它的输出。

#4


0  

Instead of using exec you could use passthru, that will output the data directly to the browser. http://php.net/manual/en/function.passthru.php

您可以使用passthru而不是使用exec,它会将数据直接输出到浏览器。 http://php.net/manual/en/function.passthru.php

That should be enough to get the println from your script.

这应该足以从您的脚本中获取println。

#5


0  

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. That is instead of printing in Python, you could send it to the PHP Script, which on receiving the data would print it.

如果是这种情况,您可以通过HTTP将其作为POST数据传递。这不是在Python中打印,而是可以将其发送到PHP脚本,在接收数据时将打印它。

I am not too sure if this is what you want though.

我不太确定这是否是你想要的。

#6


0  

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/

#7


0  

Simply use system() instead of exec(). exec() saves all lines of stdout output of the external program into an array, but system() flushes stdout output "as it happens".

只需使用system()而不是exec()。 exec()将外部程序的所有stdout输出行保存到一个数组中,但是system()会在“发生”时刷新stdout输出。