Debian Codegraph @

README.md

raw symbols SCIP
1 ## AWS CRT Python
2
3 [![Version](https://img.shields.io/pypi/v/awscrt.svg?style=flat)](https://pypi.org/project/awscrt/)
4
5 Python 3 bindings for the AWS Common Runtime.
6
7 * [API documentation](https://awslabs.github.io/aws-crt-python)
8 * [Development guide](guides/dev/README.md) for contributors to aws-crt-python's source code.
9
10 ## License
11
12 This library is licensed under the Apache 2.0 License.
13
14 ## Minimum Requirements:
15
16 * Python 3.8+
17
18 ## Installation
19
20 To install from pip:
21
22 ```bash
23 python3 -m pip install awscrt
24 ```
25
26 To install from Github:
27
28 ```bash
29 git clone https://github.com/awslabs/aws-crt-python.git
30 cd aws-crt-python
31 git submodule update --init
32 python3 -m pip install .
33 ```
34
35 See [Advanced Build Options](#advanced-build-options) for more info about building from source.
36
37 ## Fork and Multiprocessing
38
39 aws-crt-python uses background threads. This makes [os.fork()](https://docs.python.org/3/library/os.html#os.fork) unsafe. In a forked child process, all background threads vanish. The child will hang or crash when it tries to communicate with any of these (vanished) threads.
40
41 Unfortunately, Python's [multiprocessing](https://docs.python.org/3/library/multiprocessing.html) module defaults to using fork when it creates child processes (on POSIX systems except macOS, in Python versions 3.13 and earlier). `multiprocessing` is used under the hood by many tools that do work in parallel, including [concurrent.futures.ProcessPoolExecutor](https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.ProcessPoolExecutor), and [pytorch.multiprocessing](https://pytorch.org/docs/stable/multiprocessing.html).
42
43 If you need to use `multiprocessing` with aws-crt-python, set it to use "spawn" or "forkserver" instead of "fork" ([see docs](https://docs.python.org/3/library/multiprocessing.html#contexts-and-start-methods)). The Python community agrees, and `multiprocessing` will changes its default from "fork" to "spawn" in 3.14. It already uses "spawn" by default on macOS (because system libraries may start threads) and on Windows (because fork does not exist).
44
45 If you **must** use fork with aws-crt-python, you may be able to avoid hangs and crashes if you manage your threads very carefully:
46
47 1. Release all CRT resources with background threads (e.g. clean up any `io.EventLoopGroup` instances).
48 2. Join all CRT threads before forking (use `common.join_all_native_threads()` ).
49
50 For an example, see `test.test_s3.py.S3RequestTest.test_fork_workaround` .
51
52 ## macOS TLS Configuration
53
54 By default on macOS, aws-crt-python uses Apple Secure Transport for TLS. This provides FIPS-compliant cryptography
55 and integration with the macOS Keychain (e.g. PKCS#12 credentials), but is limited to TLS 1.2.
56
57 To enable TLS 1.3 on macOS, set the environment variable:
58
59 ```
60 export AWS_CRT_USE_NON_FIPS_TLS_13=1
61 ```
62
63 This switches the TLS backend from Apple Secure Transport to [s2n-tls](https://github.com/aws/s2n-tls) with
64 [aws-lc](https://github.com/aws/aws-lc) as the underlying libcrypto. The tradeoffs are:
65
66 | | Secure Transport (default) | s2n-tls (`AWS_CRT_USE_NON_FIPS_TLS_13=1`) |
67 |---|---|---|
68 | TLS versions | Up to TLS 1.2 | Up to TLS 1.3 |
69 | FIPS compliance | Yes | No |
70 | macOS Keychain integration | Yes (PKCS#12, system certs) | No |
71
72 This variable is checked at runtime and only affects macOS. It has no effect on Linux (which always uses s2n-tls)
73 or Windows (which always uses Schannel). Both TLS backends are compiled into the binary when building on macOS;
74 the environment variable selects which one is used.
75
76 ### Keychain Behavior
77
78 Please note that on Mac, once a private key is used with a certificate, that certificate-key pair is imported into the Mac Keychain. All subsequent uses of that certificate will use the stored private key and ignore anything passed in programmatically. Beginning in v0.6.2, when a stored private key from the Keychain is used, the following will be logged at the "info" log level:
79
80 ```
81 static: certificate has an existing certificate-key pair that was previously imported into the Keychain. Using key from Keychain instead of the one provided.
82 ```
83
84 ## Logging
85
86 aws-crt-python provides two ways to configure logging from the CRT's native C libraries.
87
88 ### Option 1: Python logging integration (recommended)
89
90 Route CRT log messages through Python's standard `logging` module. Each CRT subsystem gets a hierarchical logger under `awscrt` (e.g. `awscrt.io.event-loop`, `awscrt.http.http-connection`, `awscrt.s3.S3Client`), so you can filter by package or subsystem.
91
92 ```python
93 import logging
94 from awscrt.logging import init_logging, CRT_LOG_FORMAT
95
96 handler = logging.StreamHandler()
97 handler.setFormatter(logging.Formatter(CRT_LOG_FORMAT))
98 logging.getLogger('awscrt').addHandler(handler)
99 init_logging(logging.DEBUG)
100 ```
101
102 To filter to a specific CRT package:
103
104 ```python
105 # Only show IO logs at DEBUG, silence everything else
106 logging.getLogger('awscrt').setLevel(logging.WARNING)
107 logging.getLogger('awscrt.io').setLevel(logging.DEBUG)
108 ```
109
110 ### Option 2: Direct file/stdout logging
111
112 Write CRT log output directly to a file, stdout, or stderr. This bypasses Python's logging module and is useful for quick debugging.
113
114 ```python
115 from awscrt.io import LogLevel, init_logging
116
117 init_logging(LogLevel.Debug, 'stdout') # or 'stderr', or a file path
118 ```
119
120 ### Which should I use?
121
122 Use `awscrt.logging.init_logging` (Option 1) if you want CRT logs integrated with your application's existing Python logging setup, with per-subsystem filtering.
123
124 Use `awscrt.io.init_logging` (Option 2) if you just want to dump all CRT logs to a file or console quickly.
125
126 These are mutually exclusive — use one or the other, not both.
127
128 ## Crash Handler
129
130 You can enable the crash handler by setting the environment variable `AWS_CRT_CRASH_HANDLER=1` . This will print the callstack to `stderr` in the event of a fatal error.
131
132 ## Advanced Build Options
133
134 ### OpenSSL and LibCrypto
135
136 aws-crt-python does not use OpenSSL for TLS.
137 On Windows, the OS's default TLS library (Schannel) is used.
138 On Apple (macOS), both Secure Transport and s2n-tls are compiled in; the backend is selected at runtime (see [macOS TLS Configuration](#macos-tls-configuration) below).
139 On other Unix devices, [s2n-tls](https://github.com/aws/s2n-tls) is used.
140 But s2n-tls uses libcrypto, the cryptography math library bundled with OpenSSL.
141
142 To simplify installation, aws-crt-python has its own copy of libcrypto.
143 This lets you install a wheel from PyPI without having OpenSSL installed.
144 Unix wheels on PyPI come with libcrypto statically compiled in.
145 Code to build libcrypto comes from [AWS-LC](https://github.com/aws/aws-lc).
146 AWS-LC's code is included in the PyPI source package,
147 and the git repository includes it as a submodule.
148
149 If you need aws-crt-python to use the libcrypto included on your system,
150 set environment variable `AWS_CRT_BUILD_USE_SYSTEM_LIBCRYPTO=1` while building from source:
151
152 ```sh
153 AWS_CRT_BUILD_USE_SYSTEM_LIBCRYPTO=1 python3 -m pip install --no-binary :all: --verbose awscrt
154 ```
155
156 ( `--no-binary :all:` ensures you do not use the precompiled wheel from PyPI)
157
158 aws-crt-python also exposes a number of cryptographic primitives.
159 On Unix, those depend on libcrypto as described above.
160 On Apple and Windows OS level crypto libraries are used whenever possible.
161 One exception to above statement is that for ED25519 keygen on Windows and Apple,
162 libcrypto is used as no viable OS level alternative exists. In that case Unix level notes
163 about libcrypto apply to Apple and Windows as well. Libcrypto usage for ED25519 support is
164 enabled on Windows and Apple by default and can be disabled by setting environment variable
165 `AWS_CRT_BUILD_DISABLE_LIBCRYPTO_USE_FOR_ED25519_EVERYWHERE` as follows:
166 (Note: ED25519 keygen functions will start returning not supported error in this case)
167
168 ```sh
169 AWS_CRT_BUILD_DISABLE_LIBCRYPTO_USE_FOR_ED25519_EVERYWHERE=1 python3 -m pip install --no-binary :all: --verbose awscrt
170 ```
171
172 ( `--no-binary :all:` ensures you do not use the precompiled wheel from PyPI)
173
174 ### AWS_CRT_BUILD_USE_SYSTEM_LIBS ###
175
176 aws-crt-python depends on several C libraries that make up the AWS Common Runtime (libaws-c-common, libaws-c-s3, etc).
177 By default, these libraries are built along with aws-crt-python and statically compiled in
178 (their source code is under [crt/](crt/)).
179
180 To skip building these dependencies, because they're already available on your system,
181 set environment variable `AWS_CRT_BUILD_USE_SYSTEM_LIBS=1` while building from source:
182
183 ```sh
184 AWS_CRT_BUILD_USE_SYSTEM_LIBS=1 python3 -m pip install .
185 ```
186
187 If these dependencies are available as both static and shared libs, you can force the static ones to be used by setting: `AWS_CRT_BUILD_FORCE_STATIC_LIBS=1`
188
189 ### AWS_EXTRA_LIB_DIR ###
190
191 If you need to add additional library directories for the linker to search,
192 set environment variable `AWS_EXTRA_LIB_DIR` while building from source.
193 This sets the `library_dirs` parameter for the setuptools Extension, which tells the linker
194 where to find libraries during the build process.
195 These directories supplement (not replace) the linker's default search paths.
196 This is useful when you have custom library installations in non-standard locations.
197
198 For more details about `library_dirs` , see the [setuptools Extension documentation](https://setuptools.pypa.io/en/latest/userguide/ext_modules.html).
199
200 For a single directory:
201
202 ```sh
203 AWS_EXTRA_LIB_DIR=/path/to/libs python3 -m pip install .
204 ```
205
206 For multiple directories, separate them with the OS path separator ( `:` on Unix/macOS, `;` on Windows):
207
208 ```sh
209 # Unix/macOS
210 AWS_EXTRA_LIB_DIR=/path/to/libs:/another/path python3 -m pip install .
211
212 # Windows
213 AWS_EXTRA_LIB_DIR=C:\path\to\libs;D:\another\path python3 -m pip install .
214 ```
215
216 ### Windows SDK Version
217
218 aws-crt-python builds against windows sdk version `10.0.17763.0` . This is the minimal version required for TLS 1.3 support on Windows. If you need a different Windows SDK version, you can set environment variable `AWS_CRT_WINDOWS_SDK_VERSION=<version>` while building from source:
219
220 ### Attribution
221 This library exposes native XXHash implementation (https://github.com/Cyan4973/xxHash).