(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''' passUse:
>>> 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