scheibo
June 30th, 2009, 05:24 AM
I've been trying to do CGI scripting in python on my linux, but I have problems getting it to work. I'm trying to follow "Programming Python" (Mark Lutz, 3rd edition), and i'm using the web server program he offers on page 968:
#!/usr/bin/python
#python server (968)
webdir = '.' #where html and cgi-bin files live
port = 80 #use http://localhost/
import os, sys
from BaseHTTPServer import HTTPServer
from CGIHTTPServer import CGIHTTPRequestHandler
if len(sys.argv) > 1: webdir = sys.argv[1]
if len(sys.argv) > 2: port = int(sts.argv[2])
print 'webdir: "%s", port %s' % (webdir, port)
#windows hack, os.environ not propagated
if sys.platform[:3] == 'win':
CGIHTTPRequestHandler.have_popen2 = False
CGIHTTPRequestHandler.have_popen3 = False #emulate path after fork
sys.path.append('cgi-bin') #else only adds my dir
os.chdir(webdir) #run in HTML root dir
srvraddr = ("", port) #my hostname, port number
srvrobj = HTTPServer(srvraddr, CGIHTTPRequestHandler)
srvrobj.serve_forever() #serve clients till exit
this script resides in my home directory,and when i run it with 'python server.py' it gives me a the following error:
scheibo@knb$ python server.py
webdir: ".", port 80
Traceback (most recent call last):
File "server.py", line 23, in <module>
srvrobj = HTTPServer(srvraddr, CGIHTTPRequestHandler)
File "/usr/lib/python2.6/SocketServer.py", line 400, in __init__
self.server_bind()
File "/usr/lib/python2.6/BaseHTTPServer.py", line 108, in server_bind
SocketServer.TCPServer.server_bind(self)
File "/usr/lib/python2.6/SocketServer.py", line 411, in server_bind
self.socket.bind(self.server_address)
File "<string>", line 1, in bind
socket.error: [Errno 13] Permission denied
permission denied? i then try 'sudo python server.py', and after this the script works fine. the server runs as a daemon and everything seems peachy keen. however, my problem is when trying to actually USE it with my browser. Using the tutor0.py:example from the book (which resides in a cgi-bin directory in my home director)
print "Content-type: text/html\n"
print "<TITLE>CGI 101</TITLE>"
print "<H1>A First CGI script</H1>"
print "<P>Hello, CGI World!</P>"
i type: http://localhost/cgi-bin/tutor0.py into firefox and a box pops up asking me what to do with 'tutor0.py, python script'. the default option it provides is to open in a text editor, but upon opening the file it is blank.
My problem is that I want to figure out how to get the generated tutor.py script output to appear in my web browser. if i look at the output in the terminal that is running server.py, I see:
webdir: ".", port 80
localhost - - [30/Jun/2009 00:14:11] "GET /cgi-bin/tutor0.py HTTP/1.1" 200 -
Traceback (most recent call last):
File "/usr/lib/python2.6/CGIHTTPServer.py", line 255, in run_cgi
os.execve(scriptfile, args, os.environ)
OSError: [Errno 13] Permission denied
localhost - - [30/Jun/2009 00:14:11] CGI script exit status 0x7f00
localhost - - [30/Jun/2009 00:14:20] "GET /cgi-bin/tutor0.py HTTP/1.1" 200 -
which judging by the permission denied again leads me to believe that this is an administrator privilege thing. i've tried running this script on 2.5, 2.6 and 3.0 and no luck (for 3.0 i changed the print statements to function calls of course). any idea how i can get this cgi stuff to work?
tia
#!/usr/bin/python
#python server (968)
webdir = '.' #where html and cgi-bin files live
port = 80 #use http://localhost/
import os, sys
from BaseHTTPServer import HTTPServer
from CGIHTTPServer import CGIHTTPRequestHandler
if len(sys.argv) > 1: webdir = sys.argv[1]
if len(sys.argv) > 2: port = int(sts.argv[2])
print 'webdir: "%s", port %s' % (webdir, port)
#windows hack, os.environ not propagated
if sys.platform[:3] == 'win':
CGIHTTPRequestHandler.have_popen2 = False
CGIHTTPRequestHandler.have_popen3 = False #emulate path after fork
sys.path.append('cgi-bin') #else only adds my dir
os.chdir(webdir) #run in HTML root dir
srvraddr = ("", port) #my hostname, port number
srvrobj = HTTPServer(srvraddr, CGIHTTPRequestHandler)
srvrobj.serve_forever() #serve clients till exit
this script resides in my home directory,and when i run it with 'python server.py' it gives me a the following error:
scheibo@knb$ python server.py
webdir: ".", port 80
Traceback (most recent call last):
File "server.py", line 23, in <module>
srvrobj = HTTPServer(srvraddr, CGIHTTPRequestHandler)
File "/usr/lib/python2.6/SocketServer.py", line 400, in __init__
self.server_bind()
File "/usr/lib/python2.6/BaseHTTPServer.py", line 108, in server_bind
SocketServer.TCPServer.server_bind(self)
File "/usr/lib/python2.6/SocketServer.py", line 411, in server_bind
self.socket.bind(self.server_address)
File "<string>", line 1, in bind
socket.error: [Errno 13] Permission denied
permission denied? i then try 'sudo python server.py', and after this the script works fine. the server runs as a daemon and everything seems peachy keen. however, my problem is when trying to actually USE it with my browser. Using the tutor0.py:example from the book (which resides in a cgi-bin directory in my home director)
print "Content-type: text/html\n"
print "<TITLE>CGI 101</TITLE>"
print "<H1>A First CGI script</H1>"
print "<P>Hello, CGI World!</P>"
i type: http://localhost/cgi-bin/tutor0.py into firefox and a box pops up asking me what to do with 'tutor0.py, python script'. the default option it provides is to open in a text editor, but upon opening the file it is blank.
My problem is that I want to figure out how to get the generated tutor.py script output to appear in my web browser. if i look at the output in the terminal that is running server.py, I see:
webdir: ".", port 80
localhost - - [30/Jun/2009 00:14:11] "GET /cgi-bin/tutor0.py HTTP/1.1" 200 -
Traceback (most recent call last):
File "/usr/lib/python2.6/CGIHTTPServer.py", line 255, in run_cgi
os.execve(scriptfile, args, os.environ)
OSError: [Errno 13] Permission denied
localhost - - [30/Jun/2009 00:14:11] CGI script exit status 0x7f00
localhost - - [30/Jun/2009 00:14:20] "GET /cgi-bin/tutor0.py HTTP/1.1" 200 -
which judging by the permission denied again leads me to believe that this is an administrator privilege thing. i've tried running this script on 2.5, 2.6 and 3.0 and no luck (for 3.0 i changed the print statements to function calls of course). any idea how i can get this cgi stuff to work?
tia