-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathsimple_http.py
More file actions
executable file
·50 lines (37 loc) · 1.48 KB
/
simple_http.py
File metadata and controls
executable file
·50 lines (37 loc) · 1.48 KB
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
# How to start it:
# on windows: twistd.py --python simple_http.py
# on linux: twistd --python simple_http.py
from twisted.web import http
from twisted.python import log
from twisted.application import internet, service
class MyRequestHandler(http.Request):
pages = {
'/': '<h1>Home</h1>Home Page',
'/test': '<h1>Test</h1>Test Page',
}
def process(self):
log.msg("Client " + self.getClientIP() + " request " + str(self.args))
if self.path in self.pages:
self.write(self.pages[self.path])
else:
self.setResponseCode(http.NOT_FOUND)
self.write("<h1>Not Found</h1>Sorry, no such page.")
self.finish()
class MyHttp(http.HTTPChannel):
requestFactory = MyRequestHandler
class MyHttpFactory(http.HTTPFactory):
protocol = MyHttp
# configuration parameters
port = 10000
listen_ip = 'localhost'
# this will hold the services that combine to form the poetry server
top_service = service.MultiService()
# the tcp service connects the factory to a listening socket. it will
# create the listening socket when it is started
factory = MyHttpFactory()
tcp_service = internet.TCPServer(port, factory, interface=listen_ip)
tcp_service.setServiceParent(top_service)
# this variable has to be named 'application'
application = service.Application("simple_http")
# this hooks the collection we made to the application
top_service.setServiceParent(application)