Shell Script on iMac no longer working with High Sierra - python

I recently upgraded my iMac 27” (mid-2011) from Yosemite to High Sierra and I am struggling to get back some functionality that I had working previously!
To briefly explain… First of all, I grab local weather data from weather underground using some Python scripts on a Raspberry pi3. These scripts also massage the data and then create and store an XML file on the pi. I also, on the pi, run a http server that looks for calls.
On an iPad, using iRule, I have a button that is called ‘Weather Forecast’. When this button is pressed it triggers a network resource on my ISY994i (Insteon) controller that, in turn, makes a call to the http server on the pi sending it a parameter. When the pi receives the call and validates the parameter, it runs another Python script (on the pi) that takes the data in the previously created XML file and puts it into a proper format for the next step. Finally, that script sends GET requests to the iMac, through Apache2, to read the weather forecast out loud.
This was working very well on Yosemite but now that I have upgraded the saying part is not working!
I have 3 shell scripts on the iMac that are called, from the pi, in this process…
saysomethinghttp9a.sh This is the first script called which reads the current volume level and stores it in a local file (on the iMac); then it changes the volume level to an acceptable volume (I use 18);
!/bin/bash
echo -e "Content-type: text/html\n"
PHRASE=`echo "$QUERY_STRING" | sed -n 's/^.*phrase=\([^&]*\).*$/\1/p' | sed "s/+/ /g" | sed "s/%20/ /g"`
cat << junk
<html>
<head>
<title>
saying
</title>
</head>
<body>
junk
currVol=$(osascript -e "get volume settings")
echo "Current Volume Setting = $currVol"
var1=$( echo $currVol | cut -d":" -f1 )
var2=$( echo $currVol | cut -d":" -f2 )
origVol=$( echo $var2 | cut -d"," -f1 )
echo $origVol
parm="set volume output volume $origVol"
echo $parm
destfile="/Users/Sarah/Sound_Volume/Volume1.txt"
echo $parm > $destfile
osascript -e "set volume output volume 18"
cat << junk
</body>
</html>
junk
saysomethinghttp9.sh After the volume level has been set, this script does the ‘say’ part based upon what is sent from the pi. The pi calls this script and sends a parameter, which is the words I want said. This call is repeated several times for the intro, date, time, weather forecast and closing; and
#!/bin/bash
echo -e "Content-type: text/html\n"
PHRASE=`echo "$QUERY_STRING" | sed -n 's/^.*phrase=\([^&]*\).*$/\1/p' | sed "s/+/ /g" | sed "s/%20/ /g"`
cat << junk
<html>
<head>
<title>
saying
</title>
</head>
<body>
junk
say "Hey There"
cat << junk
</body>
</html>
junk
saysomethinghttp9b.sh Finally this last script is called, which reads the original volume from the file created in the first step and then resets the volume to that level.
#!/bin/bash
echo -e "Content-type: text/html\n"
cat << junk
<html>
<head>
<title>
saying
</title>
</head>
<body>
junk
file="/Users/Sarah/Sound_Volume/Volume1.txt"
echo $file
read -d $'\x04' parm < "$file"
echo $parm
osascript -e "$parm"
cat << junk
</body>
</html>
junk
(note that I go through the steps to adjust the volume because the volume for music, from iTunes, is much too loud for the ‘say’ commands)
In trying to figure out what is wrong I have tried numerous things:
I edited the script saysomethinghttp9.sh to eliminate the ‘say’ of a parameter passed to it and simply put in the line say “Hey there” (note that the code above is the edited version)
I then opened up a terminal session on the iMac and issued the commands from there...
./saysomethinghttp9a.sh
./saysomethinghttp9.sh
./saysomethinghttp9b.sh
All 3 scripts worked when called from the terminal so that wasn’t the problem.
To debug the calls to the iMac, I simplified the process by eliminating the iPad, the pi and the ISY994i from the process. Instead, I have been trying to make the calls to the iMac from a PC on the same network using a browser.
http://10.0.1.11/cgi-bin/saysomethinghttp9a.sh
http://10.0.1.11/cgi-bin/saysomethinghttp9.sh
http://10.0.1.11/cgi-bin/saysomethinghttp9a.sh
The result from running the scripts directly from the browser, on the PC, was that script saysomethinghttp9a.sh and saysomethinghttp9b.sh worked but saysomethinghttp9.sh did not!
Here are the Access and Error log entries from the iMac after trying the calls from the browser on the PC…
Access Log
10.0.1.195 - - [18/Dec/2017:21:33:30 -0500] "GET /cgi-bin/saysomethinghttp9a.sh HTTP/1.1" 200 197
10.0.1.195 - - [18/Dec/2017:21:34:04 -0500] "-" 408 -
10.0.1.195 - - [18/Dec/2017:21:33:44 -0500] "GET /cgi-bin/saysomethinghttp9.sh HTTP/1.1" 200 53
10.0.1.195 - - [18/Dec/2017:21:33:49 -0500] "GET /cgi-bin/saysomethinghttp9.sh HTTP/1.1" 200 53
10.0.1.195 - - [18/Dec/2017:21:35:05 -0500] "GET /cgi-bin/saysomethinghttp9b.sh HTTP/1.1" 200 135
Error Log
[Mon Dec 18 21:34:44.356130 2017] [cgi:warn] [pid 29997] [client 10.0.1.195:60109] AH01220: Timeout waiting for output from CGI script /Library/WebServer/CGI-Executables/saysomethinghttp9.sh
[Mon Dec 18 21:34:44.356519 2017] [core:error] [pid 29997] (70007)The timeout specified has expired: [client 10.0.1.195:60109] AH00574: ap_content_length_filter: apr_bucket_read() failed
[Mon Dec 18 21:34:49.949284 2017] [cgi:warn] [pid 29575] [client 10.0.1.195:60107] AH01220: Timeout waiting for output from CGI script /Library/WebServer/CGI-Executables/saysomethinghttp9.sh
[Mon Dec 18 21:34:49.949652 2017] [core:error] [pid 29575] (70007)The timeout specified has expired: [client 10.0.1.195:60107] AH00574: ap_content_length_filter: apr_bucket_read() failed
For full disclosure, my programming experience is relatively limited. I often piece things together using examples that I find online.
I do not know how to interpret the errors noted above! The only information I could find related to "The timeout specified has expired" was related to situations where a lot of data was being dealt with! In my case, there is very little data being processed!
I would appreciate some help or direction on how to proceed.
Edit:
After reading the comments from Mark Setchell, I added into my script the '/usr/bin/id' and ran the script first in the terminal and saw that user name was correct. Then I ran the same script from the other PC and saw that the user name was '_www'! So I then edited the httpd.conf (apache2) file and changed the section include User Sarah and Group staff. However this did not correct the problem!
Next I read up on how to 'use su to become that user and try the script'. Through the readings I kept finding suggestions to use sudo instead and finally found a suggestion to edit the sudoers file. So I did this using the command sudo visudo. Then I added in the following line
Sarah ALL=(ALL) NOPASSWD: ALL
Then I tried running the script from the PC once again however this time the script ran and is saying again!

