473,397 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

small python cgi webserver

Hi,

I am looking for a small python script, which starts a small
web server with python cgi support on a linux machine.

I tried:
#!/usr/bin/env python
import sys
from CGIHTTPServer import CGIHTTPRequestHandler
import BaseHTTPServer

class MyRequestHandler(CGIHTTPRequestHandler):
# In diesem Verzeichnis sollten die CGI-Programme stehen:
cgi_directories=["/home/fab/Desktop/cgi-bin"]
def run():
# 8000=Port-Nummer
# --http://localhost:8000/
# Fuer http://localhost/
# Port-Nummer auf 80 setzen
httpd=BaseHTTPServer.HTTPServer(('', 8000), MyRequestHandler)
httpd.serve_forever()

if __name__=="__main__":
print "Starting Server"
run()

but when I want to test a small python cgi test file:
#!/usr/bin/python
# -*- coding: UTF-8 -*-

# Debugging für CGI-Skripte 'einschalten'
import cgitb; cgitb.enable()

print "Content-Type: text/html;charset=utf-8\n"
print "Hello World!"

I just get the text and not the html output. The file's mode
is 755.

Is there anything wrong with the webserver script or do I do
something completely wrong? Maybe, you have a different
webserver script?

Greetings!
Fabian

Nov 4 '06 #1
6 2401

Fabian Braennstroem wrote:
Hi,

I am looking for a small python script, which starts a small
web server with python cgi support on a linux machine.

I tried:
#!/usr/bin/env python
import sys
from CGIHTTPServer import CGIHTTPRequestHandler
import BaseHTTPServer

class MyRequestHandler(CGIHTTPRequestHandler):
# In diesem Verzeichnis sollten die CGI-Programme stehen:
cgi_directories=["/home/fab/Desktop/cgi-bin"]
def run():
# 8000=Port-Nummer
# --http://localhost:8000/
# Fuer http://localhost/
# Port-Nummer auf 80 setzen
httpd=BaseHTTPServer.HTTPServer(('', 8000), MyRequestHandler)
httpd.serve_forever()

if __name__=="__main__":
print "Starting Server"
run()

but when I want to test a small python cgi test file:
#!/usr/bin/python
# -*- coding: UTF-8 -*-

# Debugging für CGI-Skripte 'einschalten'
import cgitb; cgitb.enable()

print "Content-Type: text/html;charset=utf-8\n"
print "Hello World!"

I just get the text and not the html output. The file's mode
is 755.

Is there anything wrong with the webserver script or do I do
something completely wrong? Maybe, you have a different
webserver script?

Greetings!
Fabian
Probably the server is not executing your CGI script. If it is the
Apache web server that you are using then just ensure the following
settings in your /etc/httpd/conf/httpd.conf file is exactly like
following:

<Directory "/var/www/cgi-bin">
AllowOverride None
Options ExecCGI
Order allow,deny
Allow from all
</Directory>

Nov 4 '06 #2
Hi,

* ArdPy <ar****@gmail.comwrote:
>
Fabian Braennstroem wrote:
>Hi,

I am looking for a small python script, which starts a small
web server with python cgi support on a linux machine.

I tried:
#!/usr/bin/env python
import sys
from CGIHTTPServer import CGIHTTPRequestHandler
import BaseHTTPServer

class MyRequestHandler(CGIHTTPRequestHandler):
# In diesem Verzeichnis sollten die CGI-Programme stehen:
cgi_directories=["/home/fab/Desktop/cgi-bin"]
def run():
# 8000=Port-Nummer
# --http://localhost:8000/
# Fuer http://localhost/
# Port-Nummer auf 80 setzen
httpd=BaseHTTPServer.HTTPServer(('', 8000), MyRequestHandler)
httpd.serve_forever()

if __name__=="__main__":
print "Starting Server"
run()

but when I want to test a small python cgi test file:
#!/usr/bin/python
# -*- coding: UTF-8 -*-

# Debugging für CGI-Skripte 'einschalten'
import cgitb; cgitb.enable()

print "Content-Type: text/html;charset=utf-8\n"
print "Hello World!"

I just get the text and not the html output. The file's mode
is 755.

Is there anything wrong with the webserver script or do I do
something completely wrong? Maybe, you have a different
webserver script?

Greetings!
Fabian

Probably the server is not executing your CGI script. If it is the
Apache web server that you are using then just ensure the following
settings in your /etc/httpd/conf/httpd.conf file is exactly like
following:

<Directory "/var/www/cgi-bin">
AllowOverride None
Options ExecCGI
Order allow,deny
Allow from all
</Directory>
Maybe, I understood something wrong, but I thought that the
above 'webserver' script would replace apache in my case; at
least I hoped!?

Greetings!
Fabian

Nov 4 '06 #3
Fabian Braennstroem wrote:
[...]
>
Maybe, I understood something wrong, but I thought that the
above 'webserver' script would replace apache in my case; at
least I hoped!?
It does. The 'ServerRoot' and 'DocumentRoot' directories are the
directories you are starting your webserver in.
Create a 'cgi' directory inside this and consider that you have to name
it in the serverscript in relation to the serverroot!

<quote>
cgi_directories=["/home/fab/Desktop/cgi-bin"]
</quote>

This means you have to start your server inside directory '/'.

If you start your server in your home dir '/home/fab' then you have to
name your cgi_directories ['/Desktop/cgi-bin'].

In your response (cgi-script) you have to divide the header from the
content '\r\n\r\n'.

