[Tutor] Problem Passing VARs to Python from PHP & capturing return string

Roy Hinkelman rhinkelman at worldtradepress.com
Fri Oct 22 20:28:31 CEST 2010


My script doesn't want to recognize the variables from the exec() command in
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
Configuration.

Any help appreciated.

[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
import mechanize, re, urllib2, sys # 'standard' set
from BeautifulSoup import BeautifulSoup as B_S

query = 'Chicago, IL' #test to make sure script works!
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
cityState = city + ', ' + state

_URL = "http://www.weather.gov/"
#_URL =
"http://forecast.weather.gov/MapClick.php?CityName=Novato&state=CA&site=MTR&
lat=38.1032&lon=-122.63"

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')
_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()

#print _thumb

[/code]

And my PHP:

[code]

<?PHP
      $city = 'Tampa';
      $state = 'FL';
      echo  '<p><p>Python Weather Feed for ' . $city . ', ' . $state .
'<p>';

      ob_start();
      $command = "include/weatherFeed.py -c " . $city . "-s " . $state;
      exec($command);
      $content=ob_get_contents();
      ob_end_clean();
      
      echo 'Content: ' . $content . '<br>';
      echo 'Result: ' . $result . '<br>';
      echo 'Data: ' . $data . '<br>';
      include('include\weatherFeed_TEMP.txt')
   ?>

 

 

Roy Hinkelman
Technical Services
Website Development & Management
707-774-7411
 <mailto:roy at worldtradepress.com> 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)
 <http://www.AtoZtheUSA.com> www.AtoZtheUSA.com (extensive state facts
database)

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20101022/4c9b4266/attachment.html>


More information about the Tutor mailing list