[Twisted-web] Can someone translate this, please?
Phil Hunt
twisted-web@twistedmatrix.com
Tue, 24 Feb 2004 18:35:41 +0000
I am trying to get my head round Twisted, particularly to use it as a
simple web server (using http as a RPC mechanism, really). I was thinknig
of using a "Rosetta Stone" approach, and am wondering if someone could
translate this ordinary Python program into Twisted, please:
=====================================================================
# simple_http.py = a simple http server
import SimpleHTTPServer
import BaseHTTPServer
import StringIO
template = """<html>
<head>
<title>%(title)s</title>
</head><body>
<h1>%(title)s</h1>
<p>Path is [<tt>%(path)s</tt>]
</body></html>
"""
portNum = 1450
class MyRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
args = {
'title': 'simple_http.py',
'path': self.path
}
result = template % args
self.send_response(200)
self.send_header("Content-type", "text/html")
self.send_header("Content-Length", str(len(result)))
self.end_headers()
f = StringIO.StringIO()
f.write(result)
f.seek(0)
self.copyfile(f, self.wfile)
f.close()
print "===== starting simple_http.py on port %d =====" % portNum
server_address = ('', portNum)
httpd = BaseHTTPServer.HTTPServer(server_address,
MyRequestHandler)
httpd.serve_forever()
#end
=====================================================================
As you can see, it's very simple. When it recieves a GET request it just
sends back an HTML page saying what the path in the GET request was.
--
Phil Hunt, phil.hunt@tech.mrc.ac.uk