After reading the comments from Mark Setchell, I added into my script the '/usr/bin/id' and ran the script first in the terminal and saw that user name was correct. Then I ran the same script from the other PC and saw that the user name was '_www'! So I then edited the httpd.conf (apache2) file and changed the section include User Sarah and Group staff. However this did not correct the problem!
Next I read up on how to 'use su to become that user and try the script'. Through the readings I kept finding suggestions to use sudo instead and finally found a suggestion to edit the sudoers file. So I did this using the command sudo visudo. Then I added in the following line
Sarah ALL=(ALL) NOPASSWD: ALL
Then I tried running the script from the PC once again however this time the script ran and is saying again!

Related

Bus stops and routes in SUMO

I recently started using SUMO traffic simulator and I am trying to simulate an intermodal network.
I've been able of launch a simulation with cars and buses using vTypeDistribution and randomTrips.py.
Now I am trying to simulate a realistic public transport network using ptlines2flows.py.
I am able to obtain all the bus stops and trips, but not to obtain the routes. In fact I get multiple warnings like this:
Warning: busStop '25902051' for vehicle '55_subway_L4:0' on lane '497770366_0' is not downstream the current route.
and 0 vehicles are loaded. Also, I get this error:
Error: Vehicle '39_bus_73:0' is not allowed on any lane of via edge '45100166#0'.
It seems that the program tries to put a bus in metro/underground lines.
I attach my bash script where I do everything:
#!/bin/bash
source autosumo_parameters.sh
# 1. Links and folder creations
mkdir -p ${folder}
cd ${folder}
ln -sf ../$map . # Link to where is the map (downloaded manually at the moment)
rm ${conf_file}
# 2. Create SUMO network from .osm map
#netconvert --osm-files ${map} --output-file ${network} --geometry.remove --roundabouts.guess --ramps.guess --junctions.join --tls.guess-signals --tls.discard-simple --tls.join
netconvert --osm-files ${map} --output-file ${network} --osm.stop-output.length 15 --ptstop-output ${ptstops_out} --ptline-output ${ptlines_out} --osm.elevation --geometry.remove --roundabouts.guess --ramps.guess --junctions.join --tls.guess-signals --tls.discard-simple --tls.join
# 2.2 Create public transport schedules
python3 /home/alejandro/Documentos/sumoDocs/pythonScripts/ptlines2flows.py -n ${network} -s ${ptstops_out} -l ${ptlines_out} -o ${ptroutes} -p 300 --min-stops 1 --use-osm-routes --verbose
#python3 $SUMO_HOME/tools/ptlines2flows.py -n ${network} -s ${ptstops_out} -l ${ptlines_out} -o ${ptroutes} -p 300 --min-stops 1 --use-osm-routes --verbose
# 3. Create SUMO simulation configuration file
echo "<?xml version='1.0' encoding='iso-8859-1'?>
<configuration xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='http://sumo.dlr.de/xsd/duarouterConfiguration.xsd'>
<input>
<net-file value='${network}'/>
<route-files value='${ptroutes}'/>
<additional-files value='${ptstops_out}'/>
</input>
<output>
<emission-output value='${emi_out}'/>
<fcd-output value='${fcd_out}'/>
</output>
<time>
<begin value='${begin_time}'/>
<end value='${end_time}'/>
</time>
<processing>
<time-to-teleport value='${teleport}'/>
</processing>
</configuration>" > $conf_file
#fi
#sumo -c $conf_file --fcd-output $fcd_out --emission-output $emi_out --device.emissions.period "20.0"
sumo -c $conf_file --ignore-route-errors
echo " \n sumo done"
# Open the configuration file in sumo-gui to launch the simulation
#sumo-gui $conf_file
# traceExporter.py converts fcd-output file to different file formats: OMNET, Shawn, ns2/ns3, PHEM
python3 $SUMO_HOME/tools/traceExporter.py --fcd-input $fcd_out --omnet-output fcd_to_omnet.xml #--kml-output fcd_to_kml.xml
Would you be so kind to help me? I would really appreciate it, I've been stucked with this a few days...
I got it finally, the problem was in the network and lanes permissions. Apparently, if you import an OpenStreetMap file, SUMO assignes permissions to highway.services road type only to bicicles, pedestrians and delivery vehicles.
To modify this, you have to change the road access permissions in the typemap file you are using. Those are located in $SUMO_HOME/data/typemap. The file used by default is called osmNetconvert.typ.xml, then you can change the permissions adding or removing vehicle type, it looks like this:
<type id="highway.service" numLanes="1" speed="5.56" priority="1" oneway="false" allow="delivery pedestrian bicycle bus"/>
Once the permissions are correct, there was no problem running the simulation.

