|
1 | 1 | = Experimenting with the `flags2*` examples
|
2 | 2 |
|
| 3 | +== Install SSL Certificates (for MacOS) |
| 4 | + |
| 5 | +On Macos, depending on how in installed Python you may need to manually run a command |
| 6 | +after Python's installer finishes, to install SSL certificates for HTTPS connections. |
| 7 | + |
| 8 | +Using the Finder, open the `Python 3.X` folder inside `/Applications` folder |
| 9 | +and double-click "Install Certificates" or "Install Certificates.command". |
| 10 | + |
| 11 | +Using the terminal, you can type for example: |
| 12 | + |
| 13 | +[source, text] |
| 14 | +---- |
| 15 | +$ open /Applications/Python 3.10/"Install Certificates.command" |
| 16 | +---- |
| 17 | + |
| 18 | + |
3 | 19 | == Setting up a test server
|
4 | 20 |
|
5 | 21 | If you don't already have a local HTTP server for testing,
|
6 |
| -here are the steps to experiment using only Python ≥ 3.9—no external libraries: |
| 22 | +here are the steps to experiment using only Python ≥ 3.9: |
7 | 23 |
|
8 |
| -. Clone or download the https://github.com/fluentpython/example-code-2e[_Fluent Python 2e_ code repository]. |
9 |
| -. Open your shell and go to the _20-futures/getflags/_ directory of your local copy of the repository. |
| 24 | +. Clone or download the https://github.com/fluentpython/example-code-2e[_Fluent Python 2e_ code repository] (this repo!). |
| 25 | +. Open your shell and go to the _20-futures/getflags/_ directory of your local copy of the repository (this directory!) |
10 | 26 | . Unzip the _flags.zip_ file, creating a _flags_ directory at _20-futures/getflags/flags/_.
|
11 | 27 | . Open a second shell, go to the _20-futures/getflags/_ directory and run `python3 -m http.server`. This will start a `ThreadingHTTPServer` listening to port 8000, serving the local files. If you open the URL http://localhost:8000/flags/[http://localhost:8000/flags/] with your browser, you'll see a long list of directories named with two-letter country codes from `ad/` to `zw/`.
|
12 | 28 | . Now you can go back to the first shell and run the _flags2*.py_ examples with the default `--server LOCAL` option.
|
13 |
| -. To test with the `--server DELAY` option, go to _20-futures/getflags/_ and run `python3 slow_server.py 8001`. This will add a .5s delay before each response. |
14 |
| -. To test with the `--server ERROR` option, go to _20-futures/getflags/_ and run `python3 slow_server.py 8002 --error-rate .25`. Each request will have a 25% probability of getting a https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/418[418 I'm a teapot] response, and all responses will be delayed .5s. |
| 29 | +. To test with the `--server DELAY` option, go to _20-futures/getflags/_ and run `python3 slow_server.py`. This bind to port 8001 by default. It will add a .5s delay before each response. |
| 30 | +. To test with the `--server ERROR` option, go to _20-futures/getflags/_ and run `python3 slow_server.py 8002 --error-rate .25`. |
| 31 | +Each request will have a 25% probability of getting a |
| 32 | +https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/418[418 I'm a teapot] response, |
| 33 | +and all responses will be delayed .5s. |
15 | 34 |
|
16 | 35 | I wrote _slow_server.py_ reusing code from Python's
|
17 | 36 | https://github.com/python/cpython/blob/917eca700aa341f8544ace43b75d41b477e98b72/Lib/http/server.py[`http.server`] standard library module,
|
18 | 37 | which "is not recommended for production"—according to the
|
19 | 38 | https://docs.python.org/3/library/http.server.html[documentation].
|
20 |
| -To set up a more reliable testing environment, I recommend configuring |
| 39 | + |
| 40 | +[NOTE] |
| 41 | +==== |
| 42 | +This is a simple testing environment that does nor require any external libraries or |
| 43 | +tools—apart from the libraries used in the `flags2*` scripts themselves, as discussed in the book. |
| 44 | +
|
| 45 | +For a more robust testing environment, I recommend configuring |
21 | 46 | https://www.nginx.com/[NGINX] and
|
22 | 47 | https://github.com/shopify/toxiproxy[Toxiproxy] with equivalent parameters.
|
| 48 | +==== |
23 | 49 |
|
24 | 50 | == Running a `flags2*` script
|
25 | 51 |
|
|
0 commit comments