[Medusa-dev] psp_handler - embed python in HTML like ASP

[Medusa-dev]
psp_handler - embed python in HTML like ASP

[Medusa-dev] psp_handler - embed python in HTML like
ASP

Kevin Smith smithk
at attbi.com
Sun Apr 27 13:27:49 EDT 2003

  • Previous message: [Medusa-dev] Small patch
    to move URI unquoting in http_server.py

  • Next message: [Medusa-dev] Channel not
    closing after error

  • Messages sorted by: [
    date ] [ thread ] [
    subject ] [ author ]



Hi, I have created a psp_handler (Python Server Pages???) for medusa
based on the script_handler. This handler lets you mix HTML with python
like ASP pages. The handler can be installed on the server in the sam
manner as the script handler.
Here is an example hello.psp file...

<html>
<title>Medusa PSP Handler</title>
<body>
<%psp
print "hello world"
%>
</body>
</html>

I have been using this handler for much more useful applications of
course :). Just wrap the inline python with <%psp ... %> tags.

Here is the handler...
# -*- Mode: Python -*-

# This is a simple python server pages script handler.

# A note about performance: This is really only suited for ‘fast‘
# scripts: The script should generate its output quickly, since the
# whole web server will stall otherwise. This doesn‘t mean you have
# to write ‘fast code‘ or anything, it simply means that you shouldn‘t
# call any long-running code, [like say something that opens up an
# internet connection, or a database query that will hold up the
# server]. If you need this sort of feature, you can support it using
# the asynchronous I/O ‘api‘ that the rest of medusa is built on. [or
# you could probably use threads]

# Put your script into your web docs directory (like a cgi-bin
# script), make sure it has the correct extension [see the overridable
# script_handler.extension member below].
#
# There‘s lots of things that can be done to tweak the restricted
# execution model. Also, of course you could just use ‘execfile‘
# instead (this is now the default, see class variable
# script_handler.restricted)

import rexec
import re
import string
import StringIO
import sys

import counter
import default_handler
import producers
from script_handler import collector

unquote = default_handler.unquote

def iif(expression,truepart,falsepart):
if expression:
return truepart
else:
return falsepart

class psp_handler:

extension = ‘psp‘

# the following should not get overridden!
fp = None
script = ‘‘
data = ‘‘
insidePsp = False

script_regex = re.compile (
r‘.*/([^/]+\.%s)‘ % extension,
re.IGNORECASE
)

def __init__ (self,
filesystem,restricted=False,preserveNamespace=True):
self.filesystem = filesystem
self.hits = counter.counter()
self.exceptions = counter.counter()
self.restricted = restricted
self.preserveNamespace = preserveNamespace

def match (self, request):
[path, params, query, fragment] = request.split_uri()
m = self.script_regex.match (path)
return (m and (m.end() == len(path)))

def handle_request (self, request):

[path, params, query, fragment] = request.split_uri()

while path and path[0] == ‘/‘:
path = path[1:]

if ‘%‘ in path:
path = unquote (path)

if not self.filesystem.isfile (path):
request.error (404)
return
else:

self.hits.increment()

request.script_filename = self.filesystem.translate (path)

if request.command in (‘PUT‘, ‘POST‘):
# look for a Content-Length header.
cl = request.get_header (‘content-length‘)
length = int(cl)
if not cl:
request.error (411)
else:
collector (self, length, request)
else:
self.continue_request (
request,
StringIO.StringIO() # empty stdin
)

def continue_request (self, request, stdin):
temp_files = stdin, StringIO.StringIO(), StringIO.StringIO()
old_files = sys.stdin, sys.stdout, sys.stderr

try:
sys.request = request
sys.stdin, sys.stdout, sys.stderr = temp_files
try:
#get the path from the uri and open the file with the
filesystem class
try:
file =
self.filesystem.open(request.split_uri()[0],‘r‘)
except IOError:
request.error (404)
return
self.fp = producers.file_producer(file)
self.dissect_psp(request)
request.reply_code = 200
except:
request.reply_code = 500
self.exceptions.increment()
finally:
sys.stdin, sys.stdout, sys.stderr = old_files
del sys.request

i,o,e = temp_files

if request.reply_code != 200:
s = e.getvalue()
else:
s = o.getvalue()

request[‘Content-Length‘] = len(s)
request.push (s)
request.done()

def status (self):
return producers.simple_producer (
‘<li>PSP - Python Server Pages Handler‘
+ ‘<ul>‘
+ ‘ <li><b>Hits:</b> %s‘ % self.hits
+ ‘ <li><b>Exceptions:</b> %s‘ % self.exceptions
+ ‘ <li><b>Execution Mode:</b>%s‘ %
iif(self.restricted,‘Restricted‘,‘Unrestricted‘ )
+ ‘ <li><b>Namespace:</b>:%sPreserved‘ %
iif(self.preserveNamespace,‘‘,‘not ‘ )
+ ‘</ul>‘
)

## this function reads the file using the file producer and sends
## the data to the client until the script start tag ‘<%psp‘
## is found. All of the text between the script start marker
## ‘<%psp‘ and the end script marker ‘%>‘ is executed as
## python script.
def dissect_psp(self,request):
self.insidePsp = False
self.script = ‘‘
while not self.fp.done:
self.data=self.fp.more()
#print the HTML to the stdout, execute the python script...
while self.data:
if self.insidePsp:
sectionend=self.data.find("%>")
if (sectionend == -1):
#end of script section is not in the current
chunk
self.script += self.data
self.data = ‘‘
else:
#end of script section is within the current
chunk
self.script += self.data[:sectionend]
self.data = self.data[sectionend+len("%>"):]
del sectionend
if self.preserveNamespace:
if self.restricted:
r = rexec.RExec()
try:
if self.restricted:
r.s_exec (self.script)
else:
exec (self.script)
request.reply_code = 200
except:
request.reply_code = 500
self.exceptions.increment()
else:
self.script_exec(request,self.script)
self.script = ‘‘
self.insidePsp = False
else:
sectionend=self.data.find("<%psp")
if (sectionend == -1):
#end of HTML section is not in the current chunk
print self.data
self.data = ‘‘
else:
#end of HTML section is within the current chunk
print self.data[:sectionend]
self.data = self.data[sectionend+len("<%psp"):]
self.insidePsp = True

