A Proof of concept of a modern python CLI with click, pydantic, rich and anyio

Overview

httpcli

This project is a proof of concept of a modern python networking cli which can be simple and easy to maintain using some of the best packages in the python ecosystem:

  • click for the foundation of a CLI application. There is also asyncclick that I used in this project which is a tiny wrapper around click to provide asynchronous support.
  • rich for pretty printing in the terminal.
  • httpx for HTTP protocol stuff.
  • anyio for concurrency.
  • pytest and pytest-trio for easy testing.

This is not a complete and mature project like httpie but I want to implement some features not present in this beautiful package like:

  • HTTP2 support
  • more authentication scheme support like digest and oauth2
  • easy cookies support
  • support of posix signals like SIGINT and SIGTERM
  • completion feature
  • git "did you mean" like feature
  • sse support

Evolution

I'm not quite sure if I will continue improving it without any motivation (sponsoring?) but it is already useful if you want to test it, you just need to have poetry dependency manager and install the project locally (poetry install). This will install two commands:

  • http useful when you don't want the cli to verify server certificate.
  • https when you need to verify server certificate.

Usage

Hopefully subcommand usage should be straightforward, but I will point some specific cases.

Usage: http [OPTIONS] COMMAND [ARGS]...

  HTTP CLI

Options:
  --config-file FILENAME          A configuration file with options used to
                                  set the cli. Note that the file takes
                                  precedence over the other options.
  -t, --timeout FLOAT             Time for request to complete, a negative
                                  value means there is no timeout.
  --follow-redirects / -N, --no-follow-redirects
                                  flag to decide if http redirections must be
                                  followed
  --auth JSON_AUTH                A json string representing authentication
                                  information.
  --http-version [h1|h2]          Version of http used to make the request.
  --proxy URL                     Proxy url.
  --version                       Show the version and exit.
  --help                          Show this message and exit.

Commands:
  delete              Performs http DELETE request.
  download            Process download of urls given as arguments.
  get                 Performs http GET request.
  head                Performs http HEAD request.
  install-completion  Install completion script for bash, zsh and fish...
  options             Performs http OPTIONS request.
  patch               Performs http PATCH request.
  post                Performs http POST request.
  put                 Performs http PUT request.
  sse                 Reads and print SSE events on a given url.

Global cli configuration

There are some options that can be configured on the root command. These options can be read from a yaml file using option --config-file. The config file looks lie the following:

# all options have default values, no need to specify them all
httpcli:
  http_version: h2
  follow_redirects: true
  proxy: https://proxy.com
  # timeout may be null to specify that you don't want a timeout
  timeout: 5.0
  auth:
    type: oauth2
    flow: password
    username: user
    password: pass
  # for https you also have the verify option to pass a custom certificate used to authenticate the server
  verify: /path/to/certificate

Those options can also be configured via environment variables. They are all prefixed with HTTP_CLI_ and they can be in lowercase or uppercase. Here is the same configuration as above but using environment variables:

HTTP_CLI_HTTP_VERSION=h2
HTTP_CLI_FOLLOW_REDIRECTS=true
HTTP_CLI_PROXY=https://proxy.com
HTTP_CLI_TIMEOUT=5.0
# here value is passed as json
HTTP_CLI_AUTH={"type": "oauth2", "flow": "password", "username": "user", "password": "pass"}
HTTP_CLI_VERIFY=/path/to/certificate

Commands

install-completion

This is obviously the first command you will want to use to have subcommand and option autocompletion. You don't need to do that for the two cli http and https. Doing it with one will install the other. The current shells supported are bash, zsh and fish. To use autocompletion for subcommands, just enter the first letter and use TAB key twice. For option autocompletion, enter the first dash and use TAB twice.

get, head, option, delete

The usage should be pretty straightforward for these commands.

http get --help
Usage: http get [OPTIONS] URL

  Performs http GET request.

  URL is the target url.

Options:
  -c, --cookie COOKIE  Cookie passed to the request, can by passed multiple
                       times.
  -H, --header HEADER  Header passed to the request, can by passed multiple
                       times.
  -q, --query QUERY    Querystring argument passed to the request, can by
                       passed multiple times.
  --help               Show this message and exit.

