473,406 Members | 2,894 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,406 software developers and data experts.

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

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

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

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 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
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...
7
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. #...
30
by: Anon | last post by:
If Http headers specify the character encoding, what is the point of the Meta tag specifying it?
5
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();...
8
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...
3
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...
5
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...
4
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...
15
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...
3
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...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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
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...
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.