March 12th, 2013, 12:15 AM
Sending values to python then back via php?
So what I'm trying to do is for a visitor to input a value (in this case a page number for a rss feed) and then that is put into a website url. The url is then sent to a python program that gathers all the titles of articles on that page and sends them back to the php in order to be displayed for the visitor to see. I've tried reading up various ways to do this ($argv, etc.) but I just can't seem to come up with how to go about it.
Here is the codes i got thus far and am currently stuck at...
from urllib import urlopen
patFinderTitle = re.compile('<title>(.*)</title>')
titles = patFinderTitle.findall(webpage)
for (i,title,) in enumerate(titles):
print('title %2d: %s' %(i,title,))
Can someone please help and direct me at how to go about this? I'm completely stuck. Thanks in advance!
<? $pagenumber = $_POST["pagenumber"]; ?>
<form method="post" action="pythontest.php">
Page Number:<input type="text" name="pagenumber"><br /> <input type="submit" value="submit" name="submit"> <br /> </form>
March 12th, 2013, 12:38 AM
This is something you'll do from your pythontest.php file. How are you invoking the Python code?
March 12th, 2013, 12:42 AM
I'm not at the moment. I tried using commands such as argv, but i could not get it to work at all.
March 12th, 2013, 12:56 AM
$argv is only for input to the PHP script; it can only be passed to the PHP script from the program that is invoking PHP.
If you use the passthru() function to execute your Python command, the output will automatically be sent straight to the browser.
You could also use exec() and pass it a second parameter. The second parameter will be an array containing the output (it is passed by reference).
If you need bi-direction communication, you can use proc_open to open a pipe between the two programs; however, this is complicated, requires modifications to your Python code and sounds like more than you need for this particular application.
March 12th, 2013, 02:20 AM
I think a bi-direction will be needed though, since it needs to be called back to php and then stylized. right? or am i totally missing the point?
March 12th, 2013, 07:51 PM
Bi-directional may not have been the best description. proc_open will allow both the PHP script and the Python script to run at the same time and allow them to pass multiple messages back and fourth as they are running.
passthru and exec will pause the PHP script while the Python script executes, and only continue running the PHP script after the Python script exits. The PHP script will only be able to send data to the Python script at one point, before the Python script starts, and it will only be able to receive data from the Python script once, after the Python script finishes.