You can play with it using https://pie.dev. Here is a simple example:

http get https://pie.dev/get -c my:cookie -q my:query -H X-MY:HEADER

post, put, patch

There are some subtleties with these commands. I will use post in the following examples but the same apply to put and patch.

json data

If you play with json, in case you only have string values, you can do this:

# here we are sending {"foo": "bar", "hello": "world"} to https://pie.dev/post
http post https://pie.dev/post -j foo:bar -j hello:world

If you need to send other values than strings, you will need to pass the json encoded value with a slightly different syntax, := instead of =.

http post https://pie.dev/post -j number:='2' -j boolean:='true' -j fruits:='["apple", "pineapple"]'

If you have a deeply nested structure you can't write simple in the terminal, you can use of json file instead. Considering we have a file fruits.json with the following content:

[
  "apple",
  "pineapple"
]

You can use the file like it:

http post https://pie.dev/post -j fruits:@fruits.json

form data

First you need to know that you can't pass form data and json data in the same request. You must choose between the two methods. The basic usage is the following:

https post https://pie.dev/post -f foo:bar -f number:2

If you need to send files, here is what you can do:

# this will send the key "foo" with the value "bar" and the key "photo" with the file photo.jpg
https post https://pie.dev/post -f foo:bar -f photo:@photo.jpg

If you want to send raw data, use the following form:

https post https://pie.dev/post --raw 'raw content'

You can also pass the raw content in a file:

# you can put what you want in your file, just be sure to set the correct content-type
https post https://pie.dev/post --raw @hello.txt

download

You can pass urls as arguments. Files will be downloaded in the current directory. If you wish to change the directory where files should be put, pass the -d option with the path of the desired destination folder.

# this will downloads two files and put them in the downloads directory of the current user
https download https://pie.dev/image/jpeg https://pie.dev/image/png -d ~/downloads

You can use a file to specify all the resources to download. There should be one url per line. Consider a file urls.txt having the following content:

https://pie.dev/image/svg
https://pie.def/image/webp

You can download urls from the file and urls from the command line at the same time:

https download https://pie.dev/image/jpeg -f urls.txt

sse

If you want to listen sse events from an endpoint, you can simply do this:

# The sse command will not stop if the data are sent without interruption, which is almost always the case
# with sse, so if you want to stop it, just Ctrl + C ;)
https sse https://endpoint.com/sse

What needs to be improved?

If I were to continue the development of the project, here are the points to review/enhance:

  • adapt code to support httpx 1.0 . At the moment of writing it is still in beta, but there is at least one breaking change concerning allow_redirects option.
  • add more authentication schemes, mainly all the oauth2 flows, but may be some others like macaroon...
  • support multiple proxy values
  • session support
  • add CI/CD
  • improve code coverage (not 100% yet)
  • refactor a bit the code, currently I don't like the structure of my helpers modules. Also auth support can be refactored using this technique I was not aware of when starting this project.
  • add autocompletion featurefor other shells like ksh, powershell or powercore
  • and probably more... :)
Owner
Kevin Tewouda
Passionate about python programming and more specifically what affects the web and computer networks. Structured concurrency enthusiast
Kevin Tewouda
pytest plugin that let you automate actions and assertions with test metrics reporting executing plain YAML files

pytest-play pytest-play is a codeless, generic, pluggable and extensible automation tool, not necessarily test automation only, based on the fantastic

pytest-dev 67 Dec 01, 2022
Mockoon is the easiest and quickest way to run mock APIs locally. No remote deployment, no account required, open source.

Mockoon Mockoon is the easiest and quickest way to run mock APIs locally. No remote deployment, no account required, open source. It has been built wi

mockoon 4.4k Dec 30, 2022
Show, Edit and Tell: A Framework for Editing Image Captions, CVPR 2020

Show, Edit and Tell: A Framework for Editing Image Captions | arXiv This contains the source code for Show, Edit and Tell: A Framework for Editing Ima