problem executing a python cgi script in apache webserver

I have searched and read other posts with similar problems. I fixed the shebang line in the .py file and made sure that httpd.conf has correct config. Unfortunately, nothing has resolved my problem and I still get the dreaded errors -
[Mon Jun 01 10:37:02.994516 2020] [cgi:error] [pid 19596:tid 1196] (OS 1920)The file cannot be accessed by the system. : [client ::1:50159] couldn't create child process: 721920: upload_file.py
[Mon Jun 01 10:37:02.994516 2020] [cgi:error] [pid 19596:tid 1196] (OS 1920)The file cannot be accessed by the system. : [client ::1:50159] AH01223: couldn't spawn child process: C:/Users/raj_d/webroot/tsp_quick/cgi-bin/upload_file.py
The Python script -
#!"C:\Users\raj_d\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\python.exe"
import cgi, os
import cgitb
cgitb.enable()
form = cgi.FieldStorage()
# Get filename here.
fileitem = form['filename']
# Test if the file was uploaded
if fileitem.filename:
# strip leading path from file name to avoid
# directory traversal attacks
fn = os.path.basename(fileitem.filename)
open('/tmp/' + fn, 'wb').write(fileitem.file.read())
message = 'The file "' + fn + '" was uploaded successfully'
else:
message = 'No file was uploaded'
print """\
Content-Type: text/html\n
<html>
<body>
<p>%s</p>
</body>
</html>
""" % (message,)
I have played with adding and removing quotes on the full path in the shebang and that hasn't helped.
I have these in httpd.conf -
LoadModule cgi_module modules/mod_cgi.so
<Directory "C:\Users\raj_d\webroot\tsp_quick\cgi-bin">
AllowOverride None
# Options None
Options +ExecCGI
AddHandler cgi-script .py
Require all granted
</Directory>
I made sure that python was excutable from the shebang path -
C:\>C:\Users\raj_d\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\python.exe
Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:37:02) [MSC v.1924 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
Any help will be appreciated.
Thanks in advance.
RD
Well, I figured it out.
First and foremost, one should always follow the guidelines :)
In my case, I gave up too quickly in running the script from command line. I had installed Python 3.8.3 from the Microsoft store and for some reason every time I ran the script from the command line, a new cmd window would popup and close very quickly. I was unable to change that behavior and couldn't see what was going wrong.
So I uninstalled python, downloaded the new installer from python's site, and installed in a common location where my dev tools are installed. Now, I could run my script from command line and was able to see what the actual problem was.
It turned out to be print! Here is the modified code which works just fine and the shebang became simpler as well.
#!C:\tools\python\\python.exe
import cgi, os
import cgitb
import time
cgitb.enable()
form = cgi.FieldStorage()
# Get filename here.
fileitem = form['filename']
# Test if the file was uploaded
if fileitem.filename:
# strip leading path from file name to avoid
# directory traversal attacks
fn = os.path.basename(fileitem.filename)
# open('/tmp/' + fn, 'wb').write(fileitem.file.read())
message = 'The file "' + fn + '" was uploaded successfully'
else:
message = 'No file was uploaded'
a = """Content-Type: text/html\n
<html>
<body>
<p>{}</p>
</body>
</html>
"""
print (a.format(message))
I had similar issues trying to execute *.py files in Apache on Windows. I followed the Apache CGI setup guide, but the problem (as you allude to) is with the Python executable in
C:/Users/../AppData/Local/Microsoft/WindowsApps/Python/
For some reason that I can't figure out, Apache won't allow you to use the Python interpreter in this location. I did as you suggested and uninstalled Python 3.10.x and re-installed on my D: drive and hey presto, it just worked.
I suspect this is a windows-only quirk, probably a security issue.

