[Tutor] Problem Passing VARs to Python from PHP & capturing return string
rhinkelman at worldtradepress.com
Tue Oct 26 20:54:04 CEST 2010
I am posting here as well as a PHP list since I am now getting an odd python
Rance: Thanks for the note. I get the same error with system or exec or
Now, this is very strange.
I made the command line string more explicit, and now it recognizes the .py
script but returns a python error:
Data: ImportError: No module named mechanize
Command: C:\WINDOWS\system32\cmd.exe C:\Program%20Files\Python26\python.exe
/c D:\Inetpub\AtoZ\hometown\include\weatherFeed.py -c Tampa -s FL
My python script will run correctly in IDE shell and from command line. I
used easy_install to install mechanize. It seems to be recognizing my other
modules (BeautifulSoup, re, urllib2, sys).
So, it seems that the script is being activated by the exec command since I
am now getting a python error. But, why would I get a python error when it
will run from command line?
Here is my PHP:
$city = 'Tampa';
$state = 'FL';
echo '<p>OPTION 1<p>Python Weather Feed for ' . $city . ',
' . $state . '<p>';
$command = "C:\WINDOWS\system32\cmd.exe
D:\Inetpub\AtoZ\hometown\include\weatherFeed.py -c " . $city . " -s " .
$data = exec($command . " 2>&1", $result);
echo 'Content: ' . $content . '<br>';
echo 'Result: ' . $result . '<br>';
echo 'Data: ' . $data . '<br>';
echo 'Command: ' . $command . '<br>';
and I've modified my python, making it a function
#!C:\Program Files\Python26\python.exe -u
# Current weather feed
# # Weather page sources: http://www.weather.gov/
# Capture relevant data, strip unusable stuff out, fix URLs, display in our
# import program modules
from BeautifulSoup import BeautifulSoup as B_S
import re, urllib2, sys, mechanize # 'standard' set
### Return small weather table
cityState = c + ', ' + s
query = 'Chicago, IL' #Test!!!
_URL = "http://www.weather.gov/"
br.open( _URL )
br.select_form( nr=1 ) #assuming form is 2nd form on page
br['inputstring'] = query
html = br.submit()
_soup = B_S(html)
# finding the correct table
_step1 = _soup.findAll('table', limit=7)
col = _step1.findAll('td')
# sys.argv is included below as a test.
_thumb = '<table><tr><td colspan=2>Forecast for ' + query + '<br>' +
str(sys.argv) + '</td></tr><tr>' + str(col) + str(col) +
_thumb = _thumb.replace( '11%','50%' )
_thumb = _thumb.replace( '/images/', 'images/weather/' )
#write to txt file TEST
_temp = 'D:\\Inetpub\\AtoZ\\hometown\\include\\weatherFeed_TEMP.txt'
temp = open( _temp, 'w' )
temp.write( _thumb )
city = 'Establish'
state = 'Variables'
count = 0
for ea in sys.argv:
if ea == '-c':
city = sys.argv[count+1]
elif ea == '-s':
state = sys.argv[count+1]
_result = str(weatherSmall(city,state))
Website Development & Management
roy at worldtradepress.com
www.WorldTradePress.com (main website)
www.StockMapAgency.com (3700+ Antique & Modern Maps)
www.BestCountryReports.com (country reports for 175 countries)
www.GiantMapArt.com (giant wall maps)
www.WorldTradeRef.com (trade and logistics)
www.GlobalRoadWarrior.com (175-country database)
www.AtoZMapsOnline.com (worlds largest map database)
www.AtoZtheUSA.com (extensive state facts database)
From: Rance Hall [mailto:ranceh at gmail.com]
Sent: Saturday, October 23, 2010 10:25 AM
To: roy at worldtradepress.com
Subject: Re: [Tutor] Problem Passing VARs to Python from PHP & capturing
On Fri, Oct 22, 2010 at 1:28 PM, Roy Hinkelman
<rhinkelman at worldtradepress.com> wrote:
> My script doesn't want to recognize the variables from the exec() command
> PHP. Plus, it won't capture the results of the script.
> This Python script works in IDLE, and I've got some testing code in there.
> One Known Unsolved Issue:
> I put Python in C:\Program Files\Python26\python.exe and have tried
> $command = "C:\Program Files\Python26\python.exe include/weatherFeed.py -c
> $city -s $state";
> to no avail.
> I added .py to Windows IIS Web Service Extensions and to the Application
> Any help appreciated.
<code snipped for brevity>
Ok, so you wrote a Python script to create a text file, and you want
PHP to include that text file in a web page.
I'm assuming that your Python is working on its own.
I also see you are running this from a windows server.
According to the PHP site the exec function takes option arguments
that you are not using:
string exec ( string $command [, array &$output [, int &$return_var ]] )
Might I suggest that you switch from exec to system()
documented here: http://www.php.net/manual/en/function.system.php
string system ( string $command [, int &$return_var ] )
Since it doesn't appear that your Python script has any output except
for the text file, it might be better to call it from system() because
system() does not care about the output of the called program.
May I also suggest that you actually track the return_var somewhere.
You can check the status of the return_var to ensure that the system()
call competed successfully or not before including the text file, on
the off chance that it isn't there.
Someone else has already suggested to you that this is a Python list
and you should probably take this to the PHP list. I agree, but the
teacher in me just couldn't leave the answer as "take it to another
forum" without some guidance. Good luck.
No virus found in this incoming message.
Checked by AVG - www.avg.com
Version: 9.0.862 / Virus Database: 271.1.1/3218 - Release Date: 10/24/10
More information about the Tutor