Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3333593
  • 博文数量: 815
  • 博客积分: 12898
  • 博客等级: 上将
  • 技术积分: 7883
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-25 09:57
文章分类

全部博文(815)

文章存档

2014年(1)

2011年(46)

2010年(192)

2009年(121)

2008年(70)

2007年(385)

分类: Oracle

2010-11-30 18:28:26

#File: twistedTCPServer.py
#Querying database asynchronously with Twisted
 
from twisted.web import resource, server
from twisted.internet import reactor
from twisted.enterprise import adbapi
 
class BlobLoads(resource.Resource):
    def __init__(self, dbconn):
        self.dbconn = dbconn
        resource.Resource.__init__(self)
    def _getBlobs(self, txn, query):
        txn.execute(query)
        return txn.fetchall()
    def render_GET(self, request):
        query = "select id, blobdoc from blob_tab"
        self.dbconn.runInteraction(self._getBlobs, query).addCallback(
            self._writeBlobs, request).addErrback(
            self._exception, request)
        return server.NOT_DONE_YET
    def _writeBlobs(self, results, request):
        request.write("""
       
        BLOBs manipulating
       
         

Writing BLOBs from the database to your disk


         """)
        for id, blobdoc in results:
          request.write("/tmp/picture%s.bmp
" % id)
          blob = blobdoc.read()
          output = open("/tmp/picture%s.bmp" % id, 'wb')
          output.write(blob)
          output.close()
   
        request.write("""
       

Operation completed


       
       
        """)
        request.finish( )
    def _exception(self, error, request):
        request.write("Error obtaining BLOBs: %s" % error.getErrorMessage())
        request.write("""
       

Could not complete operation


       
       
        """)
        request.finish( )
 
class SiteResource(resource.Resource):
    def __init__(self, dbconn):
        resource.Resource.__init__(self)
        self.putChild('', BlobLoads(dbconn))
 
if __name__ == "__main__":
    dbconn = adbapi.ConnectionPool('cx_Oracle', user='usr', password ='pswd', dsn='127.0.0.1/XE')
    site = server.Site(SiteResource(dbconn))
    print "Listening on port 8000"
    reactor.listenTCP(8000, site)
    reactor.run()

阅读(1388) | 评论(0) | 转发(0) |
0

上一篇:twistedsimple.py

下一篇:singlethread.py

给主人留下些什么吧!~~