pavelkolodin
pavelkolodin

Reputation: 2977

Full control over HTTP headers in Python?

Is there any Python HTTP library that helps to imitate one of popular web-browser and has HTTPS support? I would like to define the order of HTTP headers, the presence of each exact header, the order of cookies values - everything that relates to "fingerprint" of a browser. We need that to test specific web server.

Upvotes: 1

Views: 491

Answers (2)

Piotr Dobrogost
Piotr Dobrogost

Reputation: 42465

Check out Requests which is very easy to work with and has all you need. Alternatively you can drive web browser itself from Python using Selenium

Upvotes: 0

MattH
MattH

Reputation: 38265

httplib.request will take an OrderedDict for headers. Some headers will be added automatically for protocol compliance, which will be left out if you specify them in your supplied headers.

Take a look at the putheader and _send_request methods, which you could override if their behaviour didn't suit your purposes.

>>> import httplib
>>> from collections import OrderedDict
>>> h = OrderedDict(('X-A','a'),('X-B','b'),('X-C','c'))
>>> c = httplib.HTTPConnection('localhost')
>>> c.set_debuglevel(1)
>>> r = c.request('GET','/','',h)
send: 'GET / HTTP/1.1\r\nHost: localhost\r\nAccept-Encoding: identity\r\nX-A: a\r\nX-B: b\r\nX-C: c\r\n\r\n'

Upvotes: 2

Related Questions