File tree Expand file tree Collapse file tree 5 files changed +51
-12
lines changed
Expand file tree Collapse file tree 5 files changed +51
-12
lines changed Original file line number Diff line number Diff line change 1+ # Mojifinder: Unicode character search examples
2+
3+ Examples from _ Fluent Python, Second Edition_ —Chapter 22, _ Asynchronous Programming_ .
4+
5+ ## How to run ` web_mojifinder.py `
6+
7+ ` web_mojifinder.py ` is a Web application built with _ [ FastAPI] ( https://fastapi.tiangolo.com/ ) _ .
8+ To run it, first install _ FastAPI_ and an ASGI server.
9+ The application was tested with _ [ Uvicorn] ( https://www.uvicorn.org/ ) _ .
10+
11+ ```
12+ $ pip install fastapi uvicorn
13+ ```
14+
15+ Now you can use ` uvicorn ` to run the app.
16+
17+ ```
18+ $ uvicorn web_mojifinder:app
19+ ```
20+
21+ Finally, visit http://127.0.0.1:8000/ with your browser to see the search form.
22+
23+
24+ ## Directory contents
25+
26+ These files can be run as scripts directly from the command line:
27+
28+ - ` charindex.py ` : libray used by the Mojifinder examples. Also works as CLI search script.
29+ - ` tcp_mojifinder.py ` : TCP/IP Unicode search server. Depends only on the Python 3.9 standard library. Use a telnet application as client.
30+ - ` web_mojifinder_bottle.py ` : Unicode Web service. Depends on ` bottle.py ` and ` static/form.html ` . Use an HTTP browser as client.
31+
32+ This program requires an ASGI server to run it:
33+
34+ - ` web_mojifinder.py ` : Unicode Web service. Depends on _ [ FastAPI] ( https://fastapi.tiangolo.com/ ) _ and ` static/form.html ` .
35+
36+ Support files:
37+
38+ - ` bottle.py ` : local copy of the single-file _ [ Bottle] ( https://bottlepy.org/ ) _ Web framework.
39+ - ` requirements.txt ` : list of dependencies for ` web_mojifinder.py ` .
40+ - ` static/form.html ` : HTML form used by the ` web_* ` examples.
41+ - ` README.md ` : this file!
Original file line number Diff line number Diff line change 1616from __future__ import with_statement
1717
1818__author__ = 'Marcel Hellkamp'
19- __version__ = '0.12.18 '
19+ __version__ = '0.12.19 '
2020__license__ = 'MIT'
2121
2222# The gevent server adapter needs to patch some modules before they are imported
@@ -440,7 +440,7 @@ def match(self, environ):
440440 nocheck = set (methods )
441441 for method in set (self .static ) - nocheck :
442442 if path in self .static [method ]:
443- allowed .add (verb )
443+ allowed .add (method )
444444 for method in set (self .dyna_regexes ) - allowed - nocheck :
445445 for combined , rules in self .dyna_regexes [method ]:
446446 match = combined (path )
@@ -2585,7 +2585,7 @@ def parse_range_header(header, maxlen=0):
25852585
25862586def _parse_qsl (qs ):
25872587 r = []
2588- for pair in qs .replace ( ';' , '&' ). split ('&' ):
2588+ for pair in qs .split ('&' ):
25892589 if not pair : continue
25902590 nv = pair .split ('=' , 1 )
25912591 if len (nv ) != 2 : nv .append ('' )
Original file line number Diff line number Diff line change 11click == 7.1.2
22fastapi == 0.63.0
33h11 == 0.12.0
4- pydantic == 1.7.3
4+ pydantic == 1.8.1
55starlette == 0.13.6
6+ typing-extensions == 3.7.4.3
67uvicorn == 0.13.4
Original file line number Diff line number Diff line change 1- import pathlib
1+ from pathlib import Path
22from unicodedata import name
33
44from fastapi import FastAPI
@@ -18,9 +18,8 @@ class CharName(BaseModel): # <2>
1818
1919def init (app ): # <3>
2020 app .state .index = InvertedIndex ()
21- static = pathlib .Path (__file__ ).parent .absolute () / 'static' # <4>
22- with open (static / 'form.html' ) as fp :
23- app .state .form = fp .read ()
21+ static = Path (__file__ ).parent .absolute () / 'static' # <4>
22+ app .state .form = (static / 'form.html' ).read_text ()
2423
2524init (app ) # <5>
2625
@@ -33,4 +32,4 @@ async def search(q: str): # <7>
3332def form (): # <9>
3433 return app .state .form
3534
36- # no main funcion # <10>
35+ # no main funcion # <10>
Original file line number Diff line number Diff line change 1111
1212@route ('/' )
1313def form ():
14- return static_file ('form.html' , root = 'static/' )
14+ return static_file ('form.html' , root = 'static/' )
1515
1616
1717@route ('/search' )
@@ -28,10 +28,8 @@ def search():
2828def main (port ):
2929 global index
3030 index = InvertedIndex ()
31- host = 'localhost'
3231 run (host = 'localhost' , port = port , debug = True )
3332
3433
3534if __name__ == '__main__' :
3635 main (8000 )
37-
You can’t perform that action at this time.
0 commit comments