coding utf-8 import os path os sys from twisted web2 import log wsgi r

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# -*- coding: utf-8 -*-
import os.path, os, sys
from twisted.web2 import log, wsgi, resource, iweb
from twisted.internet import reactor
from django.core.handlers.wsgi import WSGIHandler
from django.core.servers.basehttp import AdminMediaHandler
os.environ['DJANGO_SETTINGS_MODULE'] = 'test_web.settings' # вот сюда имя проекта вбить
# default settings.
SSL_PRIVATE_KEY_PATH = None
LOG_STYLE = "apache"
#for k, v in os.environ.items() :
# if k.startswith("DOT_") :
# locals()[k.replace("DOT_", "")] = v
if not os.path.isdir("log") :
os.mkdir("log")
def write_pid (file_pid) :
fd = file(file_pid, "w")
fd.write(str(os.getpid()))
fd.close()
write_pid("log/repository.pid")
def make_wsgi (req) :
# twisted.web2 can not insert 'PATH_INFO' into environment.
os.environ["PATH_INFO"] = req.path
return wsgi.WSGIResource(AdminMediaHandler(WSGIHandler()))
class ArbitrarySettingsDecide (resource.Resource) :
addSlash = True
def locateChild (self, request, segments) :
return self, ()
def renderHTTP (self, request) :
return make_wsgi(request)
LOG_FORMAT = \
"""
========================================================
Call Type: %s
........................................................
User From: %s
Header: %s
Status: %s
Byte Sent: %d
........................................................
User-Agnet: %s
Referer: %s
"""
LOG_FORMAT_APACHE = "%s - %s [%s] \"%s\" %s %d \"%s\" \"%s\""
class AccessLoggingObserver (log.DefaultCommonAccessLoggingObserver) :
def start (self, log_type="") :
self.log_type = log_type
log.DefaultCommonAccessLoggingObserver.start(self)
def emit (self, eventDict) :
if eventDict.get("interface") is not iweb.IRequest:
return
request = eventDict["request"]
response = eventDict["response"]
loginfo = eventDict["loginfo"]
firstLine = "%s %s HTTP/%s" %(
request.method,
request.uri,
".".join([str(x) for x in request.clientproto]))
if self.log_type == "apache" :
self.logMessage( \
LOG_FORMAT_APACHE %( \
request.remoteAddr.host, \
# XXX: Where to get user from? \
"-", \
self.logDateString( \
response.headers.getHeader("date", 0)), \
firstLine, \
response.code, \
loginfo.bytesSent, \
request.headers.getHeader("referer", "-"), \
request.headers.getHeader("user-agent", "-") \
) \
)
else :
self.logMessage( \
LOG_FORMAT % (
request.headers.getRawHeaders("x-requested-with") or "",
request.remoteAddr.host,
# XXX: Where to get user from?
#self.logDateString(response.headers.getHeader("date", 0)),
firstLine,
response.code,
loginfo.bytesSent,
request.headers.getHeader("user-agent", "-"),
request.headers.getHeader("referer", "-"),
)
)
# This part gets run when you run this file via: "twistd -noy demo.py"
if __name__ == "__builtin__":
from twisted.application import service, strports
from twisted.web2 import server, vhost
from twisted.web2 import channel
__port = os.getenv("ENV_WWW_PORT", "8000") # здесь номер порта
choose_site = ArbitrarySettingsDecide()
res = log.LogWrapperResource(choose_site)
AccessLoggingObserver().start(LOG_STYLE)
# Create the site and application objects
site = server.Site(res)
application = service.Application("test_web:%s" % __port)
##################################################
# HTTP FastCGI, Serve it via standard HTTP on port 1026
#s = strports.service("tcp:%s" % __port, channel.FastCGIFactory(site))
#s.setServiceParent(application)
# HTTPs, Serve it via standard HTTP on port 8081
if not SSL_PRIVATE_KEY_PATH :
# HTTP, Serve it via standard HTTP on port 8000
s = strports.service("tcp:%s" % __port, channel.HTTPFactory(site))
s.setServiceParent(application)
else :
from twisted.internet.ssl import DefaultOpenSSLContextFactory
s = strports.service( \
"ssl:%s:privateKey=%s" % (__port, SSL_PRIVATE_KEY_PATH), \
channel.HTTPFactory(site) \
)
s.setServiceParent(application)