1 from Plugins.Plugin import PluginDescriptor
5 # set DEBUG to True, if twisted should write logoutput to a file.
7 DEBUGFILE= "/tmp/twisted.log"
9 # Passwordprotection Test
10 # set it only to True, if you have a patched wrapper.py
11 # see http://twistedmatrix.com/trac/ticket/2041
12 # in /usr/lib/python2.4/site-packages/twisted/web2/auth/wrapper.py
13 # The solution is to change this line
15 # return self.authenticate(req), seg[1:]
17 # return self.authenticate(req), seg
18 PASSWORDPROTECTION = False
19 PASSWORDPROTECTION_pwd = "root"
20 PASSWORDPROTECTION_mode = "md5-sess";
21 # twisted supports more than sha ('md5','md5-sess','sha')
22 # but only sha works for me, but IE
23 # sha, Firefox=ok, Opera=ok, wget=ok, ie=not ok
24 # md5-sess, firefox=not ok, opera=not ok,wget=ok, ie=not ok
25 # md5 same as md5-sess
28 from twisted.internet import reactor
29 from twisted.web2 import server, channel, static, resource, stream, http_headers, responsecode, http
30 from twisted.python import util
33 class ScreenPage(resource.Resource):
34 def __init__(self, path):
37 def render(self, req):
40 return http.Response(200, stream="please wait until enigma has booted")
42 class myProducerStream(stream.ProducerStream):
43 closed_callback = None
46 if self.closed_callback:
47 self.closed_callback()
48 stream.ProducerStream.close(self)
50 s = myProducerStream()
51 webif.renderPage(s, self.path, req, sessions[0]) # login?
53 return http.Response(stream=s)
55 def locateChild(self, request, segments):
56 path = '/'.join(["web"] + segments)
61 return ScreenPage(path), ()
63 class Toplevel(resource.Resource):
66 def render(self, req):
67 return http.Response(responsecode.OK, {'Content-type': http_headers.MimeType('text', 'html')},
68 stream='Hello! You want go to <a href="/web/">OSD</a> instead.')
70 child_web = ScreenPage("/") # "/web"
71 child_hdd = static.File("/hdd")
72 child_webdata = static.File(util.sibpath(__file__, "web-data")) # FIXME: web-data appears as webdata
74 if PASSWORDPROTECTION is False:
75 site = server.Site(Toplevel())
77 from twisted.cred.portal import Portal
78 from twisted.cred import checkers
79 from twisted.web2.auth import digest, basic, wrapper
80 portal = Portal(HTTPAuthRealm())
81 checker = checkers.InMemoryUsernamePasswordDatabaseDontUse(root=PASSWORDPROTECTION_pwd)
82 portal.registerChecker(checker)
83 root = wrapper.HTTPAuthResource(Toplevel(),
84 (basic.BasicCredentialFactory('DM7025'),digest.DigestCredentialFactory(PASSWORDPROTECTION_mode,'DM7025')),
86 site = server.Site(root)
87 reactor.listenTCP(80, channel.HTTPFactory(site))
89 # start classes for PASSWORDPROTECTION
90 from zope.interface import Interface, implements
91 from twisted.cred import portal
92 class IHTTPUser(Interface):
95 class HTTPUser(object):
98 class HTTPAuthRealm(object):
99 implements(portal.IRealm)
101 def requestAvatar(self, avatarId, mind, *interfaces):
102 if IHTTPUser in interfaces:
103 return IHTTPUser, HTTPUser()
105 raise NotImplementedError("Only IHTTPUser interface is supported")
106 # end classes for PASSWORDPROTECTION
108 def autostart(reason, **kwargs):
109 if "session" in kwargs:
111 sessions.append(kwargs["session"])
117 in normal console output, twisted will print only the first Traceback.
118 is this a bug in twisted or a conflict with enigma2?
119 with this option enabled, twisted will print all TB to the logfile
120 use tail -f <file> to view this log
123 from twisted.python.log import startLogging
124 print "start twisted logfile, writing to %s" % DEBUGFILE
125 startLogging(open(DEBUGFILE,'w'))
129 print "twisted not available, not starting web services"
131 def Plugins(**kwargs):
132 return PluginDescriptor(where = [PluginDescriptor.WHERE_SESSIONSTART, PluginDescriptor.WHERE_AUTOSTART], fnc = autostart)