This repository has been archived by the owner on Oct 12, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
MultipleHttpServers
115 lines (99 loc) · 3.4 KB
/
MultipleHttpServers
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
= Multiple HTTP Servers =
Implementing multiple HTTP servers as described in !CherryPy Essentials is no longer supported (see ticket #752, not there anymore?). I have been able to develop an alternative method that replaces it.
This method uses a configuration file, so you will have to rework it if you want the configuration data to be hard-coded.
Here is an example of the `httpservers.ini` configuration file:
{{{
#httpservers.ini
[HTTP_Addresses]
Host_1:192.168.0.10
Port_1:8080
# localhost in any case will be changed to 127.0.0.1
Host_2:LOCALHOST
Port_2:8008
# IPv6 may not be valid on old systems
Host_3:[::1]
# If blank or nonexistent will default to 80
Port_3:
}}}
Note that this is a standard format `ini` file, not the !CherryPy version that requires values that are Python datatypes.
This file goes in the same folder as the sample page below.
Here is the source code for a sample page:
{{{
#!python
import ConfigParser
import cherrypy
from cherrypy import _cpserver
from cherrypy import _cpwsgi_server
class Home:
def index(self):
return 'Hello World from multiple HTTP addresses'
index.exposed=True
oConf=ConfigParser.ConfigParser()
oConf.read('httpservers.ini')
i=0;bFirst=True;lXSvrs=[];sSect='HTTP_Addresses'
while True:
i+=1
sOptHost='Host_%i'%i
sOptPort='Port_%i'%i
if oConf.has_option(sSect,sOptHost):
sHost=oConf.get(sSect,sOptHost)
if len(sHost)>0:
if sHost.lower()=='localhost':
sHost='127.0.0.1'
try:
iPort=oConf.getint(sSect,sOptPort)
except:
iPort=80
if bFirst:
bFirst=False
cherrypy.config.update({
#'environment':'production',
'server.socket_host':sHost,
'server.socket_port':iPort,
'log.access_file':'access.log',
'log.error_file':'error.log',
'log.screen':True
})
else:
lXSvrs.append(_cpwsgi_server.CPWSGIServer())
lXSvrs[-1].bind_addr=(sHost,iPort)
lXSvrs.append(_cpserver.ServerAdapter(cherrypy.engine,lXSvrs[-1],(sHost,iPort)))
lXSvrs[-1].subscribe()
else:
break
try:
del oConf,i,bFirst,sSect,sOptHost,sOptPort,sHost,iPort
except:
raise RuntimeError,'ABORT: httpservers.ini file is invalid.'
cherrypy.tree.mount(Home(),'/',config={
'/':{
'tools.response_headers.on':True,
'tools.response_headers.headers':[
('Content-Type','text/plain')
]
}
})
cherrypy.engine.start()
cherrypy.engine.block()
}}}
----
Here's that same code stripped down to the bare essentials, showing the addition of an SSL port:
{{{
#!python
import cherrypy
from cherrypy import _cpserver
from cherrypy import _cpwsgi_server
secure_server = _cpwsgi_server.CPWSGIServer()
secure_server.bind_addr = ('0.0.0.0', 443)
secure_server.ssl_certificate = '/etc/myapp/server.pem'
secure_server.ssl_private_key = '/etc/myapp/server.pem'
adapter = _cpserver.ServerAdapter(cherrypy.engine, secure_server, secure_server.bind_addr)
adapter.subscribe()
cherrypy.config.update({
'server.socket_host': '0.0.0.0',
'server.socket_port': 80,
})
cherrypy.engine.start()
cherrypy.engine.block()
}}}
''--fumanchu''