~divmod-dev/divmod.org/trunk

« back to all changes in this revision

Viewing changes to Nevow/nevow/zomnesrv.py

  • Committer: Jean-Paul Calderone
  • Date: 2014-06-29 20:33:04 UTC
  • mfrom: (2749.1.1 remove-epsilon-1325289)
  • Revision ID: exarkun@twistedmatrix.com-20140629203304-gdkmbwl1suei4m97
mergeĀ lp:~exarkun/divmod.org/remove-epsilon-1325289

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
import warnings
3
 
warnings.warn("nevow.zomnesrv is deprecated.", category=DeprecationWarning)
4
 
 
5
 
import time
6
 
 
7
 
from nevow import wsgi
8
 
 
9
 
from twisted.internet import protocol
10
 
from twisted.protocols import basic
11
 
from twisted.python import log
12
 
 
13
 
 
14
 
IN_KEY = 'STDIN_FILENAME='
15
 
IN_KEY_LEN = len(IN_KEY)
16
 
 
17
 
 
18
 
class ZomneProtocol(basic.NetstringReceiver):
19
 
    def connectionMade(self):
20
 
        self.environ = {}
21
 
 
22
 
    def stringReceived(self, data):
23
 
        key, value = data.split('=', 1)
24
 
        self.environ[key] = value
25
 
        if data.startswith(IN_KEY):
26
 
            filenm = data[IN_KEY_LEN:]
27
 
            self.stdin = open(filenm).read()
28
 
 
29
 
            # WSGI variables
30
 
            self.environ['wsgi.version']      = (1,0)
31
 
            self.environ['wsgi.multithread']  = False
32
 
            self.environ['wsgi.multiprocess'] = False
33
 
            if self.environ.get('HTTPS','off') in ('on','1'):
34
 
                self.environ['wsgi.url_scheme'] = 'https'
35
 
            else:
36
 
                self.environ['wsgi.url_scheme'] = 'http'
37
 
 
38
 
            # print "ENV", self.environ
39
 
            result = self.factory.application(self.environ, self.start_response)
40
 
            for data in result:
41
 
                if data:
42
 
                    self.write(data)
43
 
            ## We got everything, let's render the request
44
 
            self.transport.loseConnection()
45
 
 
46
 
            self.factory.log('%s - - %s "%s" %d %s "%s" "%s"' % (
47
 
                self.environ['REMOTE_ADDR'],
48
 
                time.strftime("[%d/%b/%Y:%H:%M:%S +0000]", time.gmtime()),
49
 
                '%s %s %s' % (
50
 
                    self.environ['REQUEST_METHOD'],
51
 
                    self.environ['REQUEST_URI'],
52
 
                    self.environ['SERVER_PROTOCOL']),
53
 
                self.responseCode,
54
 
                self.sentLength or "-",
55
 
                self.environ.get('HTTP_REFERER', ''),
56
 
                self.environ.get('HTTP_USER_AGENT', '')))
57
 
 
58
 
    sentLength = 0
59
 
    def write(self, what):
60
 
        self.sentLength += len(what)
61
 
        self.transport.write(what)
62
 
 
63
 
    def start_response(self, status, headers, exc_info=None):
64
 
        self.responseCode = int(status.split()[0])
65
 
        self.transport.write("Status: %s\r\n" % (status, ))
66
 
        for key, value in headers:
67
 
            self.transport.write("%s: %s\r\n" % (key, value))
68
 
        self.transport.write("\r\n")
69
 
        return self.write
70
 
 
71
 
 
72
 
class NotificationProtocol(protocol.Protocol):
73
 
    def connectionMade(self):
74
 
        self.transport.loseConnection()
75
 
 
76
 
 
77
 
class NotificationFactory(protocol.ClientFactory):
78
 
    protocol = NotificationProtocol
79
 
 
80
 
 
81
 
class ZomneFactory(protocol.Factory):
82
 
    def __init__(self, root, logfile=None, prefixURL=None):
83
 
        """`prefixURL` is used by WSGI apps. wsgi.py stores it in appRootURL.
84
 
        It is the HTTP url for the nevow.cgi script"""
85
 
        if logfile is not None:
86
 
            self.log = open(logfile, 'a')
87
 
        if prefixURL:
88
 
            self.application = wsgi.createWSGIApplication(root, prefixURL)
89
 
        else:
90
 
            self.application = wsgi.createWSGIApplication(root)
91
 
 
92
 
    protocol = ZomneProtocol
93
 
 
94
 
    def startFactory(self):
95
 
        """Tell the other end that we are done starting up.
96
 
        """
97
 
        # Import reactor here to avoid installing default at startup
98
 
        from twisted.internet import reactor
99
 
        reactor.connectUNIX('zomne_startup_complete.socket', NotificationFactory())
100
 
 
101
 
    def log(self, msg):
102
 
        log.msg(msg)