Feb-15-2021, 01:51 PM
How do I capture HTTP request headers and access their values, which native module do I use and how do I do it ?
| Get HTTP Header ... |
| Feb-15-2021, 01:51 PM How do I capture HTTP request headers and access their values, which native module do I use and how do I do it ? The example is based only on the standard library of Python. from urllib.request import Request, urlopen from urllib.error import HTTPError, URLError # You can catch HTTPError and URLError def get_header(url): header = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:77.0) Gecko/20100101 Firefox/77.0"} request = Request(url, headers=header, method="HEAD") response = urlopen(request) if response.status == 200: return dict(response.headers) get_header("https://python-forum.io/Thread-Get-HTTP-Header")PS: User-Agent is often needed in Headers, because many web applications are checking if it's a real browser and not a bot.The 3rd party requests module send some User-Agent string, which works in the most cases. Almost dead, but too lazy to die: https://sourceserver.info All humans together. We don't need politicians! Feb-15-2021, 05:53 PM Thanks for the demonstration, I managed to do it as follows: # Function -> Main function that receives request web : def application(self, environ, start_response): # Function -> Get the type of content : v_cot = environ['CONTENT_TYPE']Thanks. |
| |
| Possibly Related Threads… | |||||
| Thread | Author | Replies | Views | Last Post | |
| Review my code: convert a HTTP date header to a datetime object | stevendaprano | 1 | 5,225 | Dec-17-2022, 12:24 AM Last Post: snippsat | |
| ModuleNotFoundError: No module named 'http.client'; 'http' is not a package | abhishek81py | 1 | 19,567 | Jun-25-2020, 08:58 AM Last Post: buran | |