Skip to main content

一个用于解析cURL命令的Python库

Project description

cURL 命令解析器

一个用于解析cURL命令行并将其转换为结构化数据的Python库。

特性

  • 🔄 命令解析: 解析标准的 cURL 命令并提取关键信息
  • 📊 结构化输出: 将解析结果转换为结构化的数据格式
  • 🌐 多种输入: 支持命令行直接输入、文件读取和管道输入
  • 📋 全面支持: 支持 HTTP 方法、请求头、Cookie、请求体等常见选项
  • 🔍 参数提取: 自动解析 URL 查询参数和表单数据
  • 🛠️ CLI 工具: 提供命令行工具,支持 JSON 和文本格式输出

支持的 cURL 选项

  • -X, --request: HTTP 请求方法 (GET, POST, PUT, DELETE 等)
  • -H, --header: 请求头设置
  • -d, --data: 请求体数据
  • -b, --cookie: Cookie 设置
  • -F, --form: 表单数据
  • -u, --user: 用户认证
  • -L, --location: 跟随重定向
  • -k, --insecure: 忽略 SSL 证书验证
  • -v, --verbose: 详细输出
  • -o, --output: 输出到文件

安装

pip install curl-parser

使用方法

命令行工具

# 直接解析 cURL 命令
python curl_parser/cli.py 'curl -H "Accept: application/json" https://api.example.com'

# 从文件读取 cURL 命令
python curl_parser/cli.py -f curl_command.txt

# 通过管道输入
echo 'curl https://example.com' | python curl_parser/cli.py

# JSON 格式输出
python curl_parser/cli.py -j 'curl https://example.com'

# 输出到文件
python curl_parser/cli.py -o result.txt 'curl https://example.com'

Python API

from curl_parser import parse_curl

# 解析cURL命令
curl_command = "curl 'http://example.com' -H 'Accept: application/json'"
result = parse_curl(curl_command)

# 获取解析后的数据
print(result.parsed_data.url)  # http://example.com
print(result.parsed_data.headers)  # {'Accept': 'application/json'}

数据结构

parse_curl 函数返回 CurlParseResult 对象,包含:

  • parsed_data: 解析后的数据,类型为 ParsedCurlData
  • unresolved_data: 未被解析的数据

ParsedCurlData 包含以下字段:

  • url: 请求URL
  • params: URL查询参数
  • headers: 请求头
  • cookies: Cookie
  • data: 请求体数据
  • request: 请求方法 (默认为 "GET")

示例

from curl_parser import parse_curl

curl_command = '''
curl 'http://localhost:155/common/sso/login?_t=1757126032510_JG9BY' \
  -H 'Accept: application/json, text/plain, */*' \
  -H 'Content-Type: application/x-www-form-urlencoded;charset=UTF-8' \
  -b 'locale=und' \
  --data-raw 'username=erdcadmin&password=GaSkDZ2dZOmracDMTMoRy9uqMnxa46FK3OHnE1ppGbuz9clTDWb+nZDVdFqoatPL'
'''

result = parse_curl(curl_command)
print(result.parsed_data.url)  # http://localhost:155/common/sso/login
print(result.parsed_data.params)  # {'_t': '1757126032510_JG9BY'}
print(result.parsed_data.request)  # POST

开发

安装开发依赖

git clone https://github.com/RosApr/curl-parser.git
cd curl-parser
pip install -e .

运行测试

python ./test/test_parser.py

许可证

MIT

贡献

欢迎提交问题和Pull Request!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

easy_curl_parser-0.1.0.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

easy_curl_parser-0.1.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file easy_curl_parser-0.1.0.tar.gz.

File metadata

  • Download URL: easy_curl_parser-0.1.0.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for easy_curl_parser-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e20d587ad312ae1f039c0b96df19753dd3f7e7a5e2ebccc62e06860d2bfa6e82
MD5 ce5b5252d93889abaeb61ffbb9d7230f
BLAKE2b-256 5305d228c5d74a56838c9835c8cd69bec8c629a8455e000a7c4106cf7eeb6c7f

See more details on using hashes here.

File details

Details for the file easy_curl_parser-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for easy_curl_parser-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2cf7e9edabe68be4a8cdf0e055ec722ab29e2b5430f1e9d4b4ec38c6f7111431
MD5 69a4962e42ab47bf8daeda2db122ef95
BLAKE2b-256 238134f188775cd9b8f200fb63427347a989aae190227a664c2c72ec73ccbfd5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page