dfernan
asked on
from python script to webservice
I'd like to create a very simple web service that exposes the functionality from my existing python script (attached here at the end) for use within my company... Please provide a solution and code, not links to other info.
Basically the simplest version could be this one:
INPUT: http://www.urladdress.com/mypythonwebservice/cc?region=global"e=goog
OUTPUT: price (523.07 for google stock last time I checked).
The script I want to provide as a service is:
# modules used in this function
import re
import sys
import os
import subprocess
import ystockquote
#download the file first
os.popen("curl -L http://www.bolsadesantiago.com/Theme/preciosacciones.aspx > precioacciones.aspx")
ticker = sys.argv[1]
ticker = str(ticker)
region = sys.argv[2]
region = str(region)
# This function takes as an input the name of a stock and region being traded,
# and goes to the acciones.aspx OR yahoo finance to find the price...
def ticker2price(ticker, region):
if region == "chile":
file = open("precioacciones.aspx" )
stockstxt = file.read()
pattern = "NEMO="+ticker+".*right\"> "
result = re.search(pattern, stockstxt)
price = stockstxt[result.end():res ult.end()+ 30]
price = price.split('<')[0]
price.replace( '.', '' ).replace( ',', '.' )
price = float( price.replace( '.', '' ).replace( ',', '.' ) )
return price
if region == "global":
return ystockquote.get_price(tick er)
price = ticker2price(ticker, region)
print price
NOW INSTEAD OF PRINT WANT THIS CODE TO BE A WEBSERVICE AS SPECIFIED ABOVE.
Please help me and provide code and final answer, not links to other resources.
Thanks!
Basically the simplest version could be this one:
INPUT: http://www.urladdress.com/mypythonwebservice/cc?region=global"e=goog
OUTPUT: price (523.07 for google stock last time I checked).
The script I want to provide as a service is:
# modules used in this function
import re
import sys
import os
import subprocess
import ystockquote
#download the file first
os.popen("curl -L http://www.bolsadesantiago.com/Theme/preciosacciones.aspx > precioacciones.aspx")
ticker = sys.argv[1]
ticker = str(ticker)
region = sys.argv[2]
region = str(region)
# This function takes as an input the name of a stock and region being traded,
# and goes to the acciones.aspx OR yahoo finance to find the price...
def ticker2price(ticker, region):
if region == "chile":
file = open("precioacciones.aspx"
stockstxt = file.read()
pattern = "NEMO="+ticker+".*right\">
result = re.search(pattern, stockstxt)
price = stockstxt[result.end():res
price = price.split('<')[0]
price.replace( '.', '' ).replace( ',', '.' )
price = float( price.replace( '.', '' ).replace( ',', '.' ) )
return price
if region == "global":
return ystockquote.get_price(tick
price = ticker2price(ticker, region)
print price
NOW INSTEAD OF PRINT WANT THIS CODE TO BE A WEBSERVICE AS SPECIFIED ABOVE.
Please help me and provide code and final answer, not links to other resources.
Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
assumed i ma using a windows server while i explicitly mentioned I was using a unix server.
I am sorry I missed the followup comment. On a unix or linux server you can do this like this:
Use your according package manager and install mod_python by typing the following into your shell:
Debian/Ubuntu
Open /etc/apache2/sites-availab le/default (make sure to make backup of configuration file) and add:
After that restart apache by typing:
Fedora/CentOS
Open /etc/httpd/conf.d/python.c onf (make sure to make backup of configuration file) add:
After that restart apache by typing:
If you added the the first configuration using the Publisher Handler you will be able to run files ending with *.py directly from /var/www/html/foo.py.
If you added the second configuration using PSP you can use python directly in html code called like this /var/www/html/foo.psp
Use your according package manager and install mod_python by typing the following into your shell:
#On debian/
sudo apt-get install libapache2-mod-python
#on fedora/centOS
sudo yum install mod_python
Debian/Ubuntu
Open /etc/apache2/sites-availab
#add either this if you want to use the Publisher Handler e.g. run python scripts directly
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
AddHandler mod_python .py
PythonHandler mod_python.publisher
PythonDebug On
</Directory>
#or this if you want to use the PSP Handler e.g. run python inside html
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
AddHandler mod_python .psp
PythonHandler mod_python.psp
PythonDebug On
</Directory>
After that restart apache by typing:
# if you are using standard apache installation
/etc/init.d/httpd restart
#if you are using lampp/xampp
/opt/lampp/lampp stop
/opt/lampp/lampp start
Fedora/CentOS
Open /etc/httpd/conf.d/python.c
#add either this if you want to use the Publisher Handler e.g. run python scripts directly
LoadModule python_module modules/mod_python.so
<Directory /var/www/html/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
AddHandler mod_python .py
PythonHandler mod_python.publisher
PythonDebug On
</Directory>
#or this if you want to use the PSP Handler e.g. run python inside html
LoadModule python_module modules/mod_python.so
<Directory /var/www/html/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
AddHandler mod_python .psp
PythonHandler mod_python.psp
PythonDebug On
</Directory>
After that restart apache by typing:
# if you are using standard apache installation
/etc/init.d/httpd restart
#if you are using lampp/xampp
/opt/lampp/lampp stop
/opt/lampp/lampp start
If you added the the first configuration using the Publisher Handler you will be able to run files ending with *.py directly from /var/www/html/foo.py.
If you added the second configuration using PSP you can use python directly in html code called like this /var/www/html/foo.psp
ASKER