Command Line Interface¶
pyheaderparse provides a command-line interface for quick header and cookie parsing.
Installation¶
The CLI is automatically installed when you install the package:
pip install pyheaderparse
Verify installation:
$ pyheaderparse --version
pyheaderparse 1.0.0
$ pyheaderparse --help
General Usage¶
pyheaderparse <command> [options]
Available Commands¶
parse- Parse HTTP headerscookies- Parse cookiesinfo- Show header metadata/info
Global Options¶
-v, --version- Show version-h, --help- Show help
Parse Command¶
Parse HTTP headers from a file or stdin.
Basic Usage¶
# Parse from file
$ pyheaderparse parse -f headers.txt
# Parse from stdin
$ cat headers.txt | pyheaderparse parse --stdin
# Parse inline data
$ pyheaderparse parse "content-type: application/json"
Options¶
-f, --file FILE Read headers from file
--stdin Read headers from stdin
-H, --header NAME Get specific header only
-F, --format FORMAT Output format: json (default), raw, repr
Examples¶
Parse all headers (JSON output):
$ pyheaderparse parse -f headers.txt
{
"content-type": {
"value": "application/json",
"params": {}
},
"content-length": 1024,
"user-agent": "Mozilla/5.0",
"accept": [
{"type": "*/*", "q": 1.0}
],
"dnt": true
}
Get specific header:
$ pyheaderparse parse -f headers.txt --header user-agent
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/144.0.0.0"
$ pyheaderparse parse -f headers.txt --header content-length
1024
$ pyheaderparse parse -f headers.txt --header accept
[{"type": "*/*", "q": 1.0}]
Raw output format:
$ pyheaderparse parse -f headers.txt --format raw
content-type: application/json
content-length: 1024
user-agent: Mozilla/5.0
accept: [{'type': '*/*', 'q': 1.0}]
dnt: True
From stdin:
$ echo -e "content-type: text/html\naccept: */*" | pyheaderparse parse --stdin
{
"content-type": {
"value": "text/html",
"params": {}
},
"accept": [
{"type": "*/*", "q": 1.0}
]
}
From clipboard (with xclip on Linux):
$ xclip -selection clipboard -o | pyheaderparse parse --stdin
Info Command¶
Display metadata and information about headers.
Basic Usage¶
$ pyheaderparse info -f headers.txt
Options¶
-f, --file FILE Read from file
--stdin Read from stdin
-F, --format FORMAT Output format: json (default), raw, repr
Example Output¶
$ pyheaderparse info -f headers.txt
{
"total_headers": 15,
"total_cookies": 4,
"content_type": "application/json",
"content_length": 1171,
"user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/144.0.0.0",
"origin": "https://medium.com",
"is_cors": true,
"sec_fetch": {
"site": "same-origin",
"mode": "cors",
"dest": "empty",
"user": ""
},
"client_hints": {
"sec-ch-ua": [
{"brand": "Chrome", "version": "144"},
{"brand": "Not A Brand", "version": "8"}
],
"sec-ch-ua-mobile": false,
"sec-ch-ua-platform": "Windows",
"sec-ch-ua-arch": "x86",
"sec-ch-ua-bitness": "64"
}
}
Sample Headers File¶
Create a sample headers.txt file for testing:
content-length: 1171
content-type: application/json
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/144.0.0.0
accept: */*
accept-language: en-US,en;q=0.9
accept-encoding: gzip, deflate, br
cache-control: max-age=3600, public
dnt: 1
sec-ch-ua: "Chrome";v="144", "Not A Brand";v="8"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
sec-fetch-site: same-origin
sec-fetch-mode: cors
sec-fetch-dest: empty
origin: https://example.com
referer: https://example.com/page
cookie: session=abc123
cookie: user=john
cookie: token=xyz789
Piping and Scripting¶
The CLI works well in shell pipelines:
Extract specific values:
# Get just the user-agent string
$ pyheaderparse parse -f headers.txt -H user-agent -F raw
# Count cookies
$ pyheaderparse cookies -f headers.txt --full-headers | jq 'keys | length'
Use with curl:
# Save response headers
$ curl -sI https://example.com > response_headers.txt
# Parse them
$ pyheaderparse parse -f response_headers.txt
Use with httpie:
# Get headers from httpie
$ http --print=h GET https://httpbin.org/get > headers.txt
$ pyheaderparse info -f headers.txt
Process multiple files:
# Parse multiple header files
$ for f in *.headers; do
echo "=== $f ==="
pyheaderparse parse -f "$f" -H content-type
done
Exit Codes¶
0- Success1- Error (file not found, invalid input, etc.)
$ pyheaderparse parse -f nonexistent.txt
Error: File 'nonexistent.txt' not found
$ echo $?
1