Last active
May 8, 2017 08:50
-
-
Save hightemp/f73369b8c6eeac98b4c78613d4d94fc1 to your computer and use it in GitHub Desktop.
[Python] [Twisted] Mini web-server on twisted
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: UTF8 -*- | |
if __name__=="__main__": | |
from twisted.web.server import * | |
from twisted.web.resource import * | |
from twisted.internet import * | |
from twisted.internet import reactor | |
from twisted.web.error import * | |
from os.path import * | |
import re | |
class Simple(Resource): | |
isLeaf = False | |
dTemplateVars = { | |
"sTitle": "" | |
, "sCurrentFileName": "" | |
, "": "" | |
} | |
dActions = { | |
"/": "index_action" | |
, "/index.html": "index_action" | |
, "/*": "not_found_action" | |
} | |
def getChild(self, sName): | |
return | |
def getChild(self, sName, objRequest): | |
print("[!] getChild", sName) | |
for sAction, sMethodName in self.dActions.iteritems(): | |
sAction = re.sub(r'[\[\]()\\.$^!+-]', '\1', sAction) | |
sAction = re.sub(r'[*]', r'.*?', sAction) | |
sAction = re.sub(r'[?]', r'.{1,1}', sAction) | |
aMatches = re.match(sAction, sName) | |
if aMatches is None: | |
return NoResource() | |
else: | |
return getattr(this, sMethodName) | |
return Resource.getChild(self, sName, objRequest) | |
def render_GET(self, objRequest): | |
aPath = re.sub(r'^/', '', objRequest.path).split(r'/') | |
print("[!] render_GET", aPath) | |
return "render_GET" | |
def default_action(self, objRequest): | |
return "default_action" | |
def index_action(self, objRequest): | |
return "index" | |
def not_found_action(self, objRequest): | |
objRequest | |
return NoResource() | |
objResource = Simple() | |
objWrapped = EncodingResourceWrapper(objResource, [GzipEncoderFactory()]) | |
objSite = Site(objWrapped) | |
reactor.listenTCP(8091, objSite) | |
reactor.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment