Eurasia/使用durus数据库
来自站长百科
模板:Eurasia top 通过 socket2.install() 可以使 eurasia 和 durus 实现兼容。
使用durus数据库[ ]
from durus.file_storage import FileStorage from durus.storage_server import StorageServer storage = FileStorage('test.db') server = StorageServer(storage=storage, address='test.sock') server.serve()
在 eurasia 中通过 ClientStorage 连接数据库服务器。
from eurasia import socket2 socket2.install() from time import time from eurasia.web import httpserver, mainloop form durus.persistent import Persistent from durus.connection import Connection from durus.client_storage import ClientStorage class Guest(Persistent): def __init__(self, addr, port): self.addr = addr self.port = port def handler(httpfile): conn = Connection(ClientStorage(address='test.sock')) root = conn.get_root() addr = httpfile.environ['REMOTE_ADDR'] port = httpfile.environ['REMOTE_PORT'] guest = Guest(addr, port) root[int(time())] = guest httpfile.start_response('200 OK', [('Content-Type', 'text/plain')]) httpfile.sendall('added.') httpfile.close() conn.commit()
- socket2.install() 必须在导入 durus 之前调用
- 这个程序将每个访问者的地址和 port 存入数据库(使用 time() 做 key)