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
#7
I not sure if you quite understand the task @wavic,Scapy would add a whole new level of complexity that may not be needed.
It can be use to add some features if build this,but may also not be needed.

To give n rough example when i say wrapper around Requests.
Can also bring in stuff from requests.utils eg as i do with url.user_agent,
which has a lot features that not so good documented.
import requests

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):
        return "API has not that name,check spelling"

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

    @header.setter
    def header(self):
        '''May be needed later'''
        pass
 
Use:
>>> url = Url('https://www.python.org99')
Wrong domains,chek spelling

>>> url = Url('https://www.python.org')
>>> # all Requests build in constants will be under "req"
>>> url.req.status_code
200
>>> url.req.encoding
'utf-8'

>>> # New constants added
>>> url.server
'nginx'
# Miss-spelling will call __getattr__
>>> url.serverrr
'API has not that name,check spelling'

>>> url.date
'Fri, 01 Feb 2019 01:50:02 GMT'
>>> url.user_agent
{'User-Agent': 'python-requests/2.21.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}
>>> url.user_agentt
'API has not that name,check spelling'
>>> 
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 snippsat - Feb-01-2019, 07:10 AM
RE: HTTP Headers as constants in stdlib - by wavic - Feb-02-2019, 10:52 AM
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