473,670 Members | 2,724 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 CGIHTTPRequestH andler
import BaseHTTPServer

class MyRequestHandle r(CGIHTTPReques tHandler):
# 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=BaseHTTPS erver.HTTPServe r(('', 8000), MyRequestHandle r)
httpd.serve_for ever()

if __name__=="__ma in__":
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=ut f-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 2417

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 CGIHTTPRequestH andler
import BaseHTTPServer

class MyRequestHandle r(CGIHTTPReques tHandler):
# 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=BaseHTTPS erver.HTTPServe r(('', 8000), MyRequestHandle r)
httpd.serve_for ever()

if __name__=="__ma in__":
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=ut f-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.c omwrote:
>
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 CGIHTTPRequestH andler
import BaseHTTPServer

class MyRequestHandle r(CGIHTTPReques tHandler):
# 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=BaseHTTPS erver.HTTPServe r(('', 8000), MyRequestHandle r)
httpd.serve_for ever()

if __name__=="__ma in__":
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=ut f-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.c omwrote:

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 CGIHTTPRequestH andler
import BaseHTTPServer

class MyRequestHandle r(CGIHTTPReques tHandler):
# 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=BaseHTTPS erver.HTTPServe r(('', 8000), MyRequestHandle r)
httpd.serve_for ever()

if __name__=="__ma in__":
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=ut f-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.i s_cgi method might prove helpful.

Nov 4 '06 #5
Hi Norbert,

* Norbert Kaufmann <no*****@akumak un.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
16255
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 Python 2.2 but can upgrade to 2.3 beta if needed. I am looking for something like: " $ python validate.py myxmlfile.xml mydtd.dtd "
1
1270
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 : -rw-rw-r-- 1 carlos wheel 3517 Aug 4 09:51 index.html I could not write back to the file, Traceback (most recent call last): File "/usr/local/www/cgi-bin/bob.py", line 24, in ? file=open("../data/index.html","w") IOError: Permission denied:...
48
2941
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 problem, the company would pay for it). Which is better for this kind of application? I mean, looking from purely technological and long-term development aspects and ignoring licensing issues:
1
2395
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 the right env settings, Ishould be able to launch mini_httpd like so: mini_httpd -c *.pyand be able to run scripts like so: http://fqdn/simple.pyUsing info from (python) sys.path, multiple get_config_var(..)s and sys.executable, Iwas able to build...
1
1412
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 simple CGI. Please don't ask why i want a python webserver for a ruby application. I just need it.
14
3020
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 web interface he should upload the file to the web server , the server then will do the image process with the python program I have wrote and when it finish the user must get the image file back . My question is how difficult is to set up a web...
0
965
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 KML output to GE for this. import BaseHTTPServer import CGIHTTPServer class Handler(CGIHTTPServer.CGIHTTPRequestHandler): cgi_directories = httpd = BaseHTTPServer.HTTPServer(('',8000), Handler)
8
2140
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: UTF-8 -*- # enable debugging
20
3965
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 using a sun machine (I normally use linux machines) and place this in my lunix home account would I be able to use python and sqlite? Any thoughts? I know its a bit of a stretch ...
0
8903
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8815
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8592
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8661
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6216
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5686
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4213
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2802
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.