" % (i, parameters[i]) for i in parameters }}
9 |
10 |
11 |
--------------------------------------------------------------------------------
/examples/test_wsgiserver.py:
--------------------------------------------------------------------------------
1 | from graily import make_server, init_log
2 | from bottle import route, default_app
3 | import logging
4 |
5 | @route("/")
6 | def test():
7 | return "hello from bottle with Graily"
8 |
9 | def main():
10 | init_log(level=logging.DEBUG)
11 | app = default_app()
12 | server = make_server(("", 8888), app)
13 | server.serve_forever()
14 |
15 | if __name__ == '__main__':
16 | main()
17 |
--------------------------------------------------------------------------------
/examples/test_httpserver.py:
--------------------------------------------------------------------------------
1 | from graily import HTTPServer, BaseHTTPRequestHandler, init_log
2 |
3 | def main():
4 | import logging
5 | init_log(level=logging.DEBUG)
6 |
7 | class Test(BaseHTTPRequestHandler):
8 | def GET(self):
9 | return "hello from graily httpserver"
10 | def POST(self):
11 | return str(self.parameters)
12 |
13 | server = HTTPServer(("", 8888), Test)
14 | server.serve_forever()
15 |
16 | if __name__ == '__main__':
17 | main()
18 |
--------------------------------------------------------------------------------
/examples/test_thread_poll_tcpserver.py:
--------------------------------------------------------------------------------
1 | import time
2 | from graily import ThreadPollTCPServer, Concurrent, StreamRequestHandler, init_log
3 |
4 | def main():
5 | import logging
6 | init_log(level=logging.DEBUG)
7 |
8 | class Echo(StreamRequestHandler):
9 | @Concurrent.register
10 | def dataReceived(self):
11 | time.sleep(5) # won't block the main Thread
12 | self.write(self.data)
13 | server = ThreadPollTCPServer(("", 8888), Echo)
14 | server.serve_forever()
15 |
16 | if __name__ == '__main__':
17 | main()
18 |
--------------------------------------------------------------------------------
/examples/test_tcpserver.py:
--------------------------------------------------------------------------------
1 | from graily import TCPServer, StreamRequestHandler, init_log
2 |
3 | def main():
4 | import logging
5 | init_log(level=logging.DEBUG)
6 |
7 | class Echo(StreamRequestHandler):
8 | def verify_request(self):
9 | # Request finished when received data ends with '.',
10 | # you can also define your own protocol
11 | return bytes([self.iostream._read_buffer[-1]]) == b'.'
12 |
13 | def dataReceived(self):
14 | self.write(self.data)
15 |
16 | server = TCPServer(("", 8888), Echo)
17 | server.serve_forever()
18 |
19 | if __name__ == '__main__':
20 | main()
21 |
--------------------------------------------------------------------------------
/examples/test_graily_tornadowsgi.py:
--------------------------------------------------------------------------------
1 | from graily import Graily, HTTPResponse, template, StaticFileHandler, Concurrent, init_log
2 | import tornado
3 | from tornado import wsgi, httpserver
4 |
5 | def main():
6 | import logging
7 | init_log(level=logging.DEBUG)
8 |
9 | class Test(HTTPResponse):
10 | def get(self):
11 | return "hello from Graily and tornado WSGIServer"
12 |
13 | app = Graily([(r'^.*$', Test)])
14 | container = wsgi.WSGIContainer(app)
15 | http_server = httpserver.HTTPServer(container)
16 | http_server.listen(8888)
17 | tornado.ioloop.IOLoop.instance().start()
18 |
19 | if __name__ == '__main__':
20 | main()
21 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Byte-compiled / optimized / DLL files
2 | __pycache__/
3 | *.py[cod]
4 |
5 | # C extensions
6 | *.so
7 |
8 | # Distribution / packaging
9 | .Python
10 | env/
11 | build/
12 | develop-eggs/
13 | dist/
14 | downloads/
15 | eggs/
16 | .eggs/
17 | lib/
18 | lib64/
19 | parts/
20 | sdist/
21 | var/
22 | *.egg-info/
23 | .installed.cfg
24 | *.egg
25 |
26 | # PyInstaller
27 | # Usually these files are written by a python script from a template
28 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
29 | *.manifest
30 | *.spec
31 |
32 | # Installer logs
33 | pip-log.txt
34 | pip-delete-this-directory.txt
35 |
36 | # Unit test / coverage reports
37 | htmlcov/
38 | .tox/
39 | .coverage
40 | .coverage.*
41 | .cache
42 | nosetests.xml
43 | coverage.xml
44 | *,cover
45 |
46 | # Translations
47 | *.mo
48 | *.pot
49 |
50 | # Django stuff:
51 | *.log
52 |
53 | # Sphinx documentation
54 | docs/_build/
55 |
56 | # PyBuilder
57 | target/
58 |
--------------------------------------------------------------------------------
/examples/test_graily.py:
--------------------------------------------------------------------------------
1 | from graily import Graily, HTTPResponse, template, StaticFileHandler, Concurrent, init_log
2 | import time
3 |
4 | def main():
5 | import logging
6 | init_log(level=logging.DEBUG)
7 |
8 | class Temp(HTTPResponse):
9 | def get(self):
10 | return template("graily_test.html",
11 | {'parameters': self.parameters, 'name': 'Graily'})
12 |
13 | class Test(HTTPResponse):
14 | def get(self, name):
15 | return "hello, {}".format(name)
16 |
17 | def post(self, name):
18 | return template("graily_test.html",
19 | parameters=self.parameters, name=name)
20 |
21 | class ConTest(HTTPResponse):
22 | @Concurrent.register
23 | def get(self):
24 | time.sleep(5) # won't block the main Thread
25 | return "hello from Graily concurrent"
26 |
27 | app = Graily([
28 | (r'^/temp/$', Temp),
29 | (r'^/static/(.*)$', StaticFileHandler.set_path("static")),
30 | (r'^/con/$', ConTest),
31 | (r'^/(.*)/$', Test),
32 | ])
33 | app.server_bind(("", 8888))
34 | app.serve_forever()
35 |
36 | if __name__ == '__main__':
37 | main()
38 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Graily Web Server
2 | Graily is a no-blocking web server and micro web-framework written by Python. It's only support for Python3 and Linux platform.
3 |
4 | * **Lightweight And Fast :** It's has no dependencies other than the standard library. And it's fast.
5 | * **Long-Polling :** Graily can handle tens of thousands of connections for long polling.
6 | * **WSGI Server Support :** Graily has support for WSGI, so it can be used as a HTTP server for any other frameworks.
7 | * **Concurrent :** Graily can chose to use Concurrent class to handle those request which may blocking the main thread.
8 |
9 | #####Here is a sample "Hello, world" example:
10 | ```python
11 | from graily import Graily, HTTPResponse
12 |
13 | class MainHandler(HTTPResponse):
14 | def get(self):
15 | return "Hello, world"
16 |
17 | server = Graily([
18 | (r'^/$', MainHandler),
19 | ])
20 | server.server_bind(("", 8888))
21 | server.serve_forever()
22 | ```
23 | #####Concurrent example:
24 | ```python
25 | from graily import Graily, HTTPResponse, Concurrent
26 | import time
27 |
28 | class MainHandler(HTTPResponse):
29 | @Concurrent.register
30 | def get(self):
31 | time.sleep(5) # won't blocking the main Thread
32 | return "Hello, world"
33 |
34 | server = Graily([
35 | (r'^/$', MainHandler),
36 | ])
37 | server.server_bind(("", 8888))
38 | server.serve_forever()
39 | ```
40 | #####WSGI server with Bottle Application example:
41 | ```python
42 | from bottle import route, default_app
43 | from graily import make_server
44 |
45 | @route('/')
46 | def main():
47 | return 'hello from bottle.'
48 |
49 | application = default_app()
50 | server = make_server(('', 8888), application)
51 | server.serve_forever()
52 | ```
53 | You can find more examples (concurrent, template) in [examples](https://github.com/lazywen/graily/tree/master/examples).
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
203 |
--------------------------------------------------------------------------------
/graily.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python3
2 | # -*- coding: utf-8 -*-
3 |
4 | __all__ = [
5 | # tcp server
6 | 'TCPServer', 'ThreadPollTCPServer', 'StreamRequestHandler',
7 | # http server
8 | 'HTTPServer', 'ThreadPollHTTPServer', 'BaseHTTPRequestHandler',
9 | 'HTTPResponse', 'StaticFileHandler'
10 | # wsgi server
11 | 'WSGIServer', 'WSGIRequestHandler', 'make_server',
12 | # templates
13 | 'BaseTemplate', 'templates', 'MakoTemplate',
14 | # main application and others
15 | 'Graily', 'Concurrent', 'init_log'
16 | ]
17 |
18 | import os, sys, re, errno, time, socket, select, logging, random, types, io
19 | import mimetypes, functools, heapq
20 | import traceback
21 | assert sys.version_info>=(3,0,0), "Only support for Python3"
22 |
23 | import queue
24 | import _thread, threading
25 |
26 | from select import epoll
27 | from urllib.parse import urlparse, parse_qs
28 |
29 | class GrailyPoll:
30 | '''main Poll loop'''
31 |
32 | READ = select.EPOLLIN
33 | WRITE = select.EPOLLOUT
34 | ERROR = select.EPOLLERR | select.EPOLLHUP
35 | MAX_KEEPALIVE_TIME = 300
36 |
37 | server_name = "Graily"
38 |
39 | def __init__(self, server_address, RequestHandler, allow_reuse_address=True,
40 | max_input_size=2097152):
41 | self.server_address = server_address
42 | self.RequestHandler = RequestHandler
43 | self.allow_reuse_address = allow_reuse_address
44 | self.max_input_size = max_input_size
45 | self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
46 | self._sockets = {self.socket.fileno(): self.socket}
47 | self._shutdown_request = True
48 | self._handlers = {}
49 | self._keepalive = {}
50 |
51 | self.server_bind()
52 |
53 | def init_socket(self):
54 | logging.info('Starting server at {}'.format(self.server_address))
55 | if self.allow_reuse_address:
56 | self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
57 | self.socket.bind(self.server_address)
58 | self.server_address = self.socket.getsockname()
59 | self.socket.setblocking(False)
60 | self.socket.listen(100)
61 | self.init_epoll()
62 |
63 | def init_epoll(self):
64 | self.epoll = epoll()
65 | self.epoll.register(self.socket, self.READ | self.ERROR)
66 |
67 | def server_bind(self):
68 | self.init_socket()
69 | self.init_epoll()
70 |
71 | def server_close(self):
72 | self.socket.close()
73 |
74 | def shutdown_request(self, request, flag=socket.SHUT_WR):
75 | try: request.shutdown(flag)
76 | except OSError: pass
77 |
78 | def close_request(self, request):
79 | try: request.close()
80 | except OSError: pass
81 |
82 | def _run(self, timeout=1):
83 | # TODO timeout
84 |
85 | try:
86 | event_pairs = self.epoll.poll(timeout)
87 | # logging.debug("event: {}".format(event_pairs))
88 | # logging.debug("sockets: {}".format([i for i in self._sockets]))
89 | # logging.debug("handlers: {}".format([(i.fileno(), self._handlers[i]) for i in self._handlers ]))
90 | except Exception as e:
91 | if errno_from_exception(e) == errno.EINTR:
92 | return
93 | else: raise
94 |
95 | for fd, event in event_pairs:
96 | sock = self._sockets[fd]
97 | if event & self.ERROR:
98 | self.epoll.unregister(fd)
99 | del self._sockets[fd]
100 | self._handlers.pop(sock, None)
101 | self._keepalive.pop(sock, None)
102 | self.close_request(sock)
103 | else:
104 | try:
105 | if sock is self.socket:
106 | if not self._shutdown_request:
107 | request, client_address = sock.accept()
108 | request.setblocking(False)
109 | _fd = request.fileno()
110 | self._sockets[_fd] = request
111 | self.epoll.register(request, self.READ | self.ERROR)
112 | self._keepalive[request] = time.time()
113 | # TODO wait threads exit
114 | else: time.sleep(timeout)
115 |
116 | elif event & self.READ:
117 | self._keepalive[sock] = time.time()
118 | self.handle_request(sock)
119 | elif event & self.WRITE:
120 | self.handle_response(sock)
121 |
122 | except (OSError, IOError) as e:
123 | if errno_from_exception(e) == errno.EPIPE:
124 | # Happens when the client closes the connection
125 | pass
126 | except Exception as e:
127 | self.handle_exception(e, sock)
128 |
129 | # do something when no events
130 | if len(event_pairs) == 0:
131 | for _fd, _sock in self._sockets.items():
132 | if _sock not in self._keepalive:
133 | self._keepalive[_sock] = time.time()
134 | for _sock, _time in self._keepalive.items():
135 | if time.time()-_time >= self.MAX_KEEPALIVE_TIME:
136 | self.shutdown_request(_sock)
137 |
138 | def handle_exception(self, err, sock):
139 | logging.exception('')
140 | if sock is self.socket: time.sleep(1)
141 | else: self.shutdown_request(sock)
142 |
143 | def handle_request(self, sock):
144 | if sock not in self._handlers:
145 | self._handlers[sock] = self.RequestHandler(sock, self)
146 | self._handlers[sock]._run()
147 |
148 | def handle_response(self, sock):
149 | handler = self._handlers[sock]
150 | sent = sock.send(handler.iostream._write_buffer)
151 | # logging.debug('epoll sent: %s' % sent)
152 | if sent >= len(handler.iostream._write_buffer):
153 | self.update_handler(sock, self.READ)
154 | handler.iostream._write_buffer = handler.iostream._write_buffer[sent:]
155 |
156 | def update_handler(self, sock, event):
157 | self.epoll.modify(sock, event | self.ERROR)
158 |
159 |
160 | class TCPServer(GrailyPoll):
161 | '''tcp server'''
162 | def __init__(self, server_address, RequestHandler, **kwargs):
163 | super(TCPServer, self).__init__(server_address, RequestHandler,
164 | **kwargs)
165 |
166 | def serve_forever(self):
167 | self._shutdown_request = False
168 | while not self._shutdown_request or len(self.sockets)>1:
169 | self._run()
170 |
171 | class ThreadPollTCPServer(TCPServer):
172 | '''use threading poll for handler'''
173 | def __init__(self, server_address, RequestHandler,
174 | poll_size=100, max_tasks=1000, **kwargs):
175 | super(ThreadPollTCPServer, self).__init__(server_address, RequestHandler, **kwargs)
176 | if Concurrent._concurrency:
177 | self.concurrent = Concurrent(poll_size, max_tasks, self)
178 |
179 | def put_task(self, task):
180 | self.concurrent.tasks.put(task)
181 |
182 | def serve_forever(self):
183 | if Concurrent._concurrency: self.concurrent.start()
184 | self._shutdown_request = False
185 | while not self._shutdown_request or len(self.sockets)>1:
186 | self._run()
187 |
188 | class HTTPServer(TCPServer):
189 | '''HTTP server'''
190 |
191 | class ThreadPollHTTPServer(ThreadPollTCPServer):
192 | '''HTTPServer use threading poll for handler'''
193 |
194 | class WSGIServer(HTTPServer):
195 | application = None
196 | def server_bind(self):
197 | """Override server_bind to store the server name."""
198 | HTTPServer.server_bind(self)
199 | self.setup_environ()
200 |
201 | def setup_environ(self):
202 | # Set up base environment
203 | env = self.base_environ = {}
204 | env['SERVER_NAME'] = self.server_name
205 | env['GATEWAY_INTERFACE'] = 'CGI/1.1'
206 | env['SERVER_PORT'] = str(self.server_address[1])
207 | env['REMOTE_HOST']=''
208 | env['CONTENT_LENGTH']=''
209 | env['SCRIPT_NAME'] = ''
210 |
211 | def get_app(self):
212 | return self.application
213 |
214 | def set_app(self,application):
215 | self.application = application
216 |
217 | class Concurrent:
218 | # when _concurrency is True, the server will start thread poll
219 | _concurrency = False
220 |
221 | def __init__(self, poll_size, max_tasks, server):
222 | self.poll_size = poll_size
223 | self.tasks = queue.Queue(max_tasks)
224 | self.server = server
225 | self._thread_poll = []
226 | self._running = False
227 |
228 | def start(self):
229 | logging.info('starting thread poll ...')
230 | if self._running:
231 | logging.warning('thread poll already started!')
232 | return
233 |
234 | def worker():
235 | while True:
236 | task = self.tasks.get()
237 | # logging.debug(threading.current_thread().getName()+' got a task: '+str(task))
238 | # time.sleep(random.randint(1,5)/10)
239 |
240 | next(task['run'])
241 | try: res = task['run'].send(task['args'])
242 | except StopIteration: pass
243 | except Exception as e:
244 | self.server.handle_exception(e, task['socket'])
245 | else:
246 | if res and 'result_handler' in task:
247 | task['result_handler'](res)
248 | if 'callback' in task and len(task['callback'])>0:
249 | for callback, args in task['callback']:
250 | callback(*args)
251 |
252 | for i in range(self.poll_size):
253 | td = threading.Thread(target=worker, args=())
254 | td.setDaemon(True)
255 | td.start()
256 | self._thread_poll.append(td)
257 | self._running = True
258 |
259 | def allocate(self):
260 | # TODO
261 | pass
262 |
263 | class register:
264 | '''
265 | the register decorator can make the method concurrent:
266 |
267 | @Concurrent.register
268 | def get(self):
269 | self.write("hello, world")
270 |
271 | '''
272 |
273 | def __init__(self, func):
274 | self.func = func
275 | self.__dict__['_graily_concurrency'] = True
276 | if not Concurrent._concurrency:
277 | Concurrent._concurrency = True
278 | def __call__(self, *args, **kwargs):
279 | ora_args = yield
280 | new_args = []
281 | if ora_args: new_args.extend(ora_args)
282 | new_args.extend(args)
283 | yield self.func(*new_args, **kwargs)
284 |
285 | class BaseRequestHandler:
286 | '''
287 | The base request handler class, you must implemente those method:
288 |
289 | parse_request
290 | verify_request
291 | dataReceived
292 | '''
293 |
294 | def __init__(self, request, server):
295 | self.request = request
296 | self.server = server
297 | self.iostream = BaseIOStream(request, server)
298 | self.response_handler = self.dataReceived
299 | self._initialize()
300 |
301 | def _initialize(self):
302 | pass
303 |
304 | def write(self, res):
305 | if not self.iostream.write(res):
306 | self.server.update_handler(self.request, self.server.WRITE)
307 |
308 | def close(self):
309 | self.server.shutdown_request(self.request)
310 |
311 | def _run(self):
312 | data_ready = self.iostream.read()
313 | if data_ready and self.verify_request():
314 | if self.parse_request():
315 | prg = self.dataReceived()
316 | if isinstance(prg, types.GeneratorType) and '_graily_concurrency' \
317 | in self.dataReceived.__dict__:
318 | task = {'socket': self.request, 'run': prg, 'args':(self,),
319 | 'callback': [(self._initialize, ())]}
320 | self.server.put_task(task)
321 | else: self._initialize()
322 |
323 |
324 | def parse_request(self):
325 | '''pop data that you need from iostream._read_buffer and keep the
326 | others.
327 | '''
328 | raise NotImplementedError
329 |
330 | def verify_request(self):
331 | ''' verify the received data with your own protocol, return True
332 | if the data is correct and a False value if not correct.
333 | '''
334 | raise NotImplementedError
335 |
336 | def dataReceived(self):
337 | raise NotImplementedError
338 |
339 | class StreamRequestHandler(BaseRequestHandler):
340 | ''' The request handler for TCPServer '''
341 | def _initialize(self):
342 | pass
343 |
344 | def parse_request(self):
345 | self.data = self.iostream._read_buffer.decode()
346 | self.iostream._read_buffer = b""
347 | return True
348 |
349 | def verify_request(self):
350 | return bool(self.iostream._read_buffer)
351 |
352 | class BaseHTTPRequestHandler(BaseRequestHandler):
353 | ''' The request hansler for HTTPServer '''
354 |
355 | SUPPORT_HTTP_VERSION = ('HTTP/1.0', 'HTTP/1.1')
356 | HTTP_METHOD = ('HEAD', 'GET', 'POST', 'OPTIONS', 'PUT', 'DELETE', 'TRACE', 'CONNECT')
357 | ERROR_MESSAGE_TEMPLATE = (''
358 | '%(code)d %(msg)s'
359 | '