Python Forum
HTTP Headers as constants in stdlib
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
HTTP Headers as constants in stdlib
#9
(Feb-02-2019, 10:52 AM)wavic Wrote: Perhaps the socket module is capable to do all of this too. Good to know...
Yes,but that would be a lot work(already done bye Requests).
Requests has all features @kirans ask for,just that he want constants of HTTP headers reason he explain here.
kirans Wrote:This will eliminate a class of typo errors, and also not require one to remember the exact spelling of the Http header. Should it be referer or referrer, and does case matter, etc

To give a demo that also has spelling correction with advice,using TextBlob.
Also all constants will have lower case in calls.
import requests
from textblob import TextBlob

class Url:
    def __init__(self, url):
        try:
            self.req = requests.get(url)
            self.server = self.req.headers['Server']
            self.date = self.req.headers['Date']
            self.user_agent = requests.utils.default_headers()
        except requests.exceptions.ConnectionError as e:
            print("Wrong domains,chek spelling")

    def __getattr__(self, attr):
        spell = b = TextBlob(attr)
        return f"Name not found,did you mean <{spell.correct()}>"

    @property
    def header(self):
        '''May be needed later'''
        pass

    @header.setter
    def xname(self):
        '''May be needed later'''
        pass 
Use:
>>> header = Url('http://python-forum.io')
>>> header.server
'Apache'
>>> # Now miss-spell server
>>> header.srver
'Name not found,did you mean <server>'
>>> header.servery
'Name not found,did you mean <server>'
>>> header.user_agent
{'User-Agent': 'python-requests/2.21.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}
Now just do this with all HTTP headers,and have new library Wink
Reply


Messages In This Thread
HTTP Headers as constants in stdlib - by kirans - Jan-31-2019, 06:12 AM
RE: HTTP Headers as constants in stdlib - by kirans - Jan-31-2019, 10:03 AM
RE: HTTP Headers as constants in stdlib - by nilamo - Jan-31-2019, 04:30 PM
RE: HTTP Headers as constants in stdlib - by wavic - Jan-31-2019, 05:37 PM
RE: HTTP Headers as constants in stdlib - by wavic - Feb-02-2019, 10:52 AM
RE: HTTP Headers as constants in stdlib - by snippsat - Feb-02-2019, 05:37 PM
RE: HTTP Headers as constants in stdlib - by kirans - Feb-03-2019, 03:38 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Extracting Headers from Many Pages Quickly OstermanA 2 2,037 Oct-01-2019, 08:01 AM
Last Post: OstermanA

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020