473,725 Members | 1,696 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

HTTP server

Hi all,

Ive been reading about creating a HTTP server like the one pydoc
creates (and studying pydoc source code). What i want to know, is it
possible to create server that creates a webpage with hyperlinks that
communicate back to the HTTP server, where each link accessed tells the
server to execute some arbitrary command on local machine its running
on?

Cheers

Jun 24 '06 #1
6 6306
placid wrote:
Hi all,

Ive been reading about creating a HTTP server like the one pydoc
creates (and studying pydoc source code). What i want to know, is it
possible to create server that creates a webpage with hyperlinks that
communicate back to the HTTP server, where each link accessed tells the
server to execute some arbitrary command on local machine its running
on?

Cheers


Yes. It is possible.
Ok, seriously, I don't know how pydoc does it, but when I need a
quick-and-dirty http server [written in python] I use something like
this:

from BaseHTTPServer import HTTPServer
from SimpleHTTPServe r import SimpleHTTPReque stHandler

HTTPServer(('', 8000), SimpleHTTPReque stHandler).serv e_forever()

For what you're asking about you'd probably want to use the
CGIHTTPRequestH andler from the CGIHTTPServer module instead. Check out
http://docs.python.org/lib/module-CGIHTTPServer.html

Then you'd just write one or more cgi scripts (they can be in python
IIRC) to run the commands you want.

HTH,
~Simon

Jun 24 '06 #2

Simon Forman wrote:


Ok, seriously, I don't know how pydoc does it, but when I need a
quick-and-dirty http server [written in python] I use something like
this:

from BaseHTTPServer import HTTPServer
from SimpleHTTPServe r import SimpleHTTPReque stHandler

HTTPServer(('', 8000), SimpleHTTPReque stHandler).serv e_forever()

For what you're asking about you'd probably want to use the
CGIHTTPRequestH andler from the CGIHTTPServer module instead. Check out
http://docs.python.org/lib/module-CGIHTTPServer.html
This is what i was after, thanks for the tip.

Then you'd just write one or more cgi scripts (they can be in python
IIRC) to run the commands you want.


Im having trouble running the following cgi script on windows

<code>

#!c:/Python/python.exe -u

text = """Content-type: text/html

<TITLE> CGI 101 </TITLE>
<H1>A Second CGI script</H1>
<HR>
<P>Hello, CGI World!</P>
"""
print text

</code>
using this http server from
http://effbot.org/librarybook/cgihttpserver.htm

<code>

import CGIHTTPServer
import BaseHTTPServer

class Handler(CGIHTTP Server.CGIHTTPR equestHandler):
cgi_directories = ["/cgi"]

PORT = 8000

httpd = BaseHTTPServer. HTTPServer(("", PORT), Handler)
print "serving at port", PORT
httpd.serve_for ever()

</code>

i get the error number 403, when i try to access the cgi script which
is located in a subdirectory called cgi where this file is (http
server). I have a feeling that i need to change the Handler class or
something, implement , but i couldnt find any examples other then this
from eff-bot.

It could also be this line of code, that a google search turned up, its
the way of running cgi scripts on windows

#!c:/Python/python.exe -u
Cheers

Jun 25 '06 #3
placid wrote:
Simon Forman wrote:
.... For what you're asking about you'd probably want to use the
CGIHTTPRequestH andler from the CGIHTTPServer module instead. Check out
http://docs.python.org/lib/module-CGIHTTPServer.html
This is what i was after, thanks for the tip.


You're welcome, my pleasure. : )

....
Im having trouble running the following cgi script on windows

<code>

#!c:/Python/python.exe -u

text = """Content-type: text/html

<TITLE> CGI 101 </TITLE>
<H1>A Second CGI script</H1>
<HR>
<P>Hello, CGI World!</P>
"""
print text

</code>
using this http server from
http://effbot.org/librarybook/cgihttpserver.htm

<code>

import CGIHTTPServer
import BaseHTTPServer

class Handler(CGIHTTP Server.CGIHTTPR equestHandler):
cgi_directories = ["/cgi"]

PORT = 8000

httpd = BaseHTTPServer. HTTPServer(("", PORT), Handler)
print "serving at port", PORT
httpd.serve_for ever()

</code>

i get the error number 403, when i try to access the cgi script which
is located in a subdirectory called cgi where this file is (http
server). I have a feeling that i need to change the Handler class or
something, implement , but i couldnt find any examples other then this
from eff-bot.

It could also be this line of code, that a google search turned up, its
the way of running cgi scripts on windows

#!c:/Python/python.exe -u
Cheers

Your cgi and server scripts look fine to me.

