gemini-tender 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- gemini_tender-0.1.0/PKG-INFO +177 -0
- gemini_tender-0.1.0/README.md +156 -0
- gemini_tender-0.1.0/pyproject.toml +39 -0
- gemini_tender-0.1.0/setup.cfg +4 -0
- gemini_tender-0.1.0/src/gemini_tender.egg-info/PKG-INFO +177 -0
- gemini_tender-0.1.0/src/gemini_tender.egg-info/SOURCES.txt +11 -0
- gemini_tender-0.1.0/src/gemini_tender.egg-info/dependency_links.txt +1 -0
- gemini_tender-0.1.0/src/gemini_tender.egg-info/top_level.txt +1 -0
- gemini_tender-0.1.0/src/tender/__init__.py +6 -0
- gemini_tender-0.1.0/src/tender/gemtext.py +142 -0
- gemini_tender-0.1.0/src/tender/response.py +138 -0
- gemini_tender-0.1.0/src/tender/support.py +21 -0
- gemini_tender-0.1.0/src/tender/tender.py +52 -0
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: gemini-tender
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A Gemini (the protocol) low-level client, implemented in Python
|
|
5
|
+
Author-email: Maxime 'Epoc' Gross <contact.nospam@epoc.nospam.fr>
|
|
6
|
+
Project-URL: Documentation, https://github.com/EpocDotFr/tender?tab=readme-ov-file#usage
|
|
7
|
+
Project-URL: Repository, https://github.com/EpocDotFr/tender
|
|
8
|
+
Project-URL: Issues, https://github.com/EpocDotFr/tender/issues
|
|
9
|
+
Project-URL: Changelog, https://github.com/EpocDotFr/tender/releases
|
|
10
|
+
Keywords: asyncio,gemini-protocol,client,dependency-free
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
14
|
+
Classifier: Topic :: Internet
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
19
|
+
Requires-Python: >=3.14
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
|
|
22
|
+
# Tender
|
|
23
|
+
|
|
24
|
+
A [Gemini](https://geminiprotocol.net/) (the protocol) low-level client, implemented in Python.
|
|
25
|
+
|
|
26
|
+
  
|
|
27
|
+
|
|
28
|
+
[PyPI](https://pypi.org/project/gemini-tender/) - [Documentation](https://github.com/EpocDotFr/tender?tab=readme-ov-file#usage) - [Source code](https://github.com/EpocDotFr/tender) - [Issue tracker](https://github.com/EpocDotFr/tender/issues) - [Changelog](https://github.com/EpocDotFr/tender/releases)
|
|
29
|
+
|
|
30
|
+
> [!TIP]
|
|
31
|
+
> See also: [Harbor](https://github.com/EpocDotFr/harbor), the accompanying server; and [Wayfarer](https://github.com/EpocDotFr/wayfarer), the accompanying [TUI](https://en.wikipedia.org/wiki/Text-based_user_interface) client.
|
|
32
|
+
|
|
33
|
+
## Prerequisites
|
|
34
|
+
|
|
35
|
+
- Python >= 3.14
|
|
36
|
+
|
|
37
|
+
## Installation
|
|
38
|
+
|
|
39
|
+
Use your favorite Python package manager to install `gemini-tender`. Example using [Pipenv](https://pipenv.pypa.io/en/latest/):
|
|
40
|
+
|
|
41
|
+
```shell
|
|
42
|
+
pipenv install gemini-tender
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Usage
|
|
46
|
+
|
|
47
|
+
### Setting up the SSL context
|
|
48
|
+
|
|
49
|
+
The Gemini protocol *requires* usage of TLS >= 1.2. The first thing you thus have to do is to set up an SSL context.
|
|
50
|
+
|
|
51
|
+
A bare-minimum, Gemini-compliant context can be created and used right away:
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
import tender
|
|
55
|
+
|
|
56
|
+
context = tender.create_default_ssl_context()
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
This method creates a preconfigured [ssl.SSLContext](https://docs.python.org/3.14/library/ssl.html#ssl.SSLContext) instance. However,
|
|
60
|
+
this context will *not* - by default - validate any certificate and will accept *any* certificate.
|
|
61
|
+
|
|
62
|
+
#### Using a client certificate
|
|
63
|
+
|
|
64
|
+
Create a default SSL context, and set your certificates using Python's methods:
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
import tender
|
|
68
|
+
|
|
69
|
+
context = tender.create_default_ssl_context()
|
|
70
|
+
context.load_cert_chain('cert.pem', 'key.pem')
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Note that you must of course use one context per identity.
|
|
74
|
+
|
|
75
|
+
### Sending requests
|
|
76
|
+
|
|
77
|
+
As simple as (all examples assume you already have an asyncio context):
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
import tender
|
|
81
|
+
|
|
82
|
+
context = ...
|
|
83
|
+
|
|
84
|
+
try:
|
|
85
|
+
response = await tender.request('gemini://geminiprotocol.net/', context)
|
|
86
|
+
except ValueError:
|
|
87
|
+
# On invalid URL or malformed response
|
|
88
|
+
...
|
|
89
|
+
except OSError:
|
|
90
|
+
# All other errors
|
|
91
|
+
...
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
A `tender.Response` instance will be returned.
|
|
95
|
+
|
|
96
|
+
#### Custom timeout
|
|
97
|
+
|
|
98
|
+
There's a 5-second timeout by default for the whole request process. You can change that with the third `timeout` argument:
|
|
99
|
+
|
|
100
|
+
```python
|
|
101
|
+
import tender
|
|
102
|
+
|
|
103
|
+
context = ...
|
|
104
|
+
|
|
105
|
+
try:
|
|
106
|
+
response = await tender.request('gemini://geminiprotocol.net/', context, timeout=10) # 10 seconds
|
|
107
|
+
except TimeoutError: # Note it's an OSError subclass
|
|
108
|
+
# Timeout reached
|
|
109
|
+
...
|
|
110
|
+
except ValueError:
|
|
111
|
+
# On invalid URL or malformed response
|
|
112
|
+
...
|
|
113
|
+
except OSError:
|
|
114
|
+
# All other errors
|
|
115
|
+
...
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Inspecting the response
|
|
119
|
+
|
|
120
|
+
Several attributes and methods in `tender.Response` are available:
|
|
121
|
+
|
|
122
|
+
- `is_failure()` determines whether the status is a permanent or temporary failure
|
|
123
|
+
- `is_input()`, `is_success()`, `is_redirect()`, `is_tempfail()`, `is_permfail()` and `is_auth()` determine the status group
|
|
124
|
+
- `status` is the status as an integer
|
|
125
|
+
- For input responses:
|
|
126
|
+
- `prompt` is the prompt as a string
|
|
127
|
+
- For success responses:
|
|
128
|
+
- `body` is the raw response body as bytes
|
|
129
|
+
- `mime_type` is a `tender.MimeType` instance (read below for more info)
|
|
130
|
+
- `gemtext` may be a `tender.Gemtext` instance if the returned MIME type is `text/gemini`, or `None` instead. Read below for more info
|
|
131
|
+
- For redirect responses:
|
|
132
|
+
- `uri` is the redirect URI as a string
|
|
133
|
+
- For temporary failure, permanent failure and auth responses:
|
|
134
|
+
- `error_msg` is the error message as a string or `None` if there is none
|
|
135
|
+
|
|
136
|
+
#### Inspecting the response's MIME type
|
|
137
|
+
|
|
138
|
+
`tender.MimeType` is a named tuple, with the following attributes:
|
|
139
|
+
|
|
140
|
+
- `media_type` - a string containing the actual MIME type (e.g. `text/gemini`)
|
|
141
|
+
- `parameters` - a dictionary containing the associated parameters (e.g. `{'charset': 'utf-8', 'lang': 'fr'}`)
|
|
142
|
+
|
|
143
|
+
#### Reading the parsed gemtext
|
|
144
|
+
|
|
145
|
+
`tender.Gemtext` exposes the following attributes:
|
|
146
|
+
|
|
147
|
+
- `lines` - a list of parsed lines constituting the gemtext document. This list may contain instances of the following named tuples and associated attributes:
|
|
148
|
+
- `tender.Text` (`text` - string)
|
|
149
|
+
- `tender.Link` (`url` - string, `label` - string or `None`)
|
|
150
|
+
- `tender.Heading1` (`text` - string)
|
|
151
|
+
- `tender.Heading2` (`text` - string)
|
|
152
|
+
- `tender.Heading3` (`text` - string)
|
|
153
|
+
- `tender.ListItem` (`text` - string)
|
|
154
|
+
- `tender.Quote` (`text` - string)
|
|
155
|
+
- `tender.Preformatted` (`text` - string, `alt` - string or `None`)
|
|
156
|
+
- `lang` - a list of language codes, parsed from the response's MIME type, or `None` if there's none
|
|
157
|
+
|
|
158
|
+
> [!NOTE]
|
|
159
|
+
> Tender automatically registers the `gemini` scheme in `urllib.parse` upon importing. That will make functions like
|
|
160
|
+
> `urljoin()` work as expected.
|
|
161
|
+
|
|
162
|
+
## Development
|
|
163
|
+
|
|
164
|
+
### Getting source code and installing the package
|
|
165
|
+
|
|
166
|
+
Requires pip >= 25.1.
|
|
167
|
+
|
|
168
|
+
1. Clone the repository
|
|
169
|
+
2. From the package's root: `pip install --editable . --group dev`
|
|
170
|
+
|
|
171
|
+
### Releasing the package
|
|
172
|
+
|
|
173
|
+
1. `__version__` in `src/tender/__init__.py` must be updated beforehand. It should adhere to [Semantic Versioning](https://semver.org/spec/v2.0.0.html)
|
|
174
|
+
2. Create an associated Git tag (must exactly match the aforementioned version)
|
|
175
|
+
3. `rm -rf dist/ && python -m build`
|
|
176
|
+
4. `twine upload dist/*`
|
|
177
|
+
5. An associated GitHub release must be created following the [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) format
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
# Tender
|
|
2
|
+
|
|
3
|
+
A [Gemini](https://geminiprotocol.net/) (the protocol) low-level client, implemented in Python.
|
|
4
|
+
|
|
5
|
+
  
|
|
6
|
+
|
|
7
|
+
[PyPI](https://pypi.org/project/gemini-tender/) - [Documentation](https://github.com/EpocDotFr/tender?tab=readme-ov-file#usage) - [Source code](https://github.com/EpocDotFr/tender) - [Issue tracker](https://github.com/EpocDotFr/tender/issues) - [Changelog](https://github.com/EpocDotFr/tender/releases)
|
|
8
|
+
|
|
9
|
+
> [!TIP]
|
|
10
|
+
> See also: [Harbor](https://github.com/EpocDotFr/harbor), the accompanying server; and [Wayfarer](https://github.com/EpocDotFr/wayfarer), the accompanying [TUI](https://en.wikipedia.org/wiki/Text-based_user_interface) client.
|
|
11
|
+
|
|
12
|
+
## Prerequisites
|
|
13
|
+
|
|
14
|
+
- Python >= 3.14
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
Use your favorite Python package manager to install `gemini-tender`. Example using [Pipenv](https://pipenv.pypa.io/en/latest/):
|
|
19
|
+
|
|
20
|
+
```shell
|
|
21
|
+
pipenv install gemini-tender
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
### Setting up the SSL context
|
|
27
|
+
|
|
28
|
+
The Gemini protocol *requires* usage of TLS >= 1.2. The first thing you thus have to do is to set up an SSL context.
|
|
29
|
+
|
|
30
|
+
A bare-minimum, Gemini-compliant context can be created and used right away:
|
|
31
|
+
|
|
32
|
+
```python
|
|
33
|
+
import tender
|
|
34
|
+
|
|
35
|
+
context = tender.create_default_ssl_context()
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
This method creates a preconfigured [ssl.SSLContext](https://docs.python.org/3.14/library/ssl.html#ssl.SSLContext) instance. However,
|
|
39
|
+
this context will *not* - by default - validate any certificate and will accept *any* certificate.
|
|
40
|
+
|
|
41
|
+
#### Using a client certificate
|
|
42
|
+
|
|
43
|
+
Create a default SSL context, and set your certificates using Python's methods:
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
import tender
|
|
47
|
+
|
|
48
|
+
context = tender.create_default_ssl_context()
|
|
49
|
+
context.load_cert_chain('cert.pem', 'key.pem')
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Note that you must of course use one context per identity.
|
|
53
|
+
|
|
54
|
+
### Sending requests
|
|
55
|
+
|
|
56
|
+
As simple as (all examples assume you already have an asyncio context):
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
import tender
|
|
60
|
+
|
|
61
|
+
context = ...
|
|
62
|
+
|
|
63
|
+
try:
|
|
64
|
+
response = await tender.request('gemini://geminiprotocol.net/', context)
|
|
65
|
+
except ValueError:
|
|
66
|
+
# On invalid URL or malformed response
|
|
67
|
+
...
|
|
68
|
+
except OSError:
|
|
69
|
+
# All other errors
|
|
70
|
+
...
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
A `tender.Response` instance will be returned.
|
|
74
|
+
|
|
75
|
+
#### Custom timeout
|
|
76
|
+
|
|
77
|
+
There's a 5-second timeout by default for the whole request process. You can change that with the third `timeout` argument:
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
import tender
|
|
81
|
+
|
|
82
|
+
context = ...
|
|
83
|
+
|
|
84
|
+
try:
|
|
85
|
+
response = await tender.request('gemini://geminiprotocol.net/', context, timeout=10) # 10 seconds
|
|
86
|
+
except TimeoutError: # Note it's an OSError subclass
|
|
87
|
+
# Timeout reached
|
|
88
|
+
...
|
|
89
|
+
except ValueError:
|
|
90
|
+
# On invalid URL or malformed response
|
|
91
|
+
...
|
|
92
|
+
except OSError:
|
|
93
|
+
# All other errors
|
|
94
|
+
...
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Inspecting the response
|
|
98
|
+
|
|
99
|
+
Several attributes and methods in `tender.Response` are available:
|
|
100
|
+
|
|
101
|
+
- `is_failure()` determines whether the status is a permanent or temporary failure
|
|
102
|
+
- `is_input()`, `is_success()`, `is_redirect()`, `is_tempfail()`, `is_permfail()` and `is_auth()` determine the status group
|
|
103
|
+
- `status` is the status as an integer
|
|
104
|
+
- For input responses:
|
|
105
|
+
- `prompt` is the prompt as a string
|
|
106
|
+
- For success responses:
|
|
107
|
+
- `body` is the raw response body as bytes
|
|
108
|
+
- `mime_type` is a `tender.MimeType` instance (read below for more info)
|
|
109
|
+
- `gemtext` may be a `tender.Gemtext` instance if the returned MIME type is `text/gemini`, or `None` instead. Read below for more info
|
|
110
|
+
- For redirect responses:
|
|
111
|
+
- `uri` is the redirect URI as a string
|
|
112
|
+
- For temporary failure, permanent failure and auth responses:
|
|
113
|
+
- `error_msg` is the error message as a string or `None` if there is none
|
|
114
|
+
|
|
115
|
+
#### Inspecting the response's MIME type
|
|
116
|
+
|
|
117
|
+
`tender.MimeType` is a named tuple, with the following attributes:
|
|
118
|
+
|
|
119
|
+
- `media_type` - a string containing the actual MIME type (e.g. `text/gemini`)
|
|
120
|
+
- `parameters` - a dictionary containing the associated parameters (e.g. `{'charset': 'utf-8', 'lang': 'fr'}`)
|
|
121
|
+
|
|
122
|
+
#### Reading the parsed gemtext
|
|
123
|
+
|
|
124
|
+
`tender.Gemtext` exposes the following attributes:
|
|
125
|
+
|
|
126
|
+
- `lines` - a list of parsed lines constituting the gemtext document. This list may contain instances of the following named tuples and associated attributes:
|
|
127
|
+
- `tender.Text` (`text` - string)
|
|
128
|
+
- `tender.Link` (`url` - string, `label` - string or `None`)
|
|
129
|
+
- `tender.Heading1` (`text` - string)
|
|
130
|
+
- `tender.Heading2` (`text` - string)
|
|
131
|
+
- `tender.Heading3` (`text` - string)
|
|
132
|
+
- `tender.ListItem` (`text` - string)
|
|
133
|
+
- `tender.Quote` (`text` - string)
|
|
134
|
+
- `tender.Preformatted` (`text` - string, `alt` - string or `None`)
|
|
135
|
+
- `lang` - a list of language codes, parsed from the response's MIME type, or `None` if there's none
|
|
136
|
+
|
|
137
|
+
> [!NOTE]
|
|
138
|
+
> Tender automatically registers the `gemini` scheme in `urllib.parse` upon importing. That will make functions like
|
|
139
|
+
> `urljoin()` work as expected.
|
|
140
|
+
|
|
141
|
+
## Development
|
|
142
|
+
|
|
143
|
+
### Getting source code and installing the package
|
|
144
|
+
|
|
145
|
+
Requires pip >= 25.1.
|
|
146
|
+
|
|
147
|
+
1. Clone the repository
|
|
148
|
+
2. From the package's root: `pip install --editable . --group dev`
|
|
149
|
+
|
|
150
|
+
### Releasing the package
|
|
151
|
+
|
|
152
|
+
1. `__version__` in `src/tender/__init__.py` must be updated beforehand. It should adhere to [Semantic Versioning](https://semver.org/spec/v2.0.0.html)
|
|
153
|
+
2. Create an associated Git tag (must exactly match the aforementioned version)
|
|
154
|
+
3. `rm -rf dist/ && python -m build`
|
|
155
|
+
4. `twine upload dist/*`
|
|
156
|
+
5. An associated GitHub release must be created following the [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) format
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "gemini-tender"
|
|
3
|
+
requires-python = ">= 3.14"
|
|
4
|
+
authors = [
|
|
5
|
+
{name = "Maxime 'Epoc' Gross", email = "contact.nospam@epoc.nospam.fr"},
|
|
6
|
+
]
|
|
7
|
+
description = "A Gemini (the protocol) low-level client, implemented in Python"
|
|
8
|
+
readme = "README.md"
|
|
9
|
+
keywords = ["asyncio", "gemini-protocol", "client", "dependency-free"]
|
|
10
|
+
classifiers = [
|
|
11
|
+
"Development Status :: 3 - Alpha",
|
|
12
|
+
"Intended Audience :: Developers",
|
|
13
|
+
"Topic :: Software Development :: Libraries",
|
|
14
|
+
"Topic :: Internet",
|
|
15
|
+
"Operating System :: OS Independent",
|
|
16
|
+
"Programming Language :: Python",
|
|
17
|
+
"Programming Language :: Python :: 3.14",
|
|
18
|
+
"Programming Language :: Python :: 3.15"
|
|
19
|
+
]
|
|
20
|
+
dynamic = ["version"]
|
|
21
|
+
|
|
22
|
+
[project.urls]
|
|
23
|
+
Documentation = "https://github.com/EpocDotFr/tender?tab=readme-ov-file#usage"
|
|
24
|
+
Repository = "https://github.com/EpocDotFr/tender"
|
|
25
|
+
Issues = "https://github.com/EpocDotFr/tender/issues"
|
|
26
|
+
Changelog = "https://github.com/EpocDotFr/tender/releases"
|
|
27
|
+
|
|
28
|
+
[build-system]
|
|
29
|
+
requires = ["setuptools >= 77.0.3"]
|
|
30
|
+
build-backend = "setuptools.build_meta"
|
|
31
|
+
|
|
32
|
+
[dependency-groups]
|
|
33
|
+
dev = [
|
|
34
|
+
"twine~=7.0.0",
|
|
35
|
+
"build~=1.6.0",
|
|
36
|
+
]
|
|
37
|
+
|
|
38
|
+
[tool.setuptools.dynamic]
|
|
39
|
+
version = {attr = "tender.__version__"}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: gemini-tender
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A Gemini (the protocol) low-level client, implemented in Python
|
|
5
|
+
Author-email: Maxime 'Epoc' Gross <contact.nospam@epoc.nospam.fr>
|
|
6
|
+
Project-URL: Documentation, https://github.com/EpocDotFr/tender?tab=readme-ov-file#usage
|
|
7
|
+
Project-URL: Repository, https://github.com/EpocDotFr/tender
|
|
8
|
+
Project-URL: Issues, https://github.com/EpocDotFr/tender/issues
|
|
9
|
+
Project-URL: Changelog, https://github.com/EpocDotFr/tender/releases
|
|
10
|
+
Keywords: asyncio,gemini-protocol,client,dependency-free
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
14
|
+
Classifier: Topic :: Internet
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
19
|
+
Requires-Python: >=3.14
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
|
|
22
|
+
# Tender
|
|
23
|
+
|
|
24
|
+
A [Gemini](https://geminiprotocol.net/) (the protocol) low-level client, implemented in Python.
|
|
25
|
+
|
|
26
|
+
  
|
|
27
|
+
|
|
28
|
+
[PyPI](https://pypi.org/project/gemini-tender/) - [Documentation](https://github.com/EpocDotFr/tender?tab=readme-ov-file#usage) - [Source code](https://github.com/EpocDotFr/tender) - [Issue tracker](https://github.com/EpocDotFr/tender/issues) - [Changelog](https://github.com/EpocDotFr/tender/releases)
|
|
29
|
+
|
|
30
|
+
> [!TIP]
|
|
31
|
+
> See also: [Harbor](https://github.com/EpocDotFr/harbor), the accompanying server; and [Wayfarer](https://github.com/EpocDotFr/wayfarer), the accompanying [TUI](https://en.wikipedia.org/wiki/Text-based_user_interface) client.
|
|
32
|
+
|
|
33
|
+
## Prerequisites
|
|
34
|
+
|
|
35
|
+
- Python >= 3.14
|
|
36
|
+
|
|
37
|
+
## Installation
|
|
38
|
+
|
|
39
|
+
Use your favorite Python package manager to install `gemini-tender`. Example using [Pipenv](https://pipenv.pypa.io/en/latest/):
|
|
40
|
+
|
|
41
|
+
```shell
|
|
42
|
+
pipenv install gemini-tender
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Usage
|
|
46
|
+
|
|
47
|
+
### Setting up the SSL context
|
|
48
|
+
|
|
49
|
+
The Gemini protocol *requires* usage of TLS >= 1.2. The first thing you thus have to do is to set up an SSL context.
|
|
50
|
+
|
|
51
|
+
A bare-minimum, Gemini-compliant context can be created and used right away:
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
import tender
|
|
55
|
+
|
|
56
|
+
context = tender.create_default_ssl_context()
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
This method creates a preconfigured [ssl.SSLContext](https://docs.python.org/3.14/library/ssl.html#ssl.SSLContext) instance. However,
|
|
60
|
+
this context will *not* - by default - validate any certificate and will accept *any* certificate.
|
|
61
|
+
|
|
62
|
+
#### Using a client certificate
|
|
63
|
+
|
|
64
|
+
Create a default SSL context, and set your certificates using Python's methods:
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
import tender
|
|
68
|
+
|
|
69
|
+
context = tender.create_default_ssl_context()
|
|
70
|
+
context.load_cert_chain('cert.pem', 'key.pem')
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Note that you must of course use one context per identity.
|
|
74
|
+
|
|
75
|
+
### Sending requests
|
|
76
|
+
|
|
77
|
+
As simple as (all examples assume you already have an asyncio context):
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
import tender
|
|
81
|
+
|
|
82
|
+
context = ...
|
|
83
|
+
|
|
84
|
+
try:
|
|
85
|
+
response = await tender.request('gemini://geminiprotocol.net/', context)
|
|
86
|
+
except ValueError:
|
|
87
|
+
# On invalid URL or malformed response
|
|
88
|
+
...
|
|
89
|
+
except OSError:
|
|
90
|
+
# All other errors
|
|
91
|
+
...
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
A `tender.Response` instance will be returned.
|
|
95
|
+
|
|
96
|
+
#### Custom timeout
|
|
97
|
+
|
|
98
|
+
There's a 5-second timeout by default for the whole request process. You can change that with the third `timeout` argument:
|
|
99
|
+
|
|
100
|
+
```python
|
|
101
|
+
import tender
|
|
102
|
+
|
|
103
|
+
context = ...
|
|
104
|
+
|
|
105
|
+
try:
|
|
106
|
+
response = await tender.request('gemini://geminiprotocol.net/', context, timeout=10) # 10 seconds
|
|
107
|
+
except TimeoutError: # Note it's an OSError subclass
|
|
108
|
+
# Timeout reached
|
|
109
|
+
...
|
|
110
|
+
except ValueError:
|
|
111
|
+
# On invalid URL or malformed response
|
|
112
|
+
...
|
|
113
|
+
except OSError:
|
|
114
|
+
# All other errors
|
|
115
|
+
...
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Inspecting the response
|
|
119
|
+
|
|
120
|
+
Several attributes and methods in `tender.Response` are available:
|
|
121
|
+
|
|
122
|
+
- `is_failure()` determines whether the status is a permanent or temporary failure
|
|
123
|
+
- `is_input()`, `is_success()`, `is_redirect()`, `is_tempfail()`, `is_permfail()` and `is_auth()` determine the status group
|
|
124
|
+
- `status` is the status as an integer
|
|
125
|
+
- For input responses:
|
|
126
|
+
- `prompt` is the prompt as a string
|
|
127
|
+
- For success responses:
|
|
128
|
+
- `body` is the raw response body as bytes
|
|
129
|
+
- `mime_type` is a `tender.MimeType` instance (read below for more info)
|
|
130
|
+
- `gemtext` may be a `tender.Gemtext` instance if the returned MIME type is `text/gemini`, or `None` instead. Read below for more info
|
|
131
|
+
- For redirect responses:
|
|
132
|
+
- `uri` is the redirect URI as a string
|
|
133
|
+
- For temporary failure, permanent failure and auth responses:
|
|
134
|
+
- `error_msg` is the error message as a string or `None` if there is none
|
|
135
|
+
|
|
136
|
+
#### Inspecting the response's MIME type
|
|
137
|
+
|
|
138
|
+
`tender.MimeType` is a named tuple, with the following attributes:
|
|
139
|
+
|
|
140
|
+
- `media_type` - a string containing the actual MIME type (e.g. `text/gemini`)
|
|
141
|
+
- `parameters` - a dictionary containing the associated parameters (e.g. `{'charset': 'utf-8', 'lang': 'fr'}`)
|
|
142
|
+
|
|
143
|
+
#### Reading the parsed gemtext
|
|
144
|
+
|
|
145
|
+
`tender.Gemtext` exposes the following attributes:
|
|
146
|
+
|
|
147
|
+
- `lines` - a list of parsed lines constituting the gemtext document. This list may contain instances of the following named tuples and associated attributes:
|
|
148
|
+
- `tender.Text` (`text` - string)
|
|
149
|
+
- `tender.Link` (`url` - string, `label` - string or `None`)
|
|
150
|
+
- `tender.Heading1` (`text` - string)
|
|
151
|
+
- `tender.Heading2` (`text` - string)
|
|
152
|
+
- `tender.Heading3` (`text` - string)
|
|
153
|
+
- `tender.ListItem` (`text` - string)
|
|
154
|
+
- `tender.Quote` (`text` - string)
|
|
155
|
+
- `tender.Preformatted` (`text` - string, `alt` - string or `None`)
|
|
156
|
+
- `lang` - a list of language codes, parsed from the response's MIME type, or `None` if there's none
|
|
157
|
+
|
|
158
|
+
> [!NOTE]
|
|
159
|
+
> Tender automatically registers the `gemini` scheme in `urllib.parse` upon importing. That will make functions like
|
|
160
|
+
> `urljoin()` work as expected.
|
|
161
|
+
|
|
162
|
+
## Development
|
|
163
|
+
|
|
164
|
+
### Getting source code and installing the package
|
|
165
|
+
|
|
166
|
+
Requires pip >= 25.1.
|
|
167
|
+
|
|
168
|
+
1. Clone the repository
|
|
169
|
+
2. From the package's root: `pip install --editable . --group dev`
|
|
170
|
+
|
|
171
|
+
### Releasing the package
|
|
172
|
+
|
|
173
|
+
1. `__version__` in `src/tender/__init__.py` must be updated beforehand. It should adhere to [Semantic Versioning](https://semver.org/spec/v2.0.0.html)
|
|
174
|
+
2. Create an associated Git tag (must exactly match the aforementioned version)
|
|
175
|
+
3. `rm -rf dist/ && python -m build`
|
|
176
|
+
4. `twine upload dist/*`
|
|
177
|
+
5. An associated GitHub release must be created following the [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) format
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
README.md
|
|
2
|
+
pyproject.toml
|
|
3
|
+
src/gemini_tender.egg-info/PKG-INFO
|
|
4
|
+
src/gemini_tender.egg-info/SOURCES.txt
|
|
5
|
+
src/gemini_tender.egg-info/dependency_links.txt
|
|
6
|
+
src/gemini_tender.egg-info/top_level.txt
|
|
7
|
+
src/tender/__init__.py
|
|
8
|
+
src/tender/gemtext.py
|
|
9
|
+
src/tender/response.py
|
|
10
|
+
src/tender/support.py
|
|
11
|
+
src/tender/tender.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
tender
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
from tender.gemtext import Gemtext, Text, Link, Heading1, Heading2, Heading3, ListItem, Quote, Preformatted
|
|
2
|
+
from tender.support import create_default_ssl_context, MimeType
|
|
3
|
+
from tender.response import Response
|
|
4
|
+
from tender.tender import request
|
|
5
|
+
|
|
6
|
+
__version__ = '0.1.0'
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
from typing import NamedTuple, Optional, List, Union
|
|
2
|
+
import re
|
|
3
|
+
|
|
4
|
+
text_regex = re.compile(r'=>\s*(?P<url>\S+)(?:\s+(?P<label>.+))?')
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class Text(NamedTuple):
|
|
8
|
+
text: str
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class Link(NamedTuple):
|
|
12
|
+
url: str
|
|
13
|
+
label: Optional[str] = None
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class Heading1(NamedTuple):
|
|
17
|
+
text: str
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class Heading2(NamedTuple):
|
|
21
|
+
text: str
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class Heading3(NamedTuple):
|
|
25
|
+
text: str
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class ListItem(NamedTuple):
|
|
29
|
+
text: str
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class Quote(NamedTuple):
|
|
33
|
+
text: str
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class Preformatted(NamedTuple):
|
|
37
|
+
text: str
|
|
38
|
+
alt: Optional[str] = None
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class Gemtext:
|
|
42
|
+
def __init__(self, lang: Optional[Union[str, List[str]]] = None):
|
|
43
|
+
self.lines: List[Union[Text, Link, Heading1, Heading2, Heading3, ListItem, Quote, Preformatted]] = []
|
|
44
|
+
self.lang: Optional[List[str]] = lang if isinstance(lang, list) else lang.split(',') if isinstance(lang, str) else None
|
|
45
|
+
|
|
46
|
+
def text(self, text: str) -> None:
|
|
47
|
+
self.lines.append(
|
|
48
|
+
Text(text)
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
def link(self, url: str, label: Optional[str] = None) -> None:
|
|
52
|
+
self.lines.append(
|
|
53
|
+
Link(url, label)
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
def heading_1(self, text: str) -> None:
|
|
57
|
+
self.lines.append(
|
|
58
|
+
Heading1(text)
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
def heading_2(self, text: str) -> None:
|
|
62
|
+
self.lines.append(
|
|
63
|
+
Heading2(text)
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
def heading_3(self, text: str) -> None:
|
|
67
|
+
self.lines.append(
|
|
68
|
+
Heading3(text)
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
def list_item(self, text: str) -> None:
|
|
72
|
+
self.lines.append(
|
|
73
|
+
ListItem(text)
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
def quote(self, text: str) -> None:
|
|
77
|
+
self.lines.append(
|
|
78
|
+
Quote(text)
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
def preformatted(self, text: str, alt: Optional[str] = None) -> None:
|
|
82
|
+
self.lines.append(
|
|
83
|
+
Preformatted(text, alt)
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
@staticmethod
|
|
87
|
+
def create(body: str, lang: Optional[Union[str, List[str]]] = None) -> Gemtext:
|
|
88
|
+
gemtext = Gemtext(lang)
|
|
89
|
+
|
|
90
|
+
pre = False
|
|
91
|
+
pre_text = ''
|
|
92
|
+
pre_alt = None
|
|
93
|
+
|
|
94
|
+
for line in body.splitlines():
|
|
95
|
+
if line.startswith('```'):
|
|
96
|
+
if not pre:
|
|
97
|
+
pre = True
|
|
98
|
+
|
|
99
|
+
pre_text = ''
|
|
100
|
+
pre_alt = line.removeprefix('```').lstrip() or None
|
|
101
|
+
else:
|
|
102
|
+
pre = False
|
|
103
|
+
|
|
104
|
+
gemtext.preformatted(pre_text, pre_alt)
|
|
105
|
+
elif pre:
|
|
106
|
+
pre_text += f'{line}\r\n'
|
|
107
|
+
elif line.startswith('=>'):
|
|
108
|
+
match = text_regex.fullmatch(line)
|
|
109
|
+
|
|
110
|
+
if match:
|
|
111
|
+
match = match.groupdict()
|
|
112
|
+
|
|
113
|
+
gemtext.link(match.get('url'), match.get('label')) # type: ignore[assignment]
|
|
114
|
+
else:
|
|
115
|
+
gemtext.text(line)
|
|
116
|
+
elif line.startswith('###'):
|
|
117
|
+
gemtext.heading_3(
|
|
118
|
+
line.removeprefix('###').lstrip()
|
|
119
|
+
)
|
|
120
|
+
elif line.startswith('##'):
|
|
121
|
+
gemtext.heading_2(
|
|
122
|
+
line.removeprefix('##').lstrip()
|
|
123
|
+
)
|
|
124
|
+
elif line.startswith('#'):
|
|
125
|
+
gemtext.heading_1(
|
|
126
|
+
line.removeprefix('#').lstrip()
|
|
127
|
+
)
|
|
128
|
+
elif line.startswith('* '):
|
|
129
|
+
gemtext.list_item(
|
|
130
|
+
line.removeprefix('*').lstrip()
|
|
131
|
+
)
|
|
132
|
+
elif line.startswith('>'):
|
|
133
|
+
gemtext.quote(
|
|
134
|
+
line.removeprefix('>').lstrip()
|
|
135
|
+
)
|
|
136
|
+
else:
|
|
137
|
+
gemtext.text(line)
|
|
138
|
+
|
|
139
|
+
if pre:
|
|
140
|
+
gemtext.preformatted(pre_text, pre_alt)
|
|
141
|
+
|
|
142
|
+
return gemtext
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
from tender.support import MimeType
|
|
2
|
+
from tender.gemtext import Gemtext
|
|
3
|
+
from typing import Optional
|
|
4
|
+
import asyncio
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class Response:
|
|
8
|
+
def __init__(
|
|
9
|
+
self,
|
|
10
|
+
status: int,
|
|
11
|
+
body: Optional[bytes] = None,
|
|
12
|
+
mime_type: Optional[MimeType] = None,
|
|
13
|
+
gemtext: Optional[Gemtext] = None,
|
|
14
|
+
prompt: Optional[str] = None,
|
|
15
|
+
uri: Optional[str] = None,
|
|
16
|
+
error_msg: Optional[str] = None
|
|
17
|
+
):
|
|
18
|
+
self.status = status
|
|
19
|
+
self.body = body
|
|
20
|
+
self.mime_type = mime_type
|
|
21
|
+
self.gemtext = gemtext
|
|
22
|
+
self.prompt = prompt
|
|
23
|
+
self.uri = uri
|
|
24
|
+
self.error_msg = error_msg
|
|
25
|
+
|
|
26
|
+
@staticmethod
|
|
27
|
+
async def create(reader: asyncio.StreamReader) -> Response:
|
|
28
|
+
try:
|
|
29
|
+
status = (await reader.readuntil(b' '))[:-1]
|
|
30
|
+
except asyncio.IncompleteReadError:
|
|
31
|
+
raise ValueError('Incomplete status') from None
|
|
32
|
+
|
|
33
|
+
try:
|
|
34
|
+
if len(status) != 2:
|
|
35
|
+
raise ValueError
|
|
36
|
+
|
|
37
|
+
status = int(status.decode())
|
|
38
|
+
except ValueError:
|
|
39
|
+
raise ValueError(f'Malformed status: {status}') from None
|
|
40
|
+
|
|
41
|
+
if Response.is_input(status):
|
|
42
|
+
try:
|
|
43
|
+
prompt = await reader.readuntil(b'\r\n')
|
|
44
|
+
except asyncio.IncompleteReadError:
|
|
45
|
+
raise ValueError('No prompt received') from None
|
|
46
|
+
|
|
47
|
+
return Response(status, prompt=prompt[:-2].decode(errors='replace'))
|
|
48
|
+
elif Response.is_success(status):
|
|
49
|
+
try:
|
|
50
|
+
mime_type = await reader.readuntil(b'\r\n')
|
|
51
|
+
except asyncio.IncompleteReadError:
|
|
52
|
+
mime_type = None
|
|
53
|
+
|
|
54
|
+
mime_type = Response.parse_mime_type(mime_type[:-2] if mime_type else None)
|
|
55
|
+
|
|
56
|
+
body = (await reader.read()) or b''
|
|
57
|
+
gemtext = None
|
|
58
|
+
|
|
59
|
+
if body and mime_type.media_type == 'text/gemini':
|
|
60
|
+
try:
|
|
61
|
+
body_decoded = body.decode(mime_type.parameters.get('charset', 'utf-8'), errors='replace')
|
|
62
|
+
except LookupError:
|
|
63
|
+
body_decoded = body.decode('utf-8', errors='replace')
|
|
64
|
+
|
|
65
|
+
gemtext = Gemtext.create(
|
|
66
|
+
body_decoded,
|
|
67
|
+
mime_type.parameters.get('lang')
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
return Response(status, mime_type=mime_type, body=body, gemtext=gemtext)
|
|
71
|
+
elif Response.is_redirect(status):
|
|
72
|
+
try:
|
|
73
|
+
uri = await reader.readuntil(b'\r\n')
|
|
74
|
+
except asyncio.IncompleteReadError:
|
|
75
|
+
raise ValueError('No uri received') from None
|
|
76
|
+
|
|
77
|
+
return Response(status, uri=uri[:-2].decode(errors='replace'))
|
|
78
|
+
elif Response.is_tempfail(status) or Response.is_permfail(status) or Response.is_auth(status):
|
|
79
|
+
try:
|
|
80
|
+
error_msg = await reader.readuntil(b'\r\n')
|
|
81
|
+
except asyncio.IncompleteReadError as e:
|
|
82
|
+
error_msg = e.partial
|
|
83
|
+
|
|
84
|
+
return Response(
|
|
85
|
+
status,
|
|
86
|
+
error_msg=error_msg.removesuffix(b'\r\n').decode(errors='replace') if error_msg else None
|
|
87
|
+
)
|
|
88
|
+
else:
|
|
89
|
+
raise ValueError(f'Invalid response status: {status}')
|
|
90
|
+
|
|
91
|
+
def is_failure(self) -> bool:
|
|
92
|
+
return Response.is_permfail(self.status) or Response.is_tempfail(self.status)
|
|
93
|
+
|
|
94
|
+
@staticmethod
|
|
95
|
+
def is_input(status: int) -> bool:
|
|
96
|
+
return 10 <= status <= 19
|
|
97
|
+
|
|
98
|
+
@staticmethod
|
|
99
|
+
def is_success(status: int) -> bool:
|
|
100
|
+
return 20 <= status <= 29
|
|
101
|
+
|
|
102
|
+
@staticmethod
|
|
103
|
+
def is_redirect(status: int) -> bool:
|
|
104
|
+
return 30 <= status <= 39
|
|
105
|
+
|
|
106
|
+
@staticmethod
|
|
107
|
+
def is_tempfail(status: int) -> bool:
|
|
108
|
+
return 40 <= status <= 49
|
|
109
|
+
|
|
110
|
+
@staticmethod
|
|
111
|
+
def is_permfail(status: int) -> bool:
|
|
112
|
+
return 50 <= status <= 59
|
|
113
|
+
|
|
114
|
+
@staticmethod
|
|
115
|
+
def is_auth(status: int) -> bool:
|
|
116
|
+
return 60 <= status <= 69
|
|
117
|
+
|
|
118
|
+
@staticmethod
|
|
119
|
+
def parse_mime_type(mime_type: Optional[bytes]) -> MimeType:
|
|
120
|
+
if not mime_type:
|
|
121
|
+
return MimeType('text/gemini', {'charset': 'utf-8'})
|
|
122
|
+
|
|
123
|
+
mime_type = mime_type.lower().decode(errors='replace').replace(' ', '')
|
|
124
|
+
|
|
125
|
+
if ';' in mime_type:
|
|
126
|
+
mime_type, raw_params = mime_type.split(';', maxsplit=1)
|
|
127
|
+
|
|
128
|
+
param_items = [
|
|
129
|
+
raw_param.split('=', 1) for raw_param in raw_params.split(';') if '=' in raw_param
|
|
130
|
+
]
|
|
131
|
+
|
|
132
|
+
params = {
|
|
133
|
+
param[0]: param[1] for param in param_items
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return MimeType(mime_type, params)
|
|
137
|
+
else:
|
|
138
|
+
return MimeType(mime_type, {})
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from urllib.parse import uses_relative, uses_netloc
|
|
2
|
+
from typing import NamedTuple, Dict
|
|
3
|
+
import ssl
|
|
4
|
+
|
|
5
|
+
uses_relative.append('gemini')
|
|
6
|
+
uses_netloc.append('gemini')
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def create_default_ssl_context() -> ssl.SSLContext:
|
|
10
|
+
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
|
|
11
|
+
ssl_context.minimum_version = ssl.TLSVersion.TLSv1_2
|
|
12
|
+
ssl_context.load_default_certs(ssl.Purpose.SERVER_AUTH)
|
|
13
|
+
ssl_context.check_hostname = False
|
|
14
|
+
ssl_context.verify_mode = ssl.CERT_NONE
|
|
15
|
+
|
|
16
|
+
return ssl_context
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class MimeType(NamedTuple):
|
|
20
|
+
media_type: str
|
|
21
|
+
parameters: Dict[str, str]
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
from tender.response import Response
|
|
2
|
+
from urllib.parse import urlsplit
|
|
3
|
+
import logging
|
|
4
|
+
import asyncio
|
|
5
|
+
import ssl
|
|
6
|
+
|
|
7
|
+
MAX_URI_LENGTH = 1024
|
|
8
|
+
|
|
9
|
+
logger = logging.getLogger('tender')
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
async def request(url: str, ssl_context: ssl.SSLContext, timeout: float | int = 5) -> Response:
|
|
13
|
+
if len(url.encode()) > MAX_URI_LENGTH:
|
|
14
|
+
raise ValueError(f'URI too long (must be max {MAX_URI_LENGTH} bytes)')
|
|
15
|
+
|
|
16
|
+
try:
|
|
17
|
+
parsed_url = urlsplit(url)
|
|
18
|
+
except ValueError:
|
|
19
|
+
raise ValueError('Invalid URI') from None
|
|
20
|
+
|
|
21
|
+
if not parsed_url.hostname:
|
|
22
|
+
raise ValueError('URI without hostname')
|
|
23
|
+
if parsed_url.username or parsed_url.password:
|
|
24
|
+
raise ValueError('URI with userinfo')
|
|
25
|
+
elif parsed_url.fragment:
|
|
26
|
+
raise ValueError('URI with fragment')
|
|
27
|
+
elif parsed_url.scheme != 'gemini':
|
|
28
|
+
raise ValueError(f'Invalid scheme: {parsed_url.scheme or "(none)"}')
|
|
29
|
+
|
|
30
|
+
async with asyncio.timeout(timeout):
|
|
31
|
+
reader, writer = await asyncio.open_connection(
|
|
32
|
+
parsed_url.hostname,
|
|
33
|
+
parsed_url.port or 1965,
|
|
34
|
+
ssl=ssl_context,
|
|
35
|
+
server_hostname=parsed_url.hostname
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
logger.debug(url)
|
|
39
|
+
|
|
40
|
+
try:
|
|
41
|
+
writer.write(f'{url}\r\n'.encode())
|
|
42
|
+
|
|
43
|
+
await writer.drain()
|
|
44
|
+
|
|
45
|
+
return await Response.create(reader)
|
|
46
|
+
finally:
|
|
47
|
+
writer.close()
|
|
48
|
+
|
|
49
|
+
try:
|
|
50
|
+
await writer.wait_closed()
|
|
51
|
+
except Exception:
|
|
52
|
+
pass
|