spartan-py/README.md

66 lines
1.3 KiB
Markdown
Raw Permalink Normal View History

2021-07-15 02:14:24 +00:00
# spartan-py
Basic spartan protocol implementation as a python library.
```python
import spartan
res = spartan.get("spartan://mozz.us/echo", "hi")
while True:
2021-07-15 02:37:30 +00:00
buf = res.read()
if not buf:
break
sys.stdout.buffer.write(buf)
2021-07-15 02:14:24 +00:00
res.close()
```
Try it in the REPL:
```python
>>> import spartan
>>> req = spartan.Request("spartan.mozz.us")
>>> req
Request(host='spartan.mozz.us', port=300, path='/') data-length=0
2021-07-26 07:40:37 +00:00
>>> print(req)
'spartan.mozz.us / 0'
2021-07-15 02:14:24 +00:00
>>> res = req.send()
>>> res
2021-07-16 04:23:45 +00:00
2 text/gemini
2021-07-15 02:14:24 +00:00
>>> res.read()
2021-07-16 04:23:45 +00:00
[...]
2021-07-15 02:14:24 +00:00
>>> res.close()
```
2021-07-15 02:48:59 +00:00
## install
```
pip3 install spartan-py
```
## API
- `Request(host: str, port: int = 300, path: str = "/", data: str = "")`
- `send() -> Response` - send the request
2021-07-15 02:48:59 +00:00
- `__repr__()`
- `__str__()`
- `Response(socket)`
- `read()`
- `close()` - close the socket
- `.status` - status code
- `.meta` - meta string for the status
- `.file` - socket file
- `.request` - the Request object for this response
2021-07-15 02:48:59 +00:00
- `__repr__()`
- `__str__()`
- `Status` - statuses
- `success = 2`
- `redirect = 3`
- `client_error = 4`
- `server_error = 5`
- `get(url: str, data: str = "") -> Response` - if the query string part in the URL exists, data will be ignored.
## TODO
- [ ] invalid url handling
- [ ] util functions like parsing meta and getting status type
- [ ] basic CLI usage
- [ ] async methods