寻觅生命中的那一片浅草......

文章属于类别 Python

func在openSUSE12.3上的问题

同样的程序,在CentOS上可以正常执行

func “*” ping

但在openSUSE上就有问题,经过调试发现,openSUSE下,site-packages有2个路径

通过下面的命令获取的是

import distutils.sysconfig
distutils.sysconfig.get_python_lib()
‘/usr/lib/python2.7/site-packages’

但func实际安装在

/usr/local/lib/python2.7/site-packages/func

需要修改代码:

vi /usr/local/lib/python2.7/site-packages/func/module_loader.py

module_file_path = “%s/%s” % (python_path, path)

下添加

if not os.path.isdir(module_file_path):
    python_path = ‘/usr/local/lib/python2.7/site-packages’
    module_file_path = “%s/%s” % (python_path, path)

访问Django的admin报TemplateDoesNotExist解决办法

环境是Django+uwsgi+Nginx

在浏览器访问http://localhost/admin,报下面的错误,这个错误虽然不影响正常访问,但在调试程序的时候,动不动来一坨这玩意,是比较恶心的

Traceback (most recent call last):
File “/opt/dj1.4/lib/python2.7/site-packages/django/core/handlers/wsgi.py”, line 241, in __call__
response = self.get_response(request)
File “/opt/dj1.4/lib/python2.7/site-packages/django/core/handlers/base.py”, line 153, in get_response
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
File “/opt/dj1.4/lib/python2.7/site-packages/django/core/handlers/base.py”, line 228, in handle_uncaught_exception
return callback(request, **param_dict)
File “/opt/dj1.4/lib/python2.7/site-packages/django/utils/decorators.py”, line 91, in _wrapped_view
response = view_func(request, *args, **kwargs)
File “/opt/dj1.4/lib/python2.7/site-packages/django/views/defaults.py”, line 32, in server_error
t = loader.get_template(template_name) # You need to create a 500.html template.
File “/opt/dj1.4/lib/python2.7/site-packages/django/template/loader.py”, line 145, in get_template
template, origin = find_template(template_name)
File “/opt/dj1.4/lib/python2.7/site-packages/django/template/loader.py”, line 138, in find_template
raise TemplateDoesNotExist(name)
django.template.base.TemplateDoesNotExist: 500.html

打开nginx的error_log

error_log /data/logs/admin_error_log;

可以看到

2013/03/05 17:19:17 [error] 1722#0: *1091930 upstream prematurely closed connection while reading response header from upstream, client: 192.168.1.10, server:localhost, request: “GET /favicon.ico HTTP/1.1”, upstream: “uwsgi://127.0.0.1:8000”, host: “localhost”

浏览器请求favicon.ico,但Django没有,所以报错了,我们可以在Nginx添加配置解决这个问题

location /favicon.ico {
empty_gif;
break;
}

zz:Python替换JavaScript:Brython

Brython 设计用于替换网页上的 JavaScript 脚本语言,它使用 Python 来编写脚本,并直接在网页上执行。

<script src="brython.js"></script>
<body onload="brython()">

<script type="text/python">
t = TABLE()
for i in range(10):
    t <= TR(TD(i)+TD(i*i))
doc <= t
</script>

转载自:http://www.oschina.net/p/brython

一些基于Web的运维系统

Web运维系统,指的是通过浏览器对服务器进行远程管理,有一些现成的工具,不过我也是最近半年才知道下面几个工具(小弟孤陋寡闻),真是酒香也怕项深啊,宣传真的非常重要。

一、noc

全称叫The NOC Project,Python写的,Web展示用的是Django这个框架,支持的设备/系统较多 ,什么Linux,路由器,交换机都可以管理,用什么方式登录Linux?ssh啦,无需客户端,用的是用户名和密码登录,不知道是否支持key登录,即使不支持,改成key登录也不难。

它提供一个基于OpenSUSE的安装好noc的VirtualBox镜像 ,大家可以下载回来试用下。

来自开源中国的介绍:http://www.oschina.net/p/noc

官方网站:http://kb.nocproject.org/display/SITE/NOC

二、ControlTier

java写的,分主控端和服务器端,安装使用方面都比较简单,基本是添加客户端,添加作业,运行作业,作业输出默认是摘要输出,可以点开来看详细输出,本人觉得这系统有一个亮点就是用tag对服务器进行分类,一个服务器可以有很多很多的tag啊。

这个就不发地址了,Google下很多介绍

三、OPMS

同样是基于Python+Django,这个系统简单点,但思路非常好,我们之前写的一个web管理系统,出现问题是,通过Web提交出去的作业,由于执行时间较长,全部都超时了,导致那系统无法使用。OPMS给了我们很大的启发,它是通过Web,提交作业到redis,然后服务器上跑一个Python守护进程,定期从redis里pop作业出来处理,输出保存在一个文件伦理,Web通过ajax读日志文件来显示作业的进度。

地址:http://bbs.python520.com/forum.php?mod=viewthread&tid=270

四、OpenWebSA

一个规划得非常完整的系统,含监控,远程管理, 操作日志等等,所使用的语言主要有Python+PHP+sh。

地址:http://bbs.osapub.com/

五、webistrano
Capistrano的Web界面,Ruby写的,服务器间通讯用的是SSH。

[zz]尝试PyTenjin --号称最快的Python 模板库

官网在这里http://www.kuwata-lab.com/tenjin/

曾经使用过Kid、Cheeth、Web2py的模 板,目前使用Mako和Django(Jinjia)。曾经非常的欣赏Web2py的模板,无论代码还是实际的html代码对coder都异常的友好,符 合自己‘想当然’这个库评价标准。但是因为Web2py没有将他的模板独立(好像后来,Limodou的Uliweb将这个库改造后独立了出来),且尚确 是不少功能,就放弃了。

今天看到PyTenjin又有了那种感觉:).

好了,来看代码,

<?py # -*- coding: utf-8 -*- ?>
<?py
from setting import *
from tenjin.helpers.html import *
?>
<?xml version=”1.0″ encoding=”utf-8″?>
<feed xmlns=”http://www.w3.org/2005/Atom”>
<title>#{ SITE_TITLE }</title>
<subtitle>#{SITE_SUB_TITLE}</subtitle>
<link rel=”alternate” type=”text/html” href=”http://#{curdomain}/” />
<link rel=”self” type=”application/atom+xml” href=”http://#{curdomain}/index.xml” />
<id>http://#{curdomain}/</id>
<updated>#{site_updated}</updated>
<rights>Copyright © 2010, #{ SITE_TITLE }</rights>
<?py for topic in posts: ?>
<entry>
<title>${ topic.title}</title>
<link rel=”alternate” type=”text/html” href=”http://#{curdomain}/p/#{topic.key().id()}” />
<id>tag:#{curdomain},#{topic.created.strftime(“%Y-%m-%d)”) }}:/p/#{topic.key().id()}</id>
<published>#{topic.created.strftime(“%Y-%m-%dT%H:%M:%SZ”)}</published>
<updated>#{topic.last_modified.strftime(“%Y-%m-%dT%H:%M:%SZ”)}</updated>
<author>
<name>#{topic.author_name}</name>
<uri>http://#{curdomain}/member/#{topic.author_name}</uri>
</author>
<content type=”html” xml:base=”http://#{curdomain}/” xml:lang=”en”><![CDATA[
#{urlize(myimg(nl2br(topic.short_con)))}
]]></content>
</entry>
<?py #endfor ?>
</feed>

是不是很想当然!

1,单行语句python代码 <?py  xxxxx  ?>

2,块Block语句 python代码

<?py if xxxx?>

XXXXX

<?py #endif?>

————————————————

<?py for xxx ?>

<?py #endfor?>

3, 变量 #{}不含HTML Escape转换 和 ${} 含HTML Escape转换

4, 条件判断表达式必须和if, while ..等在一行写

5,Python代码的缩进必须是4个空格

6, <?PY ?>里是预编译的语句,这些语句只会被执行一次,里面的变量和函数不能被<?py ?>、#{}和${}所用,也不能使用<?py ?>和模板参数中的变量和函数。
7, #{{}}和${{}}用法和#{}、${}类似,但只能使用<?PY ?>中的变量和函数。
8, 子模板可以用include()函数载入。

调用生成hmtl字串和习惯很相象

engine = tenjin.Engine(path=[os.path.join(‘template’, THEME), ‘template’], cache=tenjin.MemoryCacheStorage())
return engine.render(template, context, globals, layout)

原文在:http://hi.baidu.com/ghiewa/item/2f4ecf300306d3b8623affec

2024年四月
« 5月    
1234567
891011121314
15161718192021
22232425262728
2930