Client API¶
API reference for the Mototli Gopher client.
GopherClient¶
GopherClient
¶
High-level Gopher client with async/await API.
This class provides a simple, high-level interface for getting Gopher resources. It handles connection management, timeouts, and response parsing.
Examples:
>>> # Basic usage
>>> async with GopherClient() as client:
... response = await client.get("gopher.floodgap.com")
... for item in response.items:
... print(item)
>>> # Get a specific resource
>>> response = await client.get(
... "gopher.floodgap.com",
... selector="/overbite",
... )
>>> # Get with search query
>>> response = await client.get(
... "gopher.floodgap.com",
... selector="/v2/vs",
... search_query="python",
... )
Initialize the Gopher client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
float
|
Request timeout in seconds. Default is 30 seconds. |
REQUEST_TIMEOUT
|
Source code in src/mototli/client/session.py
get
async
¶
get(
host: str,
selector: str = "",
port: int = DEFAULT_PORT,
search_query: str | None = None,
item_type: ItemType = ItemType.DIRECTORY,
) -> GopherResponse
Get a Gopher resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
str
|
The Gopher server hostname. |
required |
selector
|
str
|
The resource selector (path). Default is empty (root). |
''
|
port
|
int
|
The server port. Default is 70. |
DEFAULT_PORT
|
search_query
|
str | None
|
Optional search query for type 7 (search) items. |
None
|
item_type
|
ItemType
|
Expected item type. Used to determine if response is a directory listing. Default is DIRECTORY. |
DIRECTORY
|
Returns:
| Type | Description |
|---|---|
GopherResponse
|
A GopherResponse object with items (for directories) or raw_body. |
Raises:
| Type | Description |
|---|---|
TimeoutError
|
If the request times out. |
ConnectionError
|
If the connection fails. |
Examples:
>>> response = await client.get("gopher.floodgap.com")
>>> for item in response.items:
... print(f"[{item.item_type.value}] {item.display_text}")
Source code in src/mototli/client/session.py
get_text
async
¶
Get a text resource and return its content as a string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
str
|
The Gopher server hostname. |
required |
selector
|
str
|
The resource selector (path). |
required |
port
|
int
|
The server port. Default is 70. |
DEFAULT_PORT
|
Returns:
| Type | Description |
|---|---|
str
|
The text content as a string. |
Raises:
| Type | Description |
|---|---|
TimeoutError
|
If the request times out. |
ConnectionError
|
If the connection fails. |
ValueError
|
If the response is not text. |
Examples:
>>> text = await client.get_text(
... "gopher.floodgap.com",
... "/gopher/welcome",
... )
>>> print(text)
Source code in src/mototli/client/session.py
get_binary
async
¶
Get a binary resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
str
|
The Gopher server hostname. |
required |
selector
|
str
|
The resource selector (path). |
required |
port
|
int
|
The server port. Default is 70. |
DEFAULT_PORT
|
Returns:
| Type | Description |
|---|---|
bytes
|
The binary content as bytes. |
Raises:
| Type | Description |
|---|---|
TimeoutError
|
If the request times out. |
ConnectionError
|
If the connection fails. |
Examples:
>>> data = await client.get_binary(
... "gopher.floodgap.com",
... "/some/image.gif",
... )
>>> with open("image.gif", "wb") as f:
... f.write(data)
Source code in src/mototli/client/session.py
get_attributes
async
¶
Get Gopher+ attributes for a resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
str
|
The Gopher server hostname. |
required |
selector
|
str
|
The resource selector (path). |
required |
port
|
int
|
The server port. Default is 70. |
DEFAULT_PORT
|
Returns:
| Type | Description |
|---|---|
GopherAttributes
|
A GopherAttributes object with metadata. |
Raises:
| Type | Description |
|---|---|
TimeoutError
|
If the request times out. |
ConnectionError
|
If the connection fails. |
Examples:
>>> attrs = await client.get_attributes(
... "gopher.example.com",
... "/about",
... )
>>> print(attrs.abstract)
Source code in src/mototli/client/session.py
get_with_view
async
¶
get_with_view(
host: str,
selector: str,
view_type: str,
port: int = DEFAULT_PORT,
) -> GopherResponse
Get a specific view of a Gopher+ resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
str
|
The Gopher server hostname. |
required |
selector
|
str
|
The resource selector (path). |
required |
view_type
|
str
|
The MIME type of the view to request. |
required |
port
|
int
|
The server port. Default is 70. |
DEFAULT_PORT
|
Returns:
| Type | Description |
|---|---|
GopherResponse
|
A GopherResponse with the requested view. |
Raises:
| Type | Description |
|---|---|
TimeoutError
|
If the request times out. |
ConnectionError
|
If the connection fails. |
Examples:
>>> response = await client.get_with_view(
... "gopher.example.com",
... "/document",
... "text/plain",
... )
Source code in src/mototli/client/session.py
Usage Examples¶
Basic Usage¶
import asyncio
from mototli.client import GopherClient
async def main():
async with GopherClient(timeout=30.0) as client:
response = await client.get("gopher.floodgap.com", "/")
for item in response.items:
print(f"[{item.item_type.value}] {item.display_text}")
asyncio.run(main())
Fetching Text¶
async with GopherClient() as client:
response = await client.get_text("gopher.floodgap.com", "/gopher/welcome")
print(response.content.decode())
Fetching Binary¶
async with GopherClient() as client:
response = await client.get_binary("gopher.floodgap.com", "/file.zip")
with open("file.zip", "wb") as f:
f.write(response.content)
Gopher+ Attributes¶
async with GopherClient() as client:
attrs = await client.get_attributes("gopher.floodgap.com", "/gopher")
print(f"Admin: {attrs.admin.name}")
print(f"Views: {[v.content_type for v in attrs.views]}")
Search Queries¶
async with GopherClient() as client:
response = await client.get(
"gopher.floodgap.com",
"/v2/vs",
search_query="python"
)
for item in response.items:
print(item.display_text)
Error Handling¶
try:
async with GopherClient(timeout=10.0) as client:
response = await client.get("example.com", "/")
except TimeoutError:
print("Connection timed out")
except ConnectionRefusedError:
print("Connection refused")
except OSError as e:
print(f"Network error: {e}")
See Also¶
- Building a Client - Tutorial
- Fetch Resources - How-to guide
- Protocol API - Response types