Skip to content

Instantly share code, notes, and snippets.

@mythmon
Last active December 30, 2015 12:29
Show Gist options
  • Save mythmon/7829351 to your computer and use it in GitHub Desktop.
Save mythmon/7829351 to your computer and use it in GitHub Desktop.
iPython notebook for parsing http requests.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"request = \"\"\"GET /foo/bar HTTP/1.1\n",
"Host: localhost:18080\n",
"Accept-Encoding: gzip, deflate, compress\n",
"Accept: */*\n",
"User-Agent: HTTPie/0.7.2\n",
"\n",
"\"\"\""
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import re\n",
"\n",
"req = {}\n",
"lines = request.split('\\n')\n",
"req['method'], req['path'], req['version'] = lines[0].split(' ')\n",
"req['headers'] = dict(line.split(': ', 1) for line in lines[1:] if ':' in line)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print(req)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"{'headers': {'Accept': '*/*', 'Host': 'localhost:18080', 'Accept-Encoding': 'gzip, deflate, compress', 'User-Agent': 'HTTPie/0.7.2'}, 'path': '/foo/bar', 'version': 'HTTP/1.1', 'method': 'GET'}\n"
]
}
],
"prompt_number": 11
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment