[Tutor] Problem Passing VARs to Python from PHP & capturing return string
Dave Angel
davea at ieee.org
Wed Oct 27 14:42:33 CEST 2010
On 2:59 PM, Roy Hinkelman wrote:
> I am posting here as well as a PHP list since I am now getting an odd python
> error.
>
> Rance: Thanks for the note. I get the same error with system or exec or
> passthru
>
> 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:
>
> Content:
> 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:
> [code]
> <?PHP
> error_reporting(E_ALL);
>
> $city = 'Tampa';
> $state = 'FL';
> echo '<p>OPTION 1<p>Python Weather Feed for ' . $city . ',
> ' . $state .'<p>';
>
> ob_start();
> $command = "C:\WINDOWS\system32\cmd.exe
> C:\Program%20Files\Python26\python.exe /c
> D:\Inetpub\AtoZ\hometown\include\weatherFeed.py -c " . $city . " -s " .
> $state;
>
> $data = exec($command . " 2>&1", $result);
> $content=ob_get_contents();
> ob_end_clean();
>
> echo 'Content: ' . $content . '<br>';
> echo 'Result: ' . $result . '<br>';
> echo 'Data: ' . $data . '<br>';
> echo 'Command: ' . $command . '<br>';
> include('include\weatherFeed_TEMP.txt')
> ?>
> [/code]
>
> and I've modified my python, making it a function
> [code]
> #!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
> HTML page
>
> # import program modules
> from BeautifulSoup import BeautifulSoup as B_S
> import re, urllib2, sys, mechanize # 'standard' set
>
> ### Return small weather table
> def weatherSmall(c,s):
> cityState = c + ', ' + s
> query = 'Chicago, IL' #Test!!!
> _URL = "http://www.weather.gov/"
>
> br=mechanize.Browser()
> 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)[6]
> 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[0]) + str(col[1]) +
> '</tr></table>'
> _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 )
> temp.close()
>
> return _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]
> count+=1
> _result = str(weatherSmall(city,state))
> #print _result
> [/code]
>
> Roy Hinkelman
>
I don't know anything about PHP, so take the following for what it's worth.
So where is mechanize.py ? The import error means that it can't be
found. Yet if you run the program standalone it's finding it? My guess
is that it depends on the current directory when you enter the script.
First thing to do is to split the import line, so that the import
mechanize follows the others on its own line. Then you can put some
debugging lines in between them, perhaps saving off the os.curdir and
sys.path values. I suspect the difference between running under php and
from command line is in one of those.
DaveA
More information about the Tutor
mailing list