[Tutor] Problem Passing VARs to Python from PHP & capturing return string
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
> 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
> C:\Program%20Files\Python26\python.exe /c
> 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
> 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.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 )
> 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]
> _result = str(weatherSmall(city,state))
> #print _result
> 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.
More information about the Tutor