File: echo.py

package info (click to toggle)
phantomjs 2.1.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 9,268 kB
  • ctags: 7,827
  • sloc: cpp: 6,363; ansic: 4,601; java: 2,041; python: 1,244; sh: 395; ruby: 48; cs: 27; xml: 14; makefile: 12
file content (29 lines) | stat: -rw-r--r-- 803 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import json
import urlparse
import cStringIO as StringIO

def handle_request(req):
    url = urlparse.urlparse(req.path)
    headers = {}
    for name, value in req.headers.items():
        headers[name] = value.rstrip()

    d = dict(
        command  = req.command,
        version  = req.protocol_version,
        origin   = req.client_address,
        url      = req.path,
        path     = url.path,
        params   = url.params,
        query    = url.query,
        fragment = url.fragment,
        headers  = headers,
        postdata = req.postdata
    )
    body = json.dumps(d, indent=2) + '\n'

    req.send_response(200)
    req.send_header('Content-Type', 'application/json')
    req.send_header('Content-Length', str(len(body)))
    req.end_headers()
    return StringIO.StringIO(body)