Some diagnostic questions:

What is the filename of your cgi script?
(It must end in ".py" or ".pyw" for CGIHTTPRequestH andler to recognize
it as a python script. See the is_python() method in the handler
class. Also, in this case the "#!c:/Python/python.exe -u" line isn't
needed, but it doesn't hurt. CGIHTTPRequestH andler should find the
python interpreter for you without it.)

What was the exact URL that you're using to try to reach your script?
(It should look something like:
"http://localhost:8000/cgi/myscript.py")

What was the error message that accompanied the 403 error?

Did you start the server script from the directory it's in?

What was the log output from your server script when you tried to
access the cgi script?

Peace,
~Simon

Jun 25 '06 #4

Simon Forman wrote:
placid wrote:
Simon Forman wrote:
... For what you're asking about you'd probably want to use the
CGIHTTPRequestH andler from the CGIHTTPServer module instead. Check out
http://docs.python.org/lib/module-CGIHTTPServer.html
This is what i was after, thanks for the tip.


You're welcome, my pleasure. : )

...

Im having trouble running the following cgi script on windows

<code>

#!c:/Python/python.exe -u

text = """Content-type: text/html

<TITLE> CGI 101 </TITLE>
<H1>A Second CGI script</H1>
<HR>
<P>Hello, CGI World!</P>
"""
print text

</code>
using this http server from
http://effbot.org/librarybook/cgihttpserver.htm

<code>

import CGIHTTPServer
import BaseHTTPServer

class Handler(CGIHTTP Server.CGIHTTPR equestHandler):
cgi_directories = ["/cgi"]

PORT = 8000

httpd = BaseHTTPServer. HTTPServer(("", PORT), Handler)
print "serving at port", PORT
httpd.serve_for ever()

</code>

i get the error number 403, when i try to access the cgi script which
is located in a subdirectory called cgi where this file is (http
server). I have a feeling that i need to change the Handler class or
something, implement , but i couldnt find any examples other then this
from eff-bot.

It could also be this line of code, that a google search turned up, its
the way of running cgi scripts on windows

#!c:/Python/python.exe -u
Cheers

Your cgi and server scripts look fine to me.

Some diagnostic questions:

What is the filename of your cgi script?
(It must end in ".py" or ".pyw" for CGIHTTPRequestH andler to recognize
it as a python script. See the is_python() method in the handler
class. Also, in this case the "#!c:/Python/python.exe -u" line isn't
needed, but it doesn't hurt. CGIHTTPRequestH andler should find the
python interpreter for you without it.)


The file was named test.cgi. I changed it too test.py and it worked

What was the exact URL that you're using to try to reach your script?
(It should look something like:
"http://localhost:8000/cgi/myscript.py")
i was trying to access it at http://localhost:8000/test.cgi
which i now know is wrong.

What was the error message that accompanied the 403 error?

Did you start the server script from the directory it's in?
i started the server one directory above the cgi directory
What was the log output from your server script when you tried to
access the cgi script?


The error message outputted by the server was
localhost - - [26/Jun/2006 09:56:53] code 403, message CGI script is
not a plain file ('/cgi/')
Thanks for the help. I got it to work now.

Cheers

Jun 26 '06 #5

placid wrote:
Simon Forman wrote: ....
The file was named test.cgi. I changed it too test.py and it worked

Awesome! Glad to hear it.

....
Thanks for the help. I got it to work now.


You're welcome. I'm glad I could help you. :-D
Peace,
~Simon

Jun 26 '06 #6

Simon Forman wrote:
...


Awesome! Glad to hear it.

...

Thanks for the help. I got it to work now.


You're welcome. I'm glad I could help you. :-D


Im having trouble with the following code for handling GET requests
from a client to my HTTP server. What i want to do is restrict access
only to a folder and contents within this folder. But when trying to
open files (text files) i get file not found error from send_head()
method of SimpleHTTPServe r. The reason behind this is when opening the
file the path to the file is only C:\file.txt when it should be
C:\folder\file. txt. And when i remove the code that checks if path
contains "txt" it works (i can access files without errors).

Any help will be greatly appreciated!

<code>
def list_directory( self, path):
"""Helper to produce a directory listing (absent index.html).

Return value is either a file object, or None (indicating an
error). In either case, the headers are sent, making the
interface the same as for send_head().

"""
f = StringIO()

p = self.translate_ path(self.path)
if p.find("txt") == -1:
f.write("<title >httpserver.p y: Access Denied</title>" )
f.write("<h2>ht tpserver.py: Access Denied</h2>" )
else:
try:
list = os.listdir(path )
except os.error:
self.send_error (404, "No permission to list directory")
return None