# this function will eliminate any of the unnecessary objects
# from appearing in the script namespace. print dir() should
# return only self,request, and script.

# one drawback with this method is that namespace is cleared
# for each section of script in the document.

def script_exec(self,request,script):
# for debugging we can send a copy of the script to the browser.
# this presents security issues so this next line should be
# commented out.
if self.restricted:
r = rexec.RExec()
try:
if self.restricted:
r.s_exec (script)
else:
exec (script)
request.reply_code = 200
except:
request.reply_code = 500
self.exceptions.increment()



  • Previous message: [Medusa-dev] Small patch
    to move URI unquoting in http_server.py

  • Next message: [Medusa-dev] Channel not
    closing after error

  • Messages sorted by: [
    date ] [ thread ] [
    subject ] [ author ]


[Medusa-dev] psp_handler - embed python in HTML like
ASP

时间: 2024-10-10 06:33:45

[Medusa-dev] psp_handler - embed python in HTML like ASP的相关文章

Python开发【第一篇】:初识Python

Python简介 Python前世今生 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继承. 最新的TIOBE排行榜,Python赶超PHP占据第五!!! 由上图可见,Python整体呈上升趋势,反映出Python应用越来越广泛并且也逐渐得到业内的认可!!! Python可以应用于众多领域,如:数据分析.组件集成.网络服务.图像处理.数值计算和科学计算等众

Python学习第一天

一.Python的简介 1.什么是python? Python(发音:[ 'paiθ(?)n; (US) 'paiθ?n ]),是一种面向对象的解释性的计算机程序设计语言,也是一种功能强大而完善的通用型语言,已经具有十多年的发展历史,成熟且稳定.这种语言具有非常简捷而清晰的语法特点,适合完成各种高层任务,几乎可以在所有的操作系统中运行. 特点: ①可扩充性.新的内置模块(module)可以用C 或 C++写成,而我们也可为现成的模块加上Python的接口: ②清晰的语言.因为它的作者在设计它的时

Python - 基础中的基础

前景 Python可以应用于众多领域,如:数据分析.组件集成.网络服务.图像处理.数值计算和科学计算等众多领域.目前业内几乎所有大中型互联网企业都在使用Python,如:Youtube.Dropbox.BT.Quora(中国知乎).豆瓣.知乎.Google.Yahoo!.Facebook.NASA.百度.腾讯.汽车之家.美团等. 互联网公司广泛使用Python来做的事一般有:自动化运维.自动化测试.大数据分析.爬虫.Web 等. Python与其他语言 C和Python.java.C# C:代码

Python前世今生

Python前世今生 Python简介 Python前世今生 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继承. 最新的TIOBE排行榜,Python赶超PHP占据第五!!! 由上图可见,Python整体呈上升趋势,反映出Python应用越来越广泛并且也逐渐得到业内的认可!!! Python可以应用于众多领域,如:数据分析.组件集成.网络服务.图像处理.

Python开发【第二篇】:初识Python

Python简介 Python前世今生 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继承. 最新的TIOBE排行榜,Python赶超PHP占据第四!!!   Python应用越来越广泛并且也逐渐得到业内的认可!!! Python可以应用于众多领域,如:数据分析.组件集成.网络服务.图像处理.数值计算和科学计算等众多领域.目前业内几乎所有大中型互联网企业都

python学习【一】基础入门

Python前世今生 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继承. python的种类 Cpython python的官方版,使用C语言实现,使用最为广泛,Cpython实现会将python源文件(.py)转换为python字节码(.pyc)然后运行在python虚拟机上 Jpython python的java实现,Jpython会动态将pytho

python报错:SyntaxError: Non-ASCII character &#39;\xe5&#39;的解决方法

最近在学习机器学习,上面的代码都是一些python代码,对于python只是会一些基础性的东西,刚才就遇到了一个比较low的问题,但是还是记录一下吧. 在python代码中出现了中文,但是我又把# -*- coding: utf-8 -*-这句话放到了第三行,所以导致出现了,如下的错误: SyntaxError: Non-ASCII character '\xe5' 1. 解决方法也和简单就是把编码的那句话放在第一行: # -*- coding: utf-8 -*- #!/usr/bin/pyt

The Linux Mint 18:Eclipse Run The C++ And Python ConfigorationWhen You achieve above step,you can run the c++ and python! (Next OTL ,PYOTL is Project That Write By Ruimin Shen(ability man) )

# Copyright (c) 2016, 付刘伟 (Liuwei Fu)# All rights reserved.# 转载请注明出处 1.Install The Eclipse,g++ Use The SynapTic Package Manager: Eclipse :you should select the eclipse and eclipse-cdt-qt e: g++:just select g++ e: 2.Run The Eclipse By The Root Use The

python greenlet

greenlet: Lightweight concurrent programming Motivation The "greenlet" package is a spin-off of Stackless, a version of CPython that supports micro-threads called "tasklets". Tasklets run pseudo-concurrently (typically in a single or a