项目作者: akornatskyy

项目描述 :
:sparkles: Call ASGI Python application from command line, just like CURL.
高级语言: Python
项目地址: git://github.com/akornatskyy/asgi-cli.git
创建时间: 2020-08-01T13:57:28Z
项目社区:https://github.com/akornatskyy/asgi-cli

开源协议:MIT License

下载


ASGI CLI

tests
Coverage Status
pypi version

Call ASGI
Python application from command line, just like CURL.

If you’re using this tool, ★Star this repository to show your interest, please!

Install

  1. pip install -U asgi-cli

Usage

  1. asgi-cli --help
  1. usage: asgi-cli [-h] [-V] [-X COMMAND] [-H HEADER] [-d DATA | -F MULTIPART]
  2. [-I] [-b] [-p] [-n NUMBER] [-v]
  3. app [url]
  4. positional arguments:
  5. app an application module
  6. url a uniform resource locator or path (default /)
  7. optional arguments:
  8. -h, --help show this help message and exit
  9. -V, --version show program's version number and exit
  10. -X COMMAND, --request COMMAND
  11. specify request command to use, e.g. POST (default
  12. GET)
  13. -H HEADER, --header HEADER
  14. pass custom header line, e.g. -H='Accept:
  15. application/json'
  16. -d DATA, --data DATA request body data, e.g. '{"msg":"hello"}', 'msg=hello'
  17. -F MULTIPART, --form MULTIPART
  18. specify HTTP multipart POST data, e.g. name=value or
  19. name=@file
  20. -I, --head show status and headers only
  21. -b, --benchmark issue a number of requests through repeated iterations
  22. (reports throughtput and average call time)
  23. -p, --profile prints out a report of top 10 functions ordered by
  24. internal time, saves to 'stats.cprof' file
  25. -n NUMBER a number of requests to issue (default 100K)
  26. -v, --verbose make the operation more talkative

Examples

example.py:

  1. START = {
  2. "type": "http.response.start",
  3. "status": 200,
  4. "headers": [
  5. (b"content-length", b"13"),
  6. (b"content-type", b"text/html; charset=utf-8"),
  7. ],
  8. }
  9. BODY1 = {"type": "http.response.body", "body": b"Hello"}
  10. BODY2 = {"type": "http.response.body", "body": b", world!"}
  11. async def app(scope, receive, send) -> None:
  12. await send(START)
  13. await send(BODY1)
  14. await send(BODY2)

Then run the examples:

asgi-cli example:app prints response body:

  1. Hello, world!

asgi-cli -v example:app pretty prints scope and sent messages:

  1. {'scope': {'asgi': {'spec_version': '2.1', 'version': '3.0'},
  2. 'client': ('127.0.0.1', 49327),
  3. 'headers': [(b'accept', b'*/*'),
  4. (b'user-agent', b'asgi-cli/0.0.1'),
  5. (b'host', b'127.0.0.1:8000')],
  6. 'http_version': '1.1',
  7. 'method': 'GET',
  8. 'path': '/',
  9. 'query_string': b'',
  10. 'raw_path': b'/',
  11. 'root_path': '',
  12. 'scheme': 'http',
  13. 'server': ('127.0.0.1', 8000),
  14. 'type': 'http'}}
  15. {'message': {'headers': [(b'content-length', b'13'),
  16. (b'content-type', b'text/html; charset=utf-8')],
  17. 'status': 200,
  18. 'type': 'http.response.start'}}
  19. {'message': {'body': b'Hello', 'type': 'http.response.body'}}
  20. {'message': {'body': b', world!', 'type': 'http.response.body'}}

asgi-cli -b example:app shows execution stats (runs in 3 iterations, for each iteration displays requests per second and an average call time):

  1. #1 => 477.74K, 2.09μs
  2. #2 => 438.12K, 2.28μs
  3. #3 => 446.90K, 2.24μs