DEV Community

Cover image for Don't screw up reading Headers, Keep It Simple, Stupid ! HTTP or IMAP !
TAHRI Ahmed R.
TAHRI Ahmed R.

Posted on

Don't screw up reading Headers, Keep It Simple, Stupid ! HTTP or IMAP !

Why ? Indeed Why ?!

No matters your religion, IMAP4 or HTTP, you should not worries about accessing easily header and associated attributes, adjectives or values.

But even with modern language like Python we still have those :

charset = headers['Content-Type'].split(';')[-1].split('=')[-1]

I have seen so much chunk of code like this, trying to deal with them, often in a risky way to take care of more important part of the code. In face of that I say no more !

Do not forget that headers are not 1 TO 1. One header can be repeated multiple time and attribute can have multiple value within the same header. So representing them by using a ´dict()´ is not a good idea even if using case insensible dict !

Project Kiss-Headers

That is why I created a project that would remove the pain of dealing with them no matter where they came from.

It was absolutely clear that they was something missing in the python community. From the project ´psf/requests´ to ´tornado´, each one have it own implementation, each one are also incomplete. The best so far I have seen is the one from ´httpx´ project.

Mine is not perfect, but has its amount of sweetness. Beginning from auto-completion capability in python interpreter or in your beloved IDE. It should dramatically reduce stress and pain when encountering them.

GitHub logo Ousret / kiss-headers

💡Python package for HTTP/1.1 style headers. Parse headers to objects. Most advanced available structure for http headers.

HTTP Headers for Humans 👋

Object oriented headers, parser and builder.
PyPi Publish Action Code style: black Checked with mypy Download Count Total

Why

No matter if you are currently building software using HTTP or IMAP (message, email), you should not worry about easily accessing header and associated attributes, adjectives or values.

using kiss-headers from python interpreter

I have seen so many chunks of code trying to deal with these headers; often I saw this implementation:

charset = headers['Content-Type'].split(';')[-1].split('=')[-1].replace('"', '')

No more of that!

🔪 Features

kiss-headers is a basic library that allow you to handle headers as objects.

  • A backwards-compatible syntax using bracket style.
  • Capability to alter headers using simple, human-readable operator notation + and -.
  • Flexibility if headers are from an email or HTTP, use as you need with one library.
  • Ability to parse any object and extract recognized headers…

Imaging being capable of doing those things :

python kiss-headers

End note

I am pretty confident that those kind of libraries that serve reducing time spent on the little things would actually help make the life of a programmer easier. And spent a lot more time on something else.

So, do not hesitate, when you are encountering a case that respect those criteria : (i) stupid (ii) repeatably reinventing the wheel (iii) everyone is facing this issue at least once. (iv) break the loop by contributing.

Thank you :)

Discussion (0)