By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,631 Members | 826 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,631 IT Pros & Developers. It's quick & easy.

HTTP server

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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 SimpleHTTPServer import SimpleHTTPRequestHandler

HTTPServer(('', 8000), SimpleHTTPRequestHandler).serve_forever()

For what you're asking about you'd probably want to use the
CGIHTTPRequestHandler 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

P: n/a

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 SimpleHTTPServer import SimpleHTTPRequestHandler

HTTPServer(('', 8000), SimpleHTTPRequestHandler).serve_forever()

For what you're asking about you'd probably want to use the
CGIHTTPRequestHandler 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(CGIHTTPServer.CGIHTTPRequestHandler):
cgi_directories = ["/cgi"]

PORT = 8000

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

</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

P: n/a
placid wrote:
Simon Forman wrote:
.... For what you're asking about you'd probably want to use the
CGIHTTPRequestHandler 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(CGIHTTPServer.CGIHTTPRequestHandler):
cgi_directories = ["/cgi"]

PORT = 8000

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

</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 CGIHTTPRequestHandler 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. CGIHTTPRequestHandler 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

P: n/a

Simon Forman wrote:
placid wrote:
Simon Forman wrote:
... For what you're asking about you'd probably want to use the
CGIHTTPRequestHandler 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(CGIHTTPServer.CGIHTTPRequestHandler):
cgi_directories = ["/cgi"]

PORT = 8000

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

</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 CGIHTTPRequestHandler 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. CGIHTTPRequestHandler 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

P: n/a

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

P: n/a

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 SimpleHTTPServer. 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.py: Access Denied</title>" )
f.write("<h2>httpserver.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=lambda a: a.lower())

displaypath = cgi.escape(urllib.unquote(self.path))
f.write("<title>Directory listing for %s</title>\n" %
displaypath)
f.write("<h2>Directory listing for %s</h2>\n" %
displaypath)
f.write("<hr>\n<ul>\n")
for name in list:
fullname = os.path.join(path, name)
displayname = linkname = name
# Append / for directories or @ for symbolic links
if os.path.isdir(fullname):
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(linkname),
cgi.escape(displayname)))
f.write("</ul>\n<hr>\n")

length = f.tell()
f.seek(0)
self.send_response(200)
self.send_header("Content-type", "text/html")
self.send_header("Content-Length", str(length))
self.end_headers()
return f
</code>

Jun 27 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.