February 25, 2014 · python

python httplib for http debugging

In http debugging, it's useful to send a http request as exactly as you want, to pinpoint certain problem. I encountered these problems in a recent debugging session.

A simple python script can make it happen.

Send old HTTP1.0 request

    from http.client import HTTPConnection
    HTTPConnection._http_vsn = 10
    HTTPConnection._http_vsn_str = 'HTTP/1.0'

Display request response info

    conn = HTTPConnection('', 80)

Keep a serials of headers in order

    from collections import OrderedDict
    headers = OrderedDict([
        ('X-Source-Id', ''),
        ('X-Up-Bear-Type', 'CDMA'),
        ('Host', 'www.qq.com'),
        ('X-Hts_user', 'true'),
        ('X-Forwarded-For', ''),
        ('Accept', '*/*'),
        ('User-Agent', 'Wget/1.11.4 Red Hat modified')

    conn.request("GET", "/create_iphone/create.html", headers=headers)
    r = conn.getresponse()

    if r1.status != 200:

  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket