The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Sending values to python then back via php?
Discuss Sending values to python then back via php? in the PHP Development forum on Dev Shed. Sending values to python then back via php? PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 11th, 2013, 11:15 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 5
Time spent in forums: 44 m 18 sec
Reputation Power: 0
|
|
|
Sending values to python then back via php?
Hi everyone,
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...
Python:
Code:
from urllib import urlopen
import re
webpage=urlopen('http://feeds.huffingtonpost.com/huffingtonpost/LatestNews').read()
patFinderTitle = re.compile('<title>(.*)</title>')
titles = patFinderTitle.findall(webpage)
for (i,title,) in enumerate(titles):
print('title %2d: %s' %(i,title,))
PHP/HTML:
Code:
<? $pagenumber = $_POST["pagenumber"]; ?>
<body>
<form method="post" action="pythontest.php">
Page Number:<input type="text" name="pagenumber"><br /> <input type="submit" value="submit" name="submit"> <br /> </form>
</body>
Can someone please help and direct me at how to go about this? I'm completely stuck. Thanks in advance!
|

March 11th, 2013, 11:38 PM
|
 |
Lost in code
|
|
|
|
|
This is something you'll do from your pythontest.php file. How are you invoking the Python code?
|

March 11th, 2013, 11:42 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 5
Time spent in forums: 44 m 18 sec
Reputation Power: 0
|
|
|
I'm not at the moment. I tried using commands such as argv, but i could not get it to work at all.
|

March 11th, 2013, 11:56 PM
|
 |
Lost in code
|
|
|
|
|
$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, 01:20 AM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 5
Time spent in forums: 44 m 18 sec
Reputation Power: 0
|
|
|
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, 06:51 PM
|
 |
Lost in code
|
|
|
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|