pysipp - Trying to use it with existing sipp conf file

Background
I have an existing sipp conf file that I launch like so:
sipp mysipdomain.net -sf ./testcall.conf -m 1 -s 12345 -i 10.1.1.1:5060
This runs just fine. It simulates a call in our test labs. But now I need to expand this test to make it a part of a larger test script where not only do I launch the sipp test, but I prove (via sip trace) that it's hitting the right boxes.
I decided to wrap this sipp call in python. I just found https://github.com/SIPp/pysipp and am trying to see if I can write this entire test in python. To start, i tried to run the same sipp test using pysipp.
Problem / Question
I'm currently getting an error that says:
lab2:/tmp/jj/sipp_tests# python mvv_numeric.py
No handlers could be found for logger "pysipp"
Traceback (most recent call last):
File "mvv_numeric.py", line 6, in <module>
uac()
File "/usr/lib/python2.7/site-packages/pysipp-0.1.alpha-py2.7.egg/pysipp/agent.py", line 71, in __call__
raise_exc=raise_exc, **kwargs
File "/usr/lib/python2.7/site-packages/pluggy-0.3.1-py2.7.egg/pluggy.py", line 724, in __call__
return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
File "/usr/lib/python2.7/site-packages/pluggy-0.3.1-py2.7.egg/pluggy.py", line 338, in _hookexec
return self._inner_hookexec(hook, methods, kwargs)
File "/usr/lib/python2.7/site-packages/pluggy-0.3.1-py2.7.egg/pluggy.py", line 333, in <lambda>
_MultiCall(methods, kwargs, hook.spec_opts).execute()
File "/usr/lib/python2.7/site-packages/pluggy-0.3.1-py2.7.egg/pluggy.py", line 596, in execute
res = hook_impl.function(*args)
File "/usr/lib/python2.7/site-packages/pysipp-0.1.alpha-py2.7.egg/pysipp/__init__.py", line 250, in pysipp_run_protocol
finalize(cmds2procs, raise_exc=raise_exc)
File "/usr/lib/python2.7/site-packages/pysipp-0.1.alpha-py2.7.egg/pysipp/__init__.py", line 228, in finalize
raise SIPpFailure(msg)
pysipp.SIPpFailure: Some agents failed
'uac' with exit code 255 -> Command or syntax error: check stderr output
Code
Here's what the py script looks like:
1 import pysipp
2 uac = pysipp.client(destaddr=('mysipdomain.net', 5060))
3 uac.uri_username = '12345'
4 uac.auth_password = ''
5 uac.scen_file = './numeric.xml'
6 uac()
And the original sipp "testcall.conf" has been renamed to "numeric.xml" and looks like this: (I'm only including the first part because it's quite long. if you need to see something specific, please let me know and I will add to this post)
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE scenario SYSTEM "sipp.dtd">
<scenario name="UAC with Media">
<send retrans="10000">
<![CDATA[
INVITE sip:[service]#[remote_ip]:[remote_port] SIP/2.0
Via: SIP/2.0/[transport] [local_ip]:[local_port];branch=[branch]
From: sipp <sip:sipp#[local_ip]:[local_port]>;tag=[pid]SIPpTag00[call_number]
To: [service] <sip:[service]#[remote_ip]:[remote_port]>
Call-id: [call_id]
CSeq: 1 INVITE
Contact: <sip:sipp#[local_ip]:[local_port]>
Allow: INVITE, ACK, BYE, CANCEL, OPTIONS, INFO, MESSAGE, SUBSCRIBE, NOTIFY, PRACK, UPDATE, REFER
User-Agent: PolycomVVX-VVX_300-UA/5.5.2.8571
Accept-Language: en
Supported: replaces,100rel
Allow-Events: conference,talk,hold
Max-Forwards: 70
Content-Type: application/sdp
Content-Length: [len]
I'm sure it's something simple I've overlooked. Any pointers would be appreciated.
EDIT:
I added debug level logging and reran the python script. In the logs I can now see what pysipp is actually attempting:
2018-01-31 14:40:32,715 MainThread [DEBUG] pysipp launch.py:63 : launching cmd:
"'/usr/bin/sipp' 'mysipdomain.net':'5060' -s '12345' -sn 'uac' -sf 'numeric.xml' -recv_timeout '5000' -r '1' -l '1' -m '1' -log_file '/tmp/uac_log_file' -screen_file '/tmp/uac_screen_file' -trace_logs -trace_screen "
So comparing that with the original command line I use to run sipp, I see the extra "-sn 'uac'".
Going to see about either getting my SIPP script to work with that tag or ... google to see if I can find other similar posts.
In the meantime, if you see my mistake, i'm all ears.
The problem here (as you noticed) is likely that pysipp.client() sets the -sn uac flag and sipp fails having both -sn and -sf.
To see the actual error you can enable logging before running the client:
import pysipp
pysipp.utils.log_to_stderr("DEBUG")
uac = pysipp.client(destaddr=('mysipdomain.net', 5060))
uac.uri_username = '12345'
uac.auth_password = ''
uac.scen_file = './numeric.xml'
uac()
The hack is to simply do uac.scen_name = None but the proper way to do this is to either use pysipp.scenario() (docs here) and rename your numeric.xml to have uac in the file name (i.e. uac_numeric.xml) or use instead pysipp.ua(scen_file=<path/to/numeric.xml>).
To understand the problem the client is currently applying a default scenario name argument when really the user should be able to override that (though in that case there'll be no guarantee that the user actually is sending client traffic which renders the name client kind of pointless).

My Python script hosted on OpenShift inside the .openshift/cron/minutely directory doesn't run. What's wrong?

I wrote the following script, which sends an email to a specific email address, and saved it inside the .openshift/cron/minutely directory:
import smtplib
g = smtplib.SMTP('smtp.gmail.com:587')
g.ehlo()
g.starttls()
g.ehlo()
g.login('myusername','mypassword')
g.sendmail('myemail','otheremail','message')
I then pushed the script to the server.
I expected the program to run once every minute, and receive an email every minute. However, there is no evidence indicating that my code is being run. Any idea what might be causing the problem? Did I forget a step while setting up my application?
Note: I've checked that the email address and password I provided were correct, and that cron is installed.
EDIT: It seems that the problem is originating from the server:
I deleted the original contents of the file, created 'testfile.txt', and wrote this code instead:
a = open('testfile.txt','r+')
if not a.read():
a.write('Test writing')
a.close()
after waiting for the code to run and ssh-ing into the server, I changed to the directory named app-root/logs and displayed the contents of cron.log, which looked something like this:
Sat Nov 8 11:01:11 EST 2014: START minutely cron run
__________________________________________________________________________
/var/lib/openshift/545a6ac550044652510001d3/app-root/runtime/repo//.openshift/cron/minutely/test_openshift.py:
/var/lib/openshift/545a6ac550044652510001d3/app-root/runtime/repo//.openshift/cron/minutely/test_openshift.py: line 1: syntax error near unexpected token `('
/var/lib/openshift/545a6ac550044652510001d3/app-root/runtime/repo//.openshift/cron/minutely/test_openshift.py: line 1: `a = open('testfile.txt','r+')'
__________________________________________________________________________
Sat Nov 8 11:01:11 EST 2014: END minutely cron run - status=0
__________________________________________________________________________
Could it be that the server is not interpreting the code in my file as python code? Any suggestions welcome.
connect to openshift console
rhc ssh app_name
Change to a directory to have permission to create script:
cd $OPENSHIFT_DATA_DIR
create test01.py script
touch test01.py
Give executing permission to test01.py
chmod +x test01.py
Edit script
nano test01.py
Add a simple code like
print("Hello")
run script:
./test01.py
Error:
./test01.py: line 1: syntax error near unexpected token `"Hello"'
./test01.py: line 1: `print("Hello")'
Now check python path
which python
Output
/var/lib/openshift/your-sesseion-id/python/virtenv/venv/bin/python
Now add a she bang to test01.py
#!/var/lib/openshift/your-sesseion-id/python/virtenv/venv/bin/python
print("Hello")
Now Execute it
./test01.py
Output:
Hello
Conclusion:
Your script should know how to run and where is python path, so add it at the first line of your script

Internal Server Error 500 - Python, CGI

My .py file executes ok in terminal, but gives this error in the browser
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>500 Internal Server Error</title>
</head><body>
<h1>Internal Server Error</h1>
...
...
Here is the .py file:
#!/usr/bin/python
import cgi
import cgitb; cgitb.enable()
print "Content-Type: text/html\n\n" # HTML is following
print # blank line, end of headers
print "<TITLE>CGI script output</TITLE>"
print "<H1>This is my first CGI script</H1>"
print "Hello, world!"
Should i be saving this as a .cgi file? I have tried with the same errors, i have tried many files like this and none work, i am sure the apache server is working as there are other .cgi scripts running from the same directory without issues.
I have also tried:
#!/usr/local/bin/python &
#!/usr/bin/local/python
Any help appreciated.
EDIT
error log output:
(2) No such file or directory: exec of '.../.../.../test.py' failed
Premature end of script headers: test.py
Here is something I wrote up a while ago. These are some good things to look for when troubleshooting Python CGI.
There are some tips to getting Python working in CGI.
Apache setup: This may be old
Add python as a CGI by modifying the following in the configuration:
Options Indexes FollowSymLinks ExecCGI
AddHandler cgi-script .cgi .py
Always browse the pages through Apache.
Note that viewing files in the filesystem through a browser works for most things on an html page but will not work for CGI. For scripts to work they must be opened through the htdocs file system. The address line of your browser should look like:
\\127.0.0.1\index.html or
\\localhost\index.html
If you open a file up through the file system the CGI will not work. Such as if this is in the location bar of your browser:
c:\Apache\htdocs\index.html (or some other example location)
Convert end of lines of scripts to Unix format:
Most editors have options to "show end of lines" and then a tool to convert from Unix to PC format. You must have the end of lines set to Unix format.
State the path to the Python interpreter on the first line of the CGI script:
You must have one of the following lines as the first line of your Python CGI script:
#!C:\Python25\Python.exe
#!/usr/bin/python
The top line is used when you are debugging on a PC and the bottom is for a server such as 1and1. I leave the lines as shown and then edit them once they are up on the server by deleting the first line.
Print a content type specifying HTML before printing any other output:
This can be done simply by adding the following line somewhere very early in your script:
print "Content-Type: text/html\n\n"
Note that 2 end of lines are required.
Setup Python scripts to give debugging information:
Import the following to get detailed debugging information.
import cgitb; cgitb.enable()
An alternative if cgitb is not available is to do the following:
import sys
sys.stderr = sys.stdout
On the server the python script permissions must be set to execute.
After uploading your files be sure to edit the first line and set the permissions for the file to execute.
Check to see if you can hit the python script directly. If you can't, fix with the above steps (2-6). Then when the Python script is working, debug the shtml.

Categories