list.sort(key=l ambda a: a.lower())

displaypath = cgi.escape(urll ib.unquote(self .path))
f.write("<title >Directory listing for %s</title>\n" %
displaypath)
f.write("<h2>Di rectory listing for %s</h2>\n" %
displaypath)
f.write("<hr>\n <ul>\n")
for name in list:
fullname = os.path.join(pa th, name)
displayname = linkname = name
# Append / for directories or @ for symbolic links
if os.path.isdir(f ullname):
displayname = name + "/"
linkname = name + "/"
if os.path.islink( fullname):
displayname = name + "@"
# Note: a link to a directory displays with @ and
links with /
f.write('<li><a href="%s">%s</a>\n'
% (urllib.quote(l inkname),
cgi.escape(disp layname)))
f.write("</ul>\n<hr>\n")

length = f.tell()
f.seek(0)
self.send_respo nse(200)
self.send_heade r("Content-type", "text/html")
self.send_heade r("Content-Length", str(length))
self.end_header s()
return f
</code>

Jun 27 '06 #7

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

Similar topics

1
2698
by: Erik Johnson | last post by:
Hi, I am trying to spawn a daemon type program to go off on its own and do some work (asynchoronously) from within an HTTPServer, but I am running into a problem where the web browser (actually a Perl LWP program) still seems to be waiting for more input until the child that is forked in the server finishes (granchild, actually). I don't understand this. I need to be able to have the POST to the server complete and go on even though...
7
9286
by: Michael Foord | last post by:
#!/usr/bin/python -u # 15-09-04 # v1.0.0 # auth_example.py # A simple script manually demonstrating basic authentication. # Copyright Michael Foord # Free to use, modify and relicense. # No warranty express or implied for the accuracy, fitness to purpose
30
4847
by: Anon | last post by:
If Http headers specify the character encoding, what is the point of the Meta tag specifying it?
5
6021
by: Henrik | last post by:
Hi, I am trying to read some industrial webservers using the HTTP/CGI webequest like this: wrs = (HttpWebRequest)WebRequest.Create(HTTP/CGI-string); mwst = (HttpWebResponse wrs.GetResponse(); str = mwst.GetResponseStream(); This usually works fine but on some servers i get:
8
10012
by: Rod | last post by:
I have been working with ASP.NET 1.1 for quite a while now. For some reason, opening some ASP.NET applications we wrote is producing the following error message: "The Web server reported the following error when attempting to create or open the Web project located at the following URL: 'http://localhost/WebApplication1'. 'HTTP/1.1 500 Internal Server Error'."
3
6925
by: Patrick Fogarty | last post by:
I am programming what is to be a web service client that will use an HTTP-POST to request and retrieve data. The remote server (written in java for what it's worth) requires basic authentication as per RFC 2617 (http://www.faqs.org/rfcs/rfc2617.html). My attempts to authenticate are failing. The server requires the header to be present with the request. For security reasons, it will not reply in any way if the header is not present. ...
5
10397
by: David Lozzi | last post by:
Howdy, I wrote a web service in .Net for my customer. My customer has another vendor who now has to consume it but they are not using Visual Studio. Most of their pages are jsp, and they said they need to consume this web service using HTTP. The developer's IDE is Notepad. Yeah, weird I know. How is this done? I guess if I can get it to run ASP, IDE independant, that should make them happy. Any references you can point me to?
4
2060
by: jens Jensen | last post by:
Hello, I was given the task to build a .Net client that will talk to IBM integration server via HTTP post. The idea is that each http packet exchange should be authenticated via X09 "client authentication and not "server authentification" The code i wrote is quite straight forward. (See listing below) With server authentication, the server's application will contain a certificate created specifically for that server. The client...
15
4635
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to communicate with a MySQL database table on a web server, from inside of my company's Access-VBA application. I know VBA pretty well but have never before needed to do this HTTP/XML/MySQL type functions.
3
6142
by: fniles | last post by:
In our ASP page, we call XMLHttp to download XML files. When calling our page using localhost (localhost/myWebSite/myPage.htm), it works, but when calling using the IP address of the web server (xx.x.x.xxx/myWebSite/myPage.htm), I got "Permission denied" error in the HTTP.Open statement. Set http = CreateObject("Microsoft.XmlHttp") http.open "Get", "http://" & "xx.x.x.xxx" & "/XMLFiles/N-1min.xml", ALSE --> permission denied error
0
9392
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...
1
9162
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
9091
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...
0
8069
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6694
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
5997
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
4505
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...
0
4773
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2619
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.