Fawaz Sammani 76 Nov 25, 2022
Pytest-rich - Pytest + rich integration (proof of concept)

pytest-rich Leverage rich for richer test session output. This plugin is not pub

Bruno Oliveira 170 Dec 02, 2022
AllPairs is an open source test combinations generator written in Python

AllPairs is an open source test combinations generator written in Python

Robson Agapito Correa 5 Mar 05, 2022
This is a bot that can type without any assistance and have incredible speed.

BulldozerType This is a bot that can type without any assistance and have incredible speed. This bot currently only works on the site https://onlinety

1 Jan 03, 2022
Custom Selenium Chromedriver | Zero-Config | Passes ALL bot mitigation systems (like Distil / Imperva/ Datadadome / CloudFlare IUAM)

Custom Selenium Chromedriver | Zero-Config | Passes ALL bot mitigation systems (like Distil / Imperva/ Datadadome / CloudFlare IUAM)

Leon 3.5k Dec 30, 2022
Parameterized testing with any Python test framework

Parameterized testing with any Python test framework Parameterized testing in Python sucks. parameterized fixes that. For everything. Parameterized te

David Wolever 714 Dec 21, 2022
A test fixtures replacement for Python

factory_boy factory_boy is a fixtures replacement based on thoughtbot's factory_bot. As a fixtures replacement tool, it aims to replace static, hard t

FactoryBoy project 3k Jan 05, 2023
A pure Python script to easily get a reverse shell

easy-shell A pure Python script to easily get a reverse shell. How it works? After sending a request, it generates a payload with different commands a

Cristian Souza 48 Dec 12, 2022
Automated mouse clicker script using PyAutoGUI and Typer.

clickpy Automated mouse clicker script using PyAutoGUI and Typer. This app will randomly click your mouse between 1 second and 3 minutes, to prevent y

Joe Fitzgibbons 0 Dec 01, 2021
The definitive testing tool for Python. Born under the banner of Behavior Driven Development (BDD).

mamba: the definitive test runner for Python mamba is the definitive test runner for Python. Born under the banner of behavior-driven development. Ins

Néstor Salceda 502 Dec 30, 2022
自动化爬取并自动测试所有swagger-ui.html显示的接口

swagger-hack 在测试中偶尔会碰到swagger泄露 常见的泄露如图: 有的泄露接口特别多,每一个都手动去试根本试不过来 于是用python写了个脚本自动爬取所有接口,配置好传参发包访问 原理是首先抓取http://url/swagger-resources 获取到有哪些标准及对应的文档地

jayus 534 Dec 29, 2022
Headless chrome/chromium automation library (unofficial port of puppeteer)

Pyppeteer Pyppeteer has moved to pyppeteer/pyppeteer Unofficial Python port of puppeteer JavaScript (headless) chrome/chromium browser automation libr

miyakogi 3.5k Dec 30, 2022
LuluTest is a Python framework for creating automated browser tests.

LuluTest LuluTest is an open source browser automation framework using Python and Selenium. It is relatively lightweight in that it mostly provides wr

Erik Whiting 14 Sep 26, 2022
pytest plugin for manipulating test data directories and files

pytest-datadir pytest plugin for manipulating test data directories and files. Usage pytest-datadir will look up for a directory with the name of your

Gabriel Reis 191 Dec 21, 2022
The best, free, all in one, multichecking, pentesting utility

The best, free, all in one, multichecking, pentesting utility

Mickey 58 Jan 03, 2023
A simple tool to test internet stability.

pingtest Description A personal project for testing internet stability, intended for use in Linux and Windows.

chris 0 Oct 17, 2021
A feature flipper for Django

README Django Waffle is (yet another) feature flipper for Django. You can define the conditions for which a flag should be active, and use it in a num

952 Jan 06, 2023
WEB PENETRATION TESTING TOOL 💥

N-WEB ADVANCE WEB PENETRATION TESTING TOOL Features 🎭 Admin Panel Finder Admin Scanner Dork Generator Advance Dork Finder Extract Links No Redirect H

56 Dec 23, 2022