python twisted编写一个http web服务代码

代码语言:python

所属分类:其他

代码描述:python twisted编写一个http web服务代码,请在python2.7中运行

代码标签: 一个 http web 服务

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

#!/usr/local/python3/bin/python3
# -*- coding: utf-8 -*
from twisted.web import server, resource
from twisted.internet import reactor, defer
from pprint import pprint
import base64

#请在bfwstudio中切换到python2.7版本运行
class DummyServer(resource.Resource):
    isLeaf = True

    def returnContent(self, deferred, request, msg):
        print "Finishing request to '%s'" % request.uri
        request.write(msg)
        request.finish()

    def cancelAnswer(self, err, request, delayedTask):
        print"Cancelling request to '%s': %s" % \
            (request.uri, err.getErrorMessage())
        delayedTask.cancel()

    def render_GET(self, request):
        print "Received request for '%s'" % request.uri
        if request.uri == '/delayed':
            print "Delaying answer for '/delayed'"
            d = defer.Deferred()
            delayedTask = reactor.callLater(60, self.returnContent, d,
                                            request, "Hello, delaye.........完整代码请登录后点击上方下载按钮下载查看

网友评论0