#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    5
    Rep 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!
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,301
    Rep Power
    7170
    This is something you'll do from your pythontest.php file. How are you invoking the Python code?
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    5
    Rep 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.
  6. #4
  7. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,301
    Rep Power
    7170
    $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.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    5
    Rep 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?
  10. #6
  11. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,301
    Rep Power
    7170
    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.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

IMN logo majestic logo threadwatch logo seochat tools logo