PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 7th, 2013, 03:26 PM
MynE MynE is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 35 MynE User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 32 m 59 sec
Reputation Power: 1
Proc_open problem

Hi, from my last post, I am trying to pass an input data to my python script, and thanks to user'requinix' that guided me to use proc_open. I'm now having a problem with my proc_open, I've been trying to find the solution but I couldn't.

Here's my PHP code:

PHP Code:
<?php 

$descriptorspec 
= array(
   
=> array("pipe""r"),
   
=> array("pipe""w"),
   
=> array("file""error-output.txt""a")
);

$process proc_open('python HelloPython.py'$descriptorspec$pipes);
 
$input 20;
$exp 2;

if (
$process) { 
    echo 
"Succeed!";
} else { 
    echo 
"Failed"
}  

?>


and my Python script is like this:

Code:
input = raw_input('Enter name: ')
print input

when I run the script, it echos "Failed" to me, that means something might wrong on '$process'.

Can someone help me figure out this problem, please

Reply With Quote
  #2  
Old March 7th, 2013, 03:34 PM
gw1500se gw1500se is online now
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,877 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 2 Weeks 13 h 37 m 3 sec
Reputation Power: 581
PHP Code:
if (is_resource($process)) { 
    echo 
"Succeed!";
} else { 
    echo 
"Failed"

__________________
There are 10 kinds of people in the world. Those that understand binary and those that don't.

Reply With Quote
  #3  
Old March 7th, 2013, 03:51 PM
requinix's Avatar
requinix requinix is online now
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,698 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 4 h 52 m 36 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
Do you have error_reporting turned up and display_errors on? Maybe there's an error message to read.

Reply With Quote
  #4  
Old March 7th, 2013, 04:05 PM
MynE MynE is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 35 MynE User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 32 m 59 sec
Reputation Power: 1
Thank you for your reply,

i did try is_resource as well, but the result still "Failed".

@requinix - No I don't have and error_message to read, how to create and get that?

Here's the original code I got from you:

PHP Code:
<?php 

$descriptorspec 
= array(
   
=> array("pipe""r"),
   
=> array("pipe""w"),
   
=> STDOUT
);

$process proc_open('python HelloPython.py'$descriptorspec$pipes);
 
$input 'HelloPython!!';

if (
is_resource($process)) { 
    echo 
"Succeed!";
} else { 
    echo 
"Failed"
}  

?>


the result is still the same, I'm kinda confused now. Many examples about proc_open are similar to this as well.

Reply With Quote
  #5  
Old March 7th, 2013, 04:40 PM
requinix's Avatar
requinix requinix is online now
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,698 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 4 h 52 m 36 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
Try
PHP Code:
<?php  

error_reporting
(-1);
ini_set("display_errors"true);

$descriptorspec = array( 
   
=> array("pipe""r"), 
   
=> array("pipe""w"), 
   
=> STDOUT 
); 

$process proc_open('python HelloPython.py'$descriptorspec$pipes); 
  
$input 'HelloPython!!'

if (
is_resource($process)) {  
    echo 
"Succeed!"
} else {  
    echo 
"Failed";  
}   

?>

Reply With Quote
  #6  
Old March 7th, 2013, 04:49 PM
MynE MynE is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 35 MynE User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 32 m 59 sec
Reputation Power: 1
Ok thank you, now I can read an error message,

Notice: Use of undefined constant STDOUT - assumed 'STDOUT' in /home/myne/website/index.php on line 17

Warning: proc_open() [function.proc-open]: Descriptor item must be either an array or a File-Handle in /home/myne/website/index.php on line 19
Failed

I still don't get it, can you explain what happened about this error to me please?

Reply With Quote
  #7  
Old March 7th, 2013, 07:20 PM
MynE MynE is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 35 MynE User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 32 m 59 sec
Reputation Power: 1
Oh I get it now. it is because my STDERR, so I've changed it like this:

PHP Code:
 $descriptorspec = array(  
   
=> array("pipe""r"),  
   
=> array("pipe""w"),  
   
=> array('pipe''a'
);  

$process proc_open('python HelloPython.py'$descriptorspec$pipes);  
   
$input 'MynE'

if (is_resource($process)) {   
    
fwrite($pipes[0], $input); 
    
fclose($pipes[0]); 
    
$output stream_get_contents($pipes[1]); 
    
fclose($pipes[1]); 

    
print_r($output); 
    
proc_close($process);   
} else {   
    echo 
"Failed";   



So it works now, but the result i got is "Enter your name:MynE!" which is the first line of my python script:

Code:
input = raw_input("Enter your name:")
print input


As you can see, the "print input" line doesn't show on my screen. How can I check that the value really gets in? because the result should be like this:

Enter your name:MynE!
MynE!

as followed to my python script.

Reply With Quote
  #8  
Old March 7th, 2013, 08:18 PM
requinix's Avatar
requinix requinix is online now
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,698 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 4 h 52 m 36 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
That's not actually how stdin and stdout work. When you run the script yourself and type in your name, the terminal you're using will "echo" your input. So you see what you're typing. It will also echo the newline character you type at the end.

If you want to replicate that behavior you have to do the echoing yourself: read from the process (the name prompt), write the name to it, append that name to the output to simulate the echo, also append the newline, and continue reading from the process. Basically you repeat the "read, append input, read" cycle every time you want to write to the process.

Separately, by including stderr as a pipe in the descriptorspec you need to read from and close it too. STDERR should work as PHP is supposed to create it for you, but you can use php://stderr if that doesn't work.
PHP Code:
 $descriptorspec = array(
   
=> array("pipe""r"),
   
=> array("pipe""w"),
   
// 2 => array("pipe", "w")
   
=> (defined("STDERR") && STDERR STDERR fopen("php://stderr""w"))
);

$process proc_open('python HelloPython.py'$descriptorspec$pipes);

$input 'MynE';

if (
is_resource($process)) {
    
$output stream_get_contents($pipes[1]);
    
fwrite($pipes[0], $input PHP_EOL); $output .= $input PHP_EOL;
    
fclose($pipes[0]);
    
$output .= stream_get_contents($pipes[1]);
    
fclose($pipes[1]);
    
// if you use the pipe then uncomment:
    // stream_get_contents($pipes[2]);
    // fclose($pipes[2]);

    
print_r($output);
    
proc_close($process);
} else {
    echo 
"Failed";


[edit] Normally you would need to write a newline to the pipe as well, but closing the pipe immediately after may also work for you (depends how the input is read and dealt with in the script). Including that newline is a good idea.

Last edited by requinix : March 7th, 2013 at 08:21 PM.

Reply With Quote
  #9  
Old March 12th, 2013, 11:04 PM
MynE MynE is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 35 MynE User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 32 m 59 sec
Reputation Power: 1
I'm sorry to bother you again, but I quite don't get it yet. How can we be sure that the data is actually getting in. Suppose that i'm writing 2 data into pipes. The result i got is

"Enter name here:MynE23 Enter age:"

PHP Code:
<?php 

$descriptorspec 
= array(   
   
=> array("pipe""r"),   
   
=> array("pipe""w"),   
   
=> array('pipe''a')
);   

$process proc_open('python HelloPython.py'$descriptorspec$pipes);   

$input "MynE";
$input2 "23";
    
if (
is_resource($process)) {
    
    
fwrite($pipes[0], $input);
    
fwrite($pipes[0], $input2);
    
fclose($pipes[0]);
  
    echo 
stream_get_contents($pipes[1]);
    
fclose($pipes[1]);
    
fclose($pipes[2]);
    
proc_close($process);    
} else {    
    echo 
"Failed";    
}


python code:

Code:
input = raw_input("Enter name here:")
print input

input2 = raw_input("Enter age:")
print input2

print "Your name is" + input + "and your age is"+ "input2"


Should I execute(py script after I put the data in pipe? or just proc_open to feed data into stdin is enough?)

What I expect after running is it should reach to the last line of my python script. I'm sorry for asking you again but I don't get it really.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Proc_open problem

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap