django静态url
June 15th, 2009 by skysbird
urlpatterns.append( url(r'^$', 'django.views.static.serve', dict(document_root='static', path='index.html')))
June 15th, 2009 by skysbird
urlpatterns.append( url(r'^$', 'django.views.static.serve', dict(document_root='static', path='index.html')))
May 17th, 2009 by skysbird
在使用twisted的application模式编程的时候,往往会用到twisted的log系统。该系统会将stdout重载,加入自己的机制,然而该机制对于unicode字符串的编码有一些问题。
比如在程序中print了一条string
s = u’中文’
print s
也许会很惊奇的得到下面这个异常
exceptions.UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in position 60: ordinal not in range(128) 这个异常是因为 当将一个unicode对象传给print时,在内部会将该unicode对象进行一次转换,转换成本地的默认编码(这仅是个人猜测)。 在python中str和unicode在编码和解码过程中,如果将一个str直接编码成另一种编码,会先把str解码成unicode,采用的编码为默认编码,一般默认编码是anscii,所以在上面示例代码中第一次转换的时候会出错,当设定当前默认编码为'gbk'后,就不会出错了。 在程序的入口处,设置默认python编码,方法如下: import sys reload(sys) sys.setdefaultencoding('gbk')
April 26th, 2009 by skysbird
出自:http://blog.csdn.net/magicbreaker/archive/2008/03/09/2159289.aspx
傻B式的Vista,折磨人的操作系统收藏
用了2个月VISTA之后,我坚定的把它给卸载了。虽然还是正版的business。我一直觉得微软很人性化,98到2000,2000到XP,每次升级我都是很高兴的,可这次升级我实在太痛苦了。首先是该死的UAC,我启动一个程序它给我跳出来耀武扬威一番,刚装上系统这个功能就被我和谐了。另外启动慢,任务管理器里乱七八糟我都不认识,没启动过的程序横七竖八的在里面躺着。速度慢,打补丁时间长,对硬件要求高,兼容性差都不说啥了,你把我们已经熟悉的管理配置工具搞得面目全非算什么呢?
另外还有其他朋友总结出来的:
1、注册表变得更加臃肿了
注册表对于系统速度的影响,有点说不清道不明。微软设计注册表,给应用程序一个统一存放配置信息的地方,因为注册表在系统启动时加载,所以应用程序在启动时,使用注册表项比使用外部配置文件要快。但是如果注册表太大的话,将消耗很多的系统资源,为什么有那么多注册表清理软件存在,因为注册表垃圾太多了,不光是应用软件制造注册表垃圾,Windows自己也制造注册表垃圾。在制造注册表垃圾的软件当上,微软的软件应该是名列前矛,像Office、.NET Framework,都是写注册表达数量巨大的软件。Windows Vista的注册表,据估计至少比Windows XP臃肿一倍。
2、预装了更多的字体
Windows XP/2003只预装了60种左右的TrueType字体,到了Windows Vista,居然达到了190种之多,数量翻了三倍还多,大家应该知道,系统安装的字体越多,越影响速度。根据这篇文章的测试,安装100种字体,系统延迟增加5.33秒,Windows Vista预装190多种字体,那么系统启动速度将会增加延迟近10秒。而且问题是,这些字体我们需要吗?我们需要用这些字体写文档吗?需要用它们来做设计吗?基本上是不需要的。微软推出什么所谓的雅黑字体,号称花费上亿美元,一个字100美元,抄袭Mac OSX,也只是东施效颦而已,Windows Vista的界面,还是那么丑陋,和Mac OSX根本没法比。而且更搞笑的是,居然传出Windows Vista某个字体有缺字的情况。
3、捆绑了不止一个的.NET运行环境
这个东西是个速度杀手,而且是一般用户所不需要的。搞软件开发的都知道,.NET用来做Web应用还好,但是如果用来做桌面应用,是不好的,它实在太慢了,用户不能忍受一个启动时慢吞吞的,处理工作时延迟太久。所以到目前为止,用.NET开发的桌面应用软件仍然是少之又少,就像微软,也不敢用它来做Office的主要开发工具,最多只是在一些小功能上用到.NET。.NET的可恶之处,在于它与系统紧密结合,所以十分影响操作系统的速度。首先它在安装的时候写了太多的注册表项,至少几万条。第二点就是它在Windows启动时就加载了许多DLL,微软为了造成.NET速度快的假像,于是就在Windows启动时预先加载.NET的DLL(手法如同Office)。而且,Windows Vista还不止预装一个.NET运行环境。不信你去C:WINDOWSMicrosoft.NETFramework这个目录看一下,看有几个以版本号命名的目录。相比之下,.NET的对手Java就不是这样,在Windows平台上,它只注册了很少的DLL,而且并不在系统启动时加载。在非Windows平台上,Java还可以是绿色软件。
4、使用了更加复杂、花哨的界面技术
Windows Vista使用了所谓的Aero、Flip 3D界面技术,可惜这些东西并不能给用户带来方便,而且外观设计并不优秀,甚至比较丑陋。最可恶的一点是,它们消耗极大的系统资源。微软的界面技术,又是东施效颦,越效越恶心。没想到微软这种保守的跟屁虫公司居然敢把任务栏和标题栏做成黑色调,菜单栏做成青色,控制按钮搞得大小不一,而且尺寸加大,变成扁平状,这种设计好像很大胆,很有创意,但是不难发现,这种设计十分丑陋。如果说Mac OSX就像水晶一样美丽的话,那么Windows Vista看起来就像劣质的塑料和破麻布一般丑陋,要知道Mac OSX的界面那是6年前的设计呢。微软东施效颦算是效到底了,这回把图标也搞大了,可是那些图标除了带来系统垃圾以外,有什么用呢,谁要用你的大图标啊,再看看shell32.dll里面,新图标旧图标夹在一块,就是那么的不协调。什么Aero、Flip 3D,不仅是视觉垃圾,更是系统垃圾。
April 24th, 2009 by skysbird
出自:http://app.arat.us/blog/?p=112
Writing an AMQP connector with Python, Twisted, Trial and txAMQP
Published on April 24th, 2009 by Dan Reverri in apparatus Tools:
In this article we’ll be writing an AMQP connector that we can re-use in future AMQP based projects. We’ll be using the Twisted testing component Trial to test our code as we develop it. To start let’s create our project directory, setup a test case, and run our first test.
mkdir connector
cd connector
mkdir test
vi test/test.py
The resulting directory structure will be:
connector/
connector/test/
connector/test/test.py
In test.py:
from twisted.trial import unittest
class AmqpConnectorTest(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_connector_init(self):
connector = AmqpConnector()
Now we can run our first test and enjoy the sweet, sweet failure. Note that tests are run from within the connector directory.
trial test/test.py
test
AmqpConnectorTest
test_connector_init … [ERROR]
===============================================================================
[ERROR]: test.AmqpConnectorTest.test_connector_init
Traceback (most recent call last):
File “test/test.py”, line 11, in test_connector_init
connector = AmqpConnector()
exceptions.NameError: global name ‘AmqpConnector’ is not defined
——————————————————————————-
Ran 1 tests in 0.018s
FAILED (errors=1)
Ok, let’s fix this by implementing connector.py in the connector/ directory.
class AmqpConnector():
pass
Our directory structure is now:
connector/
connector/connector.py
connector/test/
connector/test/test.py
We update our test to import the new connector and run trial.
from connector import AmqpConnector
from twisted.trial import unittest
class AmqpConnectorTest(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_connector_init(self):
connector = AmqpConnector()
trial test/*.py
test
AmqpConnectorTest
test_connector_init … [OK]
——————————————————————————-
Ran 1 tests in 0.003s
PASSED (successes=1)
Thinking about what our connector must do and referencing the examples provided with txAMQP we’ll implement tests for the following:
* connect
* authenticate
* channel_open
And here are the tests:
from twisted.trial import unittest
from twisted.internet.defer import inlineCallbacks
from connector import AmqpConnector
class AmqpConnectorTest(unittest.TestCase):
def setUp(self):
self.connector = AmqpConnector()
def tearDown(self):
pass
@inlineCallbacks
def test_connect(self):
connector = self.connector
yield connector.connect()
@inlineCallbacks
def test_authenticate(self):
connector = self.connector
yield connector.connect()
yield connector.authenticate()
@inlineCallbacks
def test_channel_open(self):
connector = self.connector
yield connector.connect()
yield connector.authenticate()
yield connector.channel_open()
You’ll notice that I’ve added an additional import to provide the inlineCallbacks decorator. This allows us to write asynchronous code as if it were synchronous code. I’ve also moved the connector initialization to setUp() and removed the init test. The setUp() method is run before each test and the tearDown() method is run after each test. This means that each test method will be passed a new connector object to work with (self.connector).
To get an idea of how to start writing our connector lets review the test code that comes with txAMQP. In a previous article I went over fetching txAQMP from launchpad. If you have not done that yet you should do it now. Review the file “src/txamqp/testlib.py”. We’re going to “borrow” the init() and connect() methods but re-arrange it a bit to move authentication into a separate method.
I’ve also changed the reference to the spec file. We’ll need to copy “amqp0-8.xml” into the connector directory.
from twisted.internet.defer import inlineCallbacks, Deferred
from txamqp.protocol import AMQClient, TwistedDelegate
from twisted.internet import protocol, reactor
import txamqp.spec
class AmqpConnector():
def __init__(self):
self.host = “localhost”
self.port = 5672
self.spec = “amqp0-8.xml”
self.user = “guest”
self.password = “guest”
self.vhost = “/”
@inlineCallbacks
def connect(self,host=None,port=None,spec=None,vhost=None):
host = host or self.host
port = port or self.port
spec = spec or self.spec
vhost = vhost or self.vhost
delegate = TwistedDelegate()
onConn = Deferred()
f = protocol._InstanceFactory(reactor, AMQClient(delegate, vhost, txamqp.spec.load(spec)), onConn)
self.cnx = reactor.connectTCP(host, port, f)
self.connection = yield onConn
@inlineCallbacks
def authenticate(self, user=None, password=None):
user = user or self.user
password = password or self.password
yield self.connection.start({”LOGIN”: user, “PASSWORD”: password})
@inlineCallbacks
def channel_open(self):
self.channel = yield self.connection.channel(1)
yield self.channel.channel_open()
When I first ran these tests they all failed:
1
exceptions.IOError: [Errno 2] No such file or directory: ‘amqp0-8.xml’
This stumped me for a while until I realized local files were being searched for in the “_trial_temp” directory that Trial uses. A simple fix to setUp() resolved the issue:
def setUp(self):
self.connector = AmqpConnector()
self.connector.spec = “../amqp0-8.xml”
Please remember that these tests assume you have a RabbitMQ instance running locally on port 5672 that responds to the user “guest” with the password “guest”. I only mention that because my next test run failed because RabbitMQ was not running.
With RabbitMQ running the next test run with slightly more successful:
test
AmqpConnectorTest
test_authenticate … [OK]
[ERROR]
test_channel_open … [OK]
[ERROR]
test_connect … [OK]
[ERROR]
===============================================================================
[ERROR]: test.AmqpConnectorTest.test_authenticate
Traceback (most recent call last):
Failure: twisted.trial.util.DirtyReactorAggregateError: Reactor was unclean.
Selectables:
<
===============================================================================
[ERROR]: test.AmqpConnectorTest.test_channel_open
Traceback (most recent call last):
Failure: twisted.trial.util.DirtyReactorAggregateError: Reactor was unclean.
Selectables:
<
===============================================================================
[ERROR]: test.AmqpConnectorTest.test_connect
Traceback (most recent call last):
Failure: twisted.trial.util.DirtyReactorAggregateError: Reactor was unclean.
Selectables:
<
——————————————————————————-
Ran 3 tests in 1.199s
FAILED (errors=3, successes=3)
The message “Reactor was unclean.” indicates our tests are leaving an open connection. We can fix this by closing the connection in tearDown():
@inlineCallbacks
def tearDown(self):
yield self.connector.cnx.disconnect()
One final test run should give us all green lights:
test
AmqpConnectorTest
test_authenticate … [OK]
test_channel_open … [OK]
test_connect … [OK]
——————————————————————————-
Ran 3 tests in 1.195s
PASSED (successes=3)
April 7th, 2009 by skysbird
最近在用twisted做数据手法中间件,感觉lineReceiver的方式不足以满足对底层数据传输时的精确控制,比如压缩,按位传输之类。
因此考虑使用比较底层的dataReceived时间,那么可能需要如下代码来拆包解包。
#!python
import struct
class Test:
def __init__ (self):
self.__buffer = ”
def dataReceived(self, data):
self.__buffer = self.__buffer + data
while len(self.__buffer) >= 4:
(length,) = struct.unpack(’I', self.__buffer[:4])
body = self.__buffer[4:4+length]
if len (body) == length:
self.onMessage (body)
self.__buffer = self.__buffer[4+length:]
elif len(body) < length:
print ‘Pack length not enough, wait for next receive’
break
else:
print ‘Error! Error! Error!’
assert False
print ‘Buffer length:’, len (self.__buffer)
def onMessage (self, msg):
print ‘onMessage:’, msg
March 31st, 2009 by skysbird
出自:http://gashero.yeax.com/?p=49
| 翻译: | gashero |
|---|
Twisted web howto中并没有提及如何创建SSL加密的WEB服务器。其实这个过程很简单,只是需要一些信息而已,所以这里讲一下。
首先,创建SSL服务器需要一个私钥文件,和一个服务器证书。这里假设你是个开发者,只想要一个测试系统,所以也没必要去搞一个正式的公共证书来。OpenSSL就是你想要的。首先,我们生成一个私钥文件:
$ openssl genrsa > privkey.pem
然后生成一个自签名的SLL证书:
$ openssl req -new -x509 -key privkey.pem -out cacert.pem -days 1000
好了,第一个挑战已经搞定了。下一步就是创建服务器了。假设你已经有了一个twisted web服务器,所以你也知道该在哪个端口监听。我们导入 ssl 模块,创建一个SSL服务器上下文,然后调用 reactor 的 listenSSL 方法:
from twisted.internet import reactor,ssl
sslContext=ssl.DefaultOpenSSLContextFactory(
'/path/to/privkey.pem',
'/path/to/cacert.pem',
)
reactor.listenSSL(port,site,contextFactory=sslContext)
同样在 twisted.application 中也很容易创建带有上下文的站点,不过这里就懒得介绍了。
gashero懒得介绍的内容,skysbird来介绍:
在 twisted.application 中,通过如下方式来启用服务器端的SSL支持:
sslContext = ssl.DefaultOpenSSLContextFactory(’./cert/prikey.pem’,’./cert/cacert.pem’)
internet.SSLServer([port number],ISignalFactory(f),contextFactory=sslContext
).setServiceParent(serviceCollection)
gashero附加的内容:
需要使用ssl的功能还需要另外安装一堆东西。包括openssl(0.9.8g)、pyOpenSSL(0.6.0)。安装openssl时,debian源里面没有dev版本,只能自己编译,注意配置时要修改安装路径:
$ ./config --prefix=/usr/local
这样以后就可以正确的编译安装pyOpenSSL了,否则默认情况下找不到OpenSSL。
真不容易,经历3天折腾,代理商让我改ip,说服务器要搬迁,我改了,他们没搬,又不会用non-windows系统,结果生生等了3天,把机器搬过来,才恢复正常。
February 22nd, 2009 by skysbird
加这个插件主要是为了利用一下微软公司这么强大的MSN Space!!!!!!!!!!
November 18th, 2008 by skysbird
November 1st, 2008 by skysbird
网上有朋友按照如下办法解决。
http://www.virtualbox.org/wiki/Linux_Downloads
After adding the ‘hardy non-free’ to sources, downloading the key and registering, run update-manager to download and configure the latest version.
When you start up VirtualBox for the first time, any existing VMs have to be converted to a new format, so make sure you backup.
I haven’t fully tested yet, but it’s working fine and the upgrade was painless.