ObpLovelyPython/ABTEURASIA

status

校对

沈崴

完成度 100%

Eurasia3

关注高性能的原创框架

作者

概述

Eurasia 沿革

Eurasia3 简介

   1 from eurasia.web import config, mainloop, Response
   2 def controller(request):
   3     response = Response(request)
   4     response['Content-Type'] = 'text/plain'
   5     response.write('hello world!')
   6     response.close()
   7 
   8 config(controller=controller, port=8080)
   9 mainloop()

表单词典       = Form(request)
文件句柄       = SimpleUpload(request)
请求头部       = request['Http-Header']
请求报文       = request.read(size) / request.readline(size)

   1 from shelve2 import open, Persistent, BTree
   2 
   3 # 定义持久化对象 (Persistent) User, User 对象可以直接以对象形式保存在数据库中,
   4 # 不需要进行对象关系映射 (Object Relational Mapping), 这也是对象数据库的特点
   5 #
   6 class User(Persistent):
   7     def __init__(self, username, password):
   8         self.username = username
   9         self.password = password
  10 
  11     def hello(self):
  12         print 'Hello Im %s, can I make friends with you?' %self.username
  13 
  14 db = open('test.fs', 'c')     # 创建并打开数据库 "test.fs"
  15 db['user'] = db.new(BTree)()  # 创建 BTree 结点 "user", 相当于在关系数据库中建表
  16 obj = db.new(User)('william', '******') # 创建一个 User 对象
  17 db['user']['william'] = obj   # 把 User 对象存入数据库
  18 db.close()
  19 
  20 db = open('test.fs')          # 重新打开数据库
  21 db['user']['william'].hello() # 访问数据库中的对象

   1 # -*- coding: utf-8 -*-
   2 html = '''\ # HTML 页面
   3 HTTP/1.1 200 OK
   4 Content-Type: text/html
   5 
   6 <html>
   7 <head>
   8     <title>Comet Example</title>
   9 </head>
  10 <body>
  11 <script language="JavaScript">
  12 
  13 // 待会服务器会远程调用这个函数
  14 function message(msg)
  15 {
  16     confirm(msg);
  17 };
  18 
  19 </script>
  20 <!-- 建立 Comet 长连接, 借助 iframe -->
  21 <iframe src="comet" style="display: none;"></iframe>
  22 </body>
  23 </html>'''
  24 
  25 import eurasia
  26 from eurasia.web import config, mainloop, Comet
  27 
  28 sleep = eurasia.modules['time'].sleep
  29 
  30 def controller(request):
  31     # 输出普通页面 (当 URL 不是 comet 时)
  32     if request.path[-5:] != 'comet':
  33         request.write(html)
  34         request.close()
  35         return
  36 
  37     browser = Comet(request)
  38     browser.begin()           # 开始和客户端之间的通讯
  39     browser.message('start')  # 使用原生 Python 代码直接调用浏览器端
  40                               # 名为 "message" 的 JavaScript 函数
  41                               # 发送消息 "start"
  42 
  43     sleep(2)                  # 每隔 2 秒调用一次客户端 message 函数
  44     for i in xrange(1, 3):
  45         browser.message(i)
  46         sleep(2)
  47 
  48     browser.message('finish')
  49     browser.end()             # 断开长连接
  50 
  51 config(controller=controller,
  52     port = 8080, verbose=True)
  53 mainloop()

Eurasia3 VS Django

   1 from eurasia.web import config, mainloop
   2 def echo(sockfile):
   3     while True:
   4         data = sockfile.readline(1024)
   5         if data == 'quit':
   6             sockfile.close()
   7             break
   8         else:
   9             sockfile.write(data)
  10 
  11 config(tcphandler=echo, port=8080) # 与前面不同, 这里使用 tcphandler
  12 mainloop()

开始使用 Eurasia3


反馈

创建 by -- ::-- ZoomQuiet [2008-09-30 14:18:41]

ObpLovelyPython/ABTEURASIA (last edited 2009-12-25 07:15:42 by localhost)