HTH

Norbert
Nov 4 '06 #4

Fabian Braennstroem wrote:
Hi,

* ArdPy <ar****@gmail.comwrote:

Fabian Braennstroem wrote:
Hi,

I am looking for a small python script, which starts a small
web server with python cgi support on a linux machine.

I tried:
#!/usr/bin/env python
import sys
from CGIHTTPServer import CGIHTTPRequestHandler
import BaseHTTPServer

class MyRequestHandler(CGIHTTPRequestHandler):
# In diesem Verzeichnis sollten die CGI-Programme stehen:
cgi_directories=["/home/fab/Desktop/cgi-bin"]
def run():
# 8000=Port-Nummer
# --http://localhost:8000/
# Fuer http://localhost/
# Port-Nummer auf 80 setzen
httpd=BaseHTTPServer.HTTPServer(('', 8000), MyRequestHandler)
httpd.serve_forever()

if __name__=="__main__":
print "Starting Server"
run()

but when I want to test a small python cgi test file:
#!/usr/bin/python
# -*- coding: UTF-8 -*-

# Debugging für CGI-Skripte 'einschalten'
import cgitb; cgitb.enable()

print "Content-Type: text/html;charset=utf-8\n"
print "Hello World!"

I just get the text and not the html output. The file's mode
is 755.

Is there anything wrong with the webserver script or do I do
something completely wrong? Maybe, you have a different
webserver script?

Greetings!
Fabian
Probably the server is not executing your CGI script. If it is the
Apache web server that you are using then just ensure the following
settings in your /etc/httpd/conf/httpd.conf file is exactly like
following:

<Directory "/var/www/cgi-bin">
AllowOverride None
Options ExecCGI
Order allow,deny
Allow from all
</Directory>

Maybe, I understood something wrong, but I thought that the
above 'webserver' script would replace apache in my case; at
least I hoped!?

Greetings!
Fabian
Oh yes...Your script is supposed to replace apache. I tried with your
script on my pc and its working just fine. However the problem still is
that the server is taking your file to be a plain file rather than a
CGI script. Looking at CGIHTTPServer.is_cgi method might prove helpful.

Nov 4 '06 #5
Hi Norbert,

* Norbert Kaufmann <no*****@akumakun.dewrote:
Fabian Braennstroem wrote:
[...]
>>
Maybe, I understood something wrong, but I thought that the
above 'webserver' script would replace apache in my case; at
least I hoped!?

It does. The 'ServerRoot' and 'DocumentRoot' directories are the
directories you are starting your webserver in.
Create a 'cgi' directory inside this and consider that you have to name
it in the serverscript in relation to the serverroot!

<quote>
cgi_directories=["/home/fab/Desktop/cgi-bin"]
</quote>

This means you have to start your server inside directory '/'.
I tried this, but it does not help ... a wait, the leading
'/' is the problem. Thanks!
>
If you start your server in your home dir '/home/fab' then you have to
name your cgi_directories ['/Desktop/cgi-bin'].

In your response (cgi-script) you have to divide the header from the
content '\r\n\r\n'.
I am not sure, what that means!? ... but it works :-)

Greetings!
Fabian

Nov 4 '06 #6
Fabian Braennstroem wrote:
[...]
>>In your response (cgi-script) you have to divide the header from the
content '\r\n\r\n'.


I am not sure, what that means!? ... but it works :-)
We are talking about HTTP, take a look at the HTTP response in version 1.1:
http://www.w3.org/Protocols/rfc2616/...sec6.html#sec6

As you may see you separate the header of a response from the body by an
empty line, generated with CRLF.
Since one CRLF ends the line inside the header you need two of them.

Bye

Norbert
Nov 5 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Will Stuyvesant | last post by:
Can you give a commandline example how to do XML Validation (checking against a DTD) with Python? Not with 4Suite or other 3rd party libraries, just the Python standard distribution. I have...
1
by: Carlos Andrade | last post by:
I have a working python script that takes in content from a form, cleans it up, adds html formatting and then places it into my index.html page on my server. When I had index.html set up as :...
48
by: Bulba! | last post by:
I'll soon start development of a specialized small app and need to choose GUI for it. I have narrowed the choice to wxPython/PythonCard and QT/PyQT (buying commercial licenses is not a big...
1
by: Venkat B | last post by:
Hi folks,I have a webserver based on mini_httpd v1.19(http://www.acme.com/software/mini_httpd/).I'd like to run some python-based CGI scripts via this webserver on an RH9 system.In theory, with...
1
by: llothar | last post by:
Hello, as the subject says, for an embedded application i need a 100% pure python webserver that can talk to a FCGI process (which runs ruby on rails). Of couse it also must be able to use...
14
by: nicolasg | last post by:
Hi folks, I have accomplished to make a python program that make some image manipulation to bmp files. I now want to provide this program as a web service. A user can visit a site and through a...
0
by: dalebryan1 | last post by:
I am working on a task to display a wireless network nodes using Google Earth (GE) with KML network links. I am using a simple python webserver (see code below) to serve up the python scripts as...
8
by: timotoole | last post by:
Hi all, I'm trying to get python to work with cgi for a small intranet site, however even a simply "hello world test isn't working". Here is the test file: #!/usr/bin/python # -*- coding:...
20
by: timotoole | last post by:
Hi all, On a (sun) webserver that I use, there is python 2.5.1 installed. I'd like to use sqlite3 with this, however sqlite3 is not installed on the webserver. If I were able to compile